Skip to content

Commit 4ad19f1

Browse files
authored
primitive arrays and collection/object changes (#47)
related commit: discourse/discourse#31882
1 parent 949728b commit 4ad19f1

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

docs/03-code-internals/21-form-kit.md

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,8 +1039,11 @@ The object component allows to handle an object in your form.
10391039

10401040
```hbs
10411041
<Form @data={{hash foo=(hash bar=1 baz=2)}} as |form|>
1042-
<form.Object @name="foo" as |object name|>
1043-
<object.Field @name={{name}} @title={{name}} as |field|>
1042+
<form.Object @name="foo" as |object data|>
1043+
<object.Field @name="bar" @title="Bar" as |field|>
1044+
<field.Input />
1045+
</object.Field>
1046+
<object.Field @name="baz" @title="Baz" as |field|>
10441047
<field.Input />
10451048
</object.Field>
10461049
</form.Object>
@@ -1054,7 +1057,7 @@ An object must have a unique name. This name is used as a prefix for the underly
10541057
**Example**
10551058

10561059
```hbs
1057-
<form.Collection @name="foo" />
1060+
<form.Object @name="foo" />
10581061
```
10591062

10601063
## Nesting
@@ -1066,18 +1069,18 @@ An object can accept a nested Object or Collection.
10661069
```hbs
10671070
<Form @data={{hash foo=(hash bar=(hash baz=1 bol=2))}} as |form|>
10681071
<form.Object @name="foo" as |parentObject|>
1069-
<parentObject.Object @name="bar" as |childObject name|>
1070-
<childObject.Field @name={{name}} @title={{name}} as |field|>
1072+
<parentObject.Object @name="bar" as |childObject data|>
1073+
<childObject.Field @name="baz" @title="Baz" as |field|>
10711074
<field.Input />
10721075
</childObject.Field>
10731076
</parentObject.Object>
10741077
</form.Object>
10751078
</Form>
10761079
1077-
<Form @data={{hash foo=(hash bar=(array (hash baz=1) (hash baz=2)))}} as |form|>
1080+
<Form @data={{hash foo=(hash bar=(array 1 2))}} as |form|>
10781081
<form.Object @name="foo" as |parentObject|>
10791082
<parentObject.Collection @name="bar" as |collection index|>
1080-
<collection.Field @name="baz" @title="baz" as |field|>
1083+
<collection.Field @title="Baz" as |field|>
10811084
<field.Input />
10821085
</collection.Field>
10831086
<form.Button
@@ -1119,14 +1122,30 @@ For example, if collection has the name "foo", the 2nd field of the collection w
11191122

11201123
## @tagName
11211124

1122-
A collection will by default render as a `<div class="form-kit__collection>`, you can alter this behavior by setting a `@tagName`.
1125+
A collection will by default render as a `<div class="form-kit__collection>`, you can alter this behavior by using a `@tagName`.
11231126

11241127
**Example**
11251128

11261129
```hbs
11271130
<form.Collection @name="foo" @tagName="tr" />
11281131
```
11291132

1133+
## Primitive array
1134+
1135+
If the shape of your data is an array of primitives, eg: [1, 2, 3], form-kit is able to handle it. You just have to omit the name on the field in this case, as the name will be auto generated for you with the index.
1136+
1137+
**Example**
1138+
1139+
```hbs
1140+
<Form @data={{hash foo=(array 1 2)}} as |form|>
1141+
<form.Collection @name="foo" as |collection|>
1142+
<collection.Field @title="Baz" as |field|>
1143+
<field.Input />
1144+
</collection.Field>
1145+
</form.Object>
1146+
</Form>
1147+
```
1148+
11301149
## Nesting
11311150

11321151
A collection can accept a nested Object or Collection.

0 commit comments

Comments
 (0)