Skip to content

Commit ccc188d

Browse files
committed
Rename ArrayForm/ChildForm -> ArrayField/ObjectField
1 parent b97294e commit ccc188d

File tree

9 files changed

+38
-38
lines changed

9 files changed

+38
-38
lines changed

docs/advanced/Array-fields.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ title: Array fields
66

77
# Array fields
88

9-
To create dynamic array fields, you should use the [`ArrayForm`](/typed-react-form/reference/ArrayForm) component or [`useArrayForm`](/typed-react-form/useArrayForm) hook. These are wrappers around [`useChildForm`](/typed-react-form/reference/useChildForm) which provide useful functions and optimizations for arrays.
9+
To create dynamic array fields, you should use the [`ArrayForm`](/typed-react-form/reference/ArrayForm) component or [`useArrayForm`](/typed-react-form/useArrayForm) hook. These are wrappers around [`useObjectField`](/typed-react-form/reference/useObjectField) which provide useful functions and optimizations for arrays.
1010

1111
- [ArrayForm](/typed-react-form/reference/ArrayForm)
1212
- [useArrayForm](/typed-react-form/reference/useArrayForm)
1313

14-
If you have an array field with a constant size, you should probably just use [`ChildForm`](/typed-react-form/reference/ChildForm). (See bottom for examples)
14+
If you have an array field with a constant size, you should probably just use [`ObjectField`](/typed-react-form/reference/ObjectField). (See bottom for examples)
1515

1616
**Note on keys**: you **should** use the index as key, this seems against nature at first, but remember that this library does not rerender each time something in the array changes. When 2 array items get swapped, it does not rerender either, only when the array size changes, it rerenders. For this reason, it is not a problem (and it's recommended) to use index as the key. (This can change in the future)
1717

@@ -89,7 +89,7 @@ function ShoppingListForm() {
8989

9090
{values.map((_, i) => (
9191
// Create a child form for each item in the array, because each array item is an object.
92-
<ChildForm
92+
<ObjectField
9393
key={i} // You should index as key
9494
form={form} // Pass the parent form
9595
name={i} // Pass the current index as the name
@@ -171,7 +171,7 @@ function ShoppingListItemsForm(props: { parent: FormState<ShoppingList> }) {
171171
}
172172

173173
function ShoppingListItemForm(props: { parent: FormState<ShoppingListItem[]>; index: number; remove: (i: number) => void }) {
174-
const form = useChildForm(props.parent, props.index);
174+
const form = useObjectField(props.parent, props.index);
175175
return (
176176
<div>
177177
<Field form={form} type="text" name="name" />
@@ -186,7 +186,7 @@ function ShoppingListItemForm(props: { parent: FormState<ShoppingListItem[]>; in
186186

187187
## Fixed array example
188188

189-
A fixed array always has the same size, [`ChildForm`](/typed-react-form/reference/ChildForm) is used, and the index into the array is given using the name prop.
189+
A fixed array always has the same size, [`ObjectField`](/typed-react-form/reference/ObjectField) is used, and the index into the array is given using the name prop.
190190

191191
✔️ **Fixed array field containing strings**
192192

@@ -203,7 +203,7 @@ function AnswerForm() {
203203
console.log("submit", form.values);
204204
}}
205205
>
206-
<ChildForm
206+
<ObjectField
207207
parent={form}
208208
name="answers"
209209
render={(form) => (

docs/advanced/Object-fields.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function PersonForm() {
3333
>
3434
{/* Input on root form */}
3535
<Field form={form} type="text" name="name" />
36-
<ChildForm
36+
<ObjectField
3737
form={form}
3838
name="info"
3939
render={(form) => (

docs/advanced/Toggling-a-field.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function ToggleForm() {
5656
{/* Use the setNullOnUncheck prop. The value prop contains the value that is set when the box gets checked again, you can omit it to use the default value */}
5757
<Field form={form} name="location" type="checkbox" setNullOnUncheck />
5858
{/* ChildForm hides its contents when null/undefined by default */}
59-
<ChildForm
59+
<ObjectField
6060
form={form}
6161
name="location"
6262
render={(form) => (

docs/reference/ChildForm.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ parent: Reference
44
title: ChildForm component
55
---
66

7-
# `<ChildForm />`
7+
# `<ObjectField />`
88

99
Use the `ChildForm` component if you want to make fields inside an object field available to inputs. This is a wrapper around [`useChildForm`](/typed-react-form/reference/useChildForm).
1010

src/Components.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import React from "react";
22
import { ChildFormState, DefaultError, DefaultState, DirtyMap, ErrorMap, FieldsOfType, FormState, KeysOfType } from "./form";
3-
import { useArrayForm, useListener, useAnyListener, useChildForm, useTruthyListener } from "./hooks";
3+
import { useArrayField, useListener, useAnyListener, useObjectField, useTruthyListener } from "./hooks";
44

55
/**
6-
* Wrapper around useArrayForm (which is a wrapper around useChildForm).
6+
* Wrapper around useArrayForm (which is a wrapper around useObjectField).
77
* Exports useful functions to manipulate arrays.
88
* This hook does cause a rerender, but only if the whole array becomes null/undefined.
99
* @param parent The parent form.
1010
* @param name The parent's field to create a child form for.
1111
*/
12-
export function ArrayForm<
12+
export function ArrayField<
1313
T extends FieldsOfType<any, any[]>,
1414
K extends KeysOfType<T, any[] | object>,
1515
State = DefaultState,
@@ -28,7 +28,7 @@ export function ArrayForm<
2828
setValues: (values: NonNullable<T[K]>) => void;
2929
}) => React.ReactNode;
3030
}) {
31-
const childForm = useArrayForm(props.form, props.name);
31+
const childForm = useArrayField(props.form, props.name);
3232

3333
// Causes a rerender when the array becomes null/not null
3434
useTruthyListener(props.form, props.name);
@@ -77,13 +77,13 @@ export function AnyListener<T extends object, State = DefaultState, Error extend
7777
}
7878

7979
/**
80-
* Wrapper around useChildForm
80+
* Wrapper around useObjectField
8181
* Creates a child form for another root or child form. You must use this for object and array (see useArrayForm) fields.
8282
* This hook does cause a rerender, but only if the object field becomes null/undefined.
8383
* @param parentForm The parent form.
8484
* @param name The parent's field to create a child form for.
8585
*/
86-
export function ChildForm<
86+
export function ObjectField<
8787
T extends FieldsOfType<any, object>,
8888
K extends KeysOfType<T, object>,
8989
ParentState = DefaultState,
@@ -93,7 +93,7 @@ export function ChildForm<
9393
name: K;
9494
render?: (props: ChildFormState<T, K, ParentState, ParentError>) => React.ReactNode;
9595
}) {
96-
const childForm = useChildForm(props.form, props.name);
96+
const childForm = useObjectField(props.form, props.name);
9797
// Causes a rerender when the object value becomes null/undefined
9898
useTruthyListener(props.form, props.name);
9999

src/hooks.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export function useForm<T extends object, State = DefaultState, Error extends st
3939
* @param parentForm The parent form.
4040
* @param name The parent's field to create a child form for.
4141
*/
42-
export function useChildForm<
42+
export function useObjectField<
4343
T extends FieldsOfType<any, object | any[]>,
4444
K extends KeysOfType<T, object | any[]>,
4545
State = DefaultState,
@@ -128,13 +128,13 @@ export function useAnyListener<T extends object, State = DefaultState, Error ext
128128
* @param parentForm The parent form.
129129
* @param name The parent's field to create a child form for.
130130
*/
131-
export function useArrayForm<
131+
export function useArrayField<
132132
T extends FieldsOfType<any, any[]>,
133133
K extends KeysOfType<T, any[] | object>,
134134
State = DefaultState,
135135
Error extends string = DefaultError
136136
>(parentForm: FormState<T, State, Error>, name: K) {
137-
const form = useChildForm(parentForm, name);
137+
const form = useObjectField(parentForm, name);
138138
const oldLength = useRef(-1);
139139
const [, setRender] = useState(0);
140140

testing/src/ExampleForm.tsx

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AnyListener, ArrayForm, ChildForm, ErrorMap, Field, useForm } from "typed-react-form";
1+
import { AnyListener, ArrayField, ObjectField, ErrorMap, Field, useForm } from "typed-react-form";
22
import tv, { SchemaType } from "typed-object-validator";
33
import React from "react";
44
import { VisualRender } from "./VisualRender";
@@ -174,7 +174,7 @@ export function ExampleForm() {
174174
{/* Object field */}
175175
<label>Object field</label>
176176
<div>
177-
<ChildForm
177+
<ObjectField
178178
form={form}
179179
name="object"
180180
render={(form) => (
@@ -189,7 +189,7 @@ export function ExampleForm() {
189189
</div>
190190
<pre>
191191
{`
192-
<ChildForm
192+
<ObjectField
193193
form={form}
194194
name="parentObjectFieldName"
195195
render={(childForm) => (
@@ -217,7 +217,7 @@ export function ExampleForm() {
217217

218218
{/* Simple string array */}
219219
<label>String array</label>
220-
<ArrayForm
220+
<ArrayField
221221
form={form}
222222
name="array"
223223
render={({ form }) => (
@@ -232,7 +232,7 @@ export function ExampleForm() {
232232
/>
233233
<pre>
234234
{`
235-
<ArrayForm
235+
<ArrayField
236236
form={form}
237237
name="array"
238238
render={({ form }) => (
@@ -249,7 +249,7 @@ export function ExampleForm() {
249249

250250
{/* Dynamic string array */}
251251
<label>Dynamic string array</label>
252-
<ArrayForm
252+
<ArrayField
253253
form={form}
254254
name="array"
255255
render={({ form, append, remove }) => (
@@ -268,7 +268,7 @@ export function ExampleForm() {
268268
/>
269269
<pre>
270270
{`
271-
<ArrayForm
271+
<ArrayField
272272
form={form}
273273
name="arrayFieldName"
274274
render={({ form, append, remove }) => (
@@ -291,13 +291,13 @@ export function ExampleForm() {
291291

292292
{/* Object array */}
293293
<label>Object array</label>
294-
<ArrayForm
294+
<ArrayField
295295
form={form}
296296
name="objectArray"
297297
render={({ form }) => (
298298
<ul>
299299
{form.values.map((_, i) => (
300-
<ChildForm
300+
<ObjectField
301301
form={form}
302302
name={i}
303303
render={(form) => (
@@ -319,7 +319,7 @@ export function ExampleForm() {
319319
render={({ form }) => (
320320
<ul>
321321
{form.values.map((_, i) => (
322-
<ChildForm
322+
<ObjectField
323323
form={form}
324324
name={i}
325325
render={(form) => (
@@ -336,13 +336,13 @@ export function ExampleForm() {
336336

337337
{/* Dynamic object array */}
338338
<label>Dynamic object array</label>
339-
<ArrayForm
339+
<ArrayField
340340
form={form}
341341
name="objectArray"
342342
render={({ form, append, remove }) => (
343343
<ul>
344344
{form.values.map((_, i) => (
345-
<ChildForm
345+
<ObjectField
346346
form={form}
347347
name={i}
348348
render={(form) => (
@@ -364,13 +364,13 @@ export function ExampleForm() {
364364
/>
365365
<pre>
366366
{`
367-
<ArrayForm
367+
<ArrayField
368368
form={form}
369369
name="objectArrayField"
370370
render={({ form, append, remove }) => (
371371
<ul>
372372
{form.values.map((_, i) => (
373-
<ChildForm
373+
<ObjectField
374374
form={form}
375375
name={i}
376376
render={(form) => (

testing/src/OneOfObjectArrayForm.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from "react";
2-
import { AnyListener, ArrayForm, Field, FormState, useChildForm, useForm, useListener } from "typed-react-form";
2+
import { AnyListener, ArrayField, Field, FormState, useObjectField, useForm, useListener } from "typed-react-form";
33

44
interface Apple {
55
type: "apple";
@@ -40,7 +40,7 @@ export default function OneOfObjectArrayForm() {
4040
}}
4141
>
4242
<a href="https://github.com/CodeStix/typed-react-form/blob/master/example/src/OneOfObjectArrayForm.tsx">View source code</a>
43-
<ArrayForm
43+
<ArrayField
4444
form={form}
4545
name="objects"
4646
render={({ form, values, append, remove }) => (
@@ -82,7 +82,7 @@ export default function OneOfObjectArrayForm() {
8282

8383
function BreadOrAppleForm(props: { parent: FormState<FormDataObject[]>; index: number; remove: () => void }) {
8484
// Create a new child form with the array as the parent and index as the key
85-
const form = useChildForm(props.parent, props.index);
85+
const form = useObjectField(props.parent, props.index);
8686
// Listen for changes on the 'type' field, which contains 'apple' or 'bread'. This component will rerender when it changes
8787
const { value: type } = useListener(form, "type");
8888
return (

testing/src/OneOfObjectForm.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from "react";
2-
import { AnyListener, Field, FormState, Listener, useChildForm, useForm } from "typed-react-form";
2+
import { AnyListener, Field, FormState, Listener, useObjectField, useForm } from "typed-react-form";
33

44
interface Apple {
55
type: "apple";
@@ -59,7 +59,7 @@ export default function OneOfObjectArrayForm() {
5959

6060
function AppleOrBreadForm(props: { parent: FormState<FormData> }) {
6161
// Create a new form based on the 'breadOrApple' field
62-
const form = useChildForm(props.parent, "breadOrApple");
62+
const form = useObjectField(props.parent, "breadOrApple");
6363
return (
6464
<div style={{ background: "#0001", padding: "1em", margin: "1em" }}>
6565
<label>Object type: </label>

0 commit comments

Comments
 (0)