Skip to content

A platform for community discussion. Free, open, simple.

DEV: generate identifier for collections (#32368)

Joffrey JAFFEUXe887a389fe05
diff --git a/app/assets/javascripts/discourse/app/form-kit/components/fk/collection.gjs b/app/assets/javascripts/discourse/app/form-kit/components/fk/collection.gjs
index 40479c3a296..a5498b7f691 100644
--- a/app/assets/javascripts/discourse/app/form-kit/components/fk/collection.gjs
+++ b/app/assets/javascripts/discourse/app/form-kit/components/fk/collection.gjs
@@ -1,6 +1,8 @@
import Component from "@glimmer/component";
-import { concat, hash } from "@ember/helper";
-import { action, get } from "@ember/object";
+import { hash } from "@ember/helper";
+import { action } from "@ember/object";
+import { getOwner } from "@ember/owner";
+import curryComponent from "ember-curry-component";
import FKField from "discourse/form-kit/components/fk/field";
import FKObject from "discourse/form-kit/components/fk/object";
import element from "discourse/helpers/element";
@@ -11,8 +13,39 @@ export default class FKCollection extends Component {
this.args.remove(this.name, index);
}
+ @action
+ componentFor(componentClass, index) {
+ const instance = this;
+ const baseArguments = {
+ collectionIndex: index,
+ addError: instance.args.addError,
+ set: instance.args.set,
+ triggerRevalidationFor: instance.args.triggerRevalidationFor,
+ parentName: `${instance.name}.${index}`,
+ registerField: instance.args.registerField,
+ unregisterField: instance.args.unregisterField,
+ get errors() {
+ return instance.args.errors;
+ },
+ get data() {
+ return instance.args.data;
+ },
+ };
+
+ if (componentClass === FKCollection || componentClass === FKObject) {
+ baseArguments.remove = instance.args.remove;
+ }
+
+ return curryComponent(componentClass, baseArguments, getOwner(this));
+ }
+
get collectionData() {
- return this.args.data.get(this.name);
+ return this.args.data.get(this.name).map((item, index) => {
+ return {
+ identifier: `${this.name}-${index}`,
+ item,
+ };
+ });
}
get name() {
@@ -30,49 +63,16 @@ export default class FKCollection extends Component {
<template>
{{#let (element this.tagName) as |Wrapper|}}
<Wrapper class="form-kit__collection">
- {{#each this.collectionData key="index" as |data index|}}
+ {{#each this.collectionData key="identifier" as |data index|}}
{{yield
(hash
- Field=(component
- FKField
- errors=@errors
- collectionIndex=index
- addError=@addError
- data=@data
- set=@set
- registerField=@registerField
- unregisterField=@unregisterField
- triggerRevalidationFor=@triggerRevalidationFor
- parentName=(concat this.name "." index)
- )
- Object=(component
- FKObject
- errors=@errors
- addError=@addError
- data=@data
- set=@set
- registerField=@registerField
- unregisterField=@unregisterField
- triggerRevalidationFor=@triggerRevalidationFor
- parentName=(concat this.name "." index)
- remove=@remove
- )
- Collection=(component
- FKCollection
- errors=@errors
- addError=@addError
- data=@data
- set=@set
- registerField=@registerField
- unregisterField=@unregisterField
- triggerRevalidationFor=@triggerRevalidationFor
- parentName=(concat this.name "." index)
- remove=@remove
- )
+ Field=(this.componentFor FKField index)
+ Object=(this.componentFor FKObject index)
+ Collection=(this.componentFor FKCollection index)
remove=this.remove
)
index
- (get this.collectionData index)
+ data.item
}}
{{/each}}
</Wrapper>
diff --git a/app/assets/javascripts/discourse/tests/integration/components/form-kit/collection-test.gjs b/app/assets/javascripts/discourse/tests/integration/components/form-kit/collection-test.gjs
index 554b5279452..dc399ba885f 100644
--- a/app/assets/javascripts/discourse/tests/integration/components/form-kit/collection-test.gjs
+++ b/app/assets/javascripts/discourse/tests/integration/components/form-kit/collection-test.gjs
@@ -175,4 +175,34 @@ module("Integration | Component | FormKit | Collection", function (hooks) {
assert.form().field("one.0.two.0.three.0.foo").hasValue("2");
});
+
+ test("emptying a collection field", async function (assert) {
+ const onSubmit = (data) => {
+ assert.deepEqual(
+ data.animals,
+ ["souna", undefined],
+ "correctly makes the field undefined"
+ );
+ };
+
+ await render(
+ <template>
+ <Form
+ @data={{hash animals=(array "souna" "sissi")}}
+ @onSubmit={{onSubmit}}
+ as |form|
+ >
+ <form.Collection @name="animals" as |collection|>
+ <collection.Field @title="cat" as |field|>
+ <field.Input />
+ </collection.Field>
+ </form.Collection>
+ <form.Submit />
+ </Form>
+ </template>
+ );
+
+ await formKit().field("animals.1").fillIn("");
+ await formKit().submit();
+ });
});