import * as React from 'react'; import { createRoot } from 'react-dom/client'; import { useForm } from '@tanstack/react-form'; import { z } from 'zod'; import { FieldError } from './field-error'; import './index.css'; interface Role { external: External; name: string; } interface Person { name: string; age: number; roles: Role[]; } enum External { True = 'true', False = 'false', } const defaultPerson: Person = { name: 'John Doe', age: 30, roles: [ { name: 'Admin', external: External.False }, { name: 'User', external: External.False }, ], }; const roles: Role[] = [ { name: 'Admin', external: External.False }, { name: 'User', external: External.False }, ]; function App() { const form = useForm({ defaultValues: defaultPerson, onSubmit({ value }) { alert(JSON.stringify(value)); }, }); return (
{ e.preventDefault(); e.stopPropagation(); form.handleSubmit(); }} > {(field) => { return ( <> {roles.map((role, idx) => ( {(subField) => (
stateRole.name === role.name )} name={subField.state.value} onBlur={subField.handleBlur} onChange={(event) => { if (event.target.checked) { field.pushValue(role); } else { const roleIdx = field.state.value.findIndex( (stateRole) => stateRole.name === role.name ); field.removeValue(roleIdx); } }} value={role.name} />
)}
))} ); }}
[state.canSubmit, state.isSubmitting]} children={([canSubmit, isSubmitting]) => ( )} /> state.values}> {(values) => ( <>
                {JSON.stringify(values, null, 4)}
              
)}
); } const rootElement = document.getElementById('root')!; createRoot(rootElement).render( );