This repository has been archived by the owner on Jul 27, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(useFieldArray): nested field array not working
- Loading branch information
Showing
5 changed files
with
115 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"react-cool-form": patch | ||
--- | ||
|
||
fix(useFieldArray): nested field array not working |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,92 @@ | ||
/* eslint-disable no-console */ | ||
|
||
import { useForm } from "react-cool-form"; | ||
import { useForm, useFieldArray } from "react-cool-form"; | ||
|
||
export default () => { | ||
const { form, runValidation } = useForm({ | ||
// validate: () => ({ foo: "Required" }), | ||
focusOnError: ["foo"], | ||
const { form } = useForm({ | ||
defaultValues: { | ||
foo: [ | ||
{ | ||
name: "Iron Man", | ||
arr: [{ name: "iron arr.0" }, { name: "iron arr.1" }], | ||
}, | ||
], | ||
}, | ||
onSubmit: (values) => alert(JSON.stringify(values, undefined, 2)), | ||
}); | ||
const [fields, { push, insert, remove }] = useFieldArray("foo"); | ||
|
||
return ( | ||
<> | ||
<form ref={form} noValidate> | ||
<input name="foo" required /> | ||
<input name="bar" required /> | ||
{/* <input type="submit" /> */} | ||
</form> | ||
<button type="button" onClick={() => runValidation(["bar"])}> | ||
Validate | ||
</button> | ||
</> | ||
<form ref={form}> | ||
<table> | ||
<thead> | ||
<tr> | ||
<th>Name</th> | ||
<th>Arr</th> | ||
<th>Actions</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{fields.map((fieldName, index) => ( | ||
<tr key={fieldName}> | ||
<td> | ||
<input name={`${fieldName}.name`} /> | ||
</td> | ||
<td> | ||
<Arr field={fieldName} /> | ||
</td> | ||
<td> | ||
<button type="button" onClick={() => remove(index)}> | ||
REMOVE | ||
</button> | ||
</td> | ||
</tr> | ||
))} | ||
</tbody> | ||
</table> | ||
<div> | ||
<button | ||
type="button" | ||
onClick={() => { | ||
push({ name: "Loki", arr: [{ name: "Your Savior Is Here" }] }); | ||
}} | ||
> | ||
PUSH | ||
</button> | ||
<button | ||
type="button" | ||
onClick={() => | ||
insert(0, { | ||
name: "Spider Man", | ||
arr: [{ name: "Your Friendly Neighborhood Spider-Man" }], | ||
}) | ||
} | ||
> | ||
INSERT | ||
</button> | ||
</div> | ||
<input type="submit" /> | ||
<input type="reset" /> | ||
</form> | ||
); | ||
}; | ||
|
||
function Arr({ field }: any) { | ||
const [fields, { push }] = useFieldArray(`${field}.arr`); | ||
|
||
return ( | ||
<div> | ||
{fields.map((fieldName) => ( | ||
<input | ||
key={fieldName} | ||
style={{ height: "20px", width: "100px" }} | ||
type="text" | ||
name={`${fieldName}.name`} | ||
/> | ||
))} | ||
<button type="button" onClick={() => push({ name: "xxx" })}> | ||
inner push | ||
</button> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters