()
| 5 | import { Index, Show } from 'solid-js' |
| 6 | |
| 7 | function App() { |
| 8 | const form = createForm(() => ({ |
| 9 | defaultValues: { |
| 10 | people: [] as Array<{ age: number; name: string }>, |
| 11 | }, |
| 12 | onSubmit: ({ value }) => alert(JSON.stringify(value)), |
| 13 | })) |
| 14 | |
| 15 | return ( |
| 16 | <div> |
| 17 | <form |
| 18 | onSubmit={(e) => { |
| 19 | e.preventDefault() |
| 20 | e.stopPropagation() |
| 21 | form.handleSubmit() |
| 22 | }} |
| 23 | > |
| 24 | <form.Field name="people"> |
| 25 | {(field) => ( |
| 26 | <div> |
| 27 | <Show when={field().state.value.length > 0}> |
| 28 | {/* Do not change this to For or the test will fail */} |
| 29 | <Index each={field().state.value}> |
| 30 | {(_, i) => ( |
| 31 | <form.Field name={`people[${i}].name`}> |
| 32 | {(subField) => ( |
| 33 | <div> |
| 34 | <label> |
| 35 | <div>Name for person {i}</div> |
| 36 | <input |
| 37 | value={subField().state.value} |
| 38 | onInput={(e) => { |
| 39 | subField().handleChange(e.currentTarget.value) |
| 40 | }} |
| 41 | /> |
| 42 | </label> |
| 43 | </div> |
| 44 | )} |
| 45 | </form.Field> |
| 46 | )} |
| 47 | </Index> |
| 48 | </Show> |
| 49 | |
| 50 | <button |
| 51 | onClick={() => field().pushValue({ name: '', age: 0 })} |
| 52 | type="button" |
| 53 | > |
| 54 | Add person |
| 55 | </button> |
| 56 | </div> |
| 57 | )} |
| 58 | </form.Field> |
| 59 | <button type="submit">Submit</button> |
| 60 | </form> |
| 61 | </div> |
| 62 | ) |
| 63 | } |
| 64 |
nothing calls this directly
no test coverage detected