TanStack Form 支持将数组作为表单值,包括数组内部的子对象值。
要使用数组,您可以在数组值上结合使用 field.state.value 和来自 solid-js 的 Index
function App() {
const form = createForm(() => ({
defaultValues: {
people: [],
},
}))
return (
<form.Field name="people" mode="array">
{(field) => (
<Show when={field().state.value.length > 0}>
{/* Do not change this to `For` or things will not work as-expected */}
<Index each={field().state.value}>
{
(_, i) => null // ...
}
</Index>
</Show>
)}
</form.Field>
)
}
function App() {
const form = createForm(() => ({
defaultValues: {
people: [],
},
}))
return (
<form.Field name="people" mode="array">
{(field) => (
<Show when={field().state.value.length > 0}>
{/* Do not change this to `For` or things will not work as-expected */}
<Index each={field().state.value}>
{
(_, i) => null // ...
}
</Index>
</Show>
)}
</form.Field>
)
}
您必须使用来自 solid-js 的 Index 而不是 For,因为 For 会导致数组更改时,内部组件每次都被重新渲染。
这会导致字段丢失其值,从而删除子字段的值。
每次在 field 上调用 pushValue 时,都会生成映射的 JSX。
<button onClick={() => field().pushValue({ name: '', age: 0 })} type="button">
Add person
</button>
<button onClick={() => field().pushValue({ name: '', age: 0 })} type="button">
Add person
</button>
最后,你可以像这样使用子字段:
<form.Field name={`people[${i}].name`}>
{(subField) => (
<input
value={subField().state.value}
onInput={(e) => {
subField().handleChange(e.currentTarget.value)
}}
/>
)}
</form.Field>
<form.Field name={`people[${i}].name`}>
{(subField) => (
<input
value={subField().state.value}
onInput={(e) => {
subField().handleChange(e.currentTarget.value)
}}
/>
)}
</form.Field>
function App() {
const form = createForm(() => ({
defaultValues: {
people: [],
},
onSubmit: ({ value }) => alert(JSON.stringify(value)),
}))
return (
<div>
<form
onSubmit={(e) => {
e.preventDefault()
e.stopPropagation()
form.handleSubmit()
}}
>
<form.Field name="people">
{(field) => (
<div>
<Show when={field().state.value.length > 0}>
{/* Do not change this to For or the test will fail */}
<Index each={field().state.value}>
{(_, i) => (
<form.Field name={`people[${i}].name`}>
{(subField) => (
<div>
<label>
<div>Name for person {i}</div>
<input
value={subField().state.value}
onInput={(e) => {
subField().handleChange(e.currentTarget.value)
}}
/>
</label>
</div>
)}
</form.Field>
)}
</Index>
</Show>
<button
onClick={() => field().pushValue({ name: '', age: 0 })}
type="button"
>
Add person
</button>
</div>
)}
</form.Field>
<button type="submit">Submit</button>
</form>
</div>
)
}
function App() {
const form = createForm(() => ({
defaultValues: {
people: [],
},
onSubmit: ({ value }) => alert(JSON.stringify(value)),
}))
return (
<div>
<form
onSubmit={(e) => {
e.preventDefault()
e.stopPropagation()
form.handleSubmit()
}}
>
<form.Field name="people">
{(field) => (
<div>
<Show when={field().state.value.length > 0}>
{/* Do not change this to For or the test will fail */}
<Index each={field().state.value}>
{(_, i) => (
<form.Field name={`people[${i}].name`}>
{(subField) => (
<div>
<label>
<div>Name for person {i}</div>
<input
value={subField().state.value}
onInput={(e) => {
subField().handleChange(e.currentTarget.value)
}}
/>
</label>
</div>
)}
</form.Field>
)}
</Index>
</Show>
<button
onClick={() => field().pushValue({ name: '', age: 0 })}
type="button"
>
Add person
</button>
</div>
)}
</form.Field>
<button type="submit">Submit</button>
</form>
</div>
)
}
您的每周 JavaScript 资讯。每周一免费发送给超过 10 万开发者。