import { createFileRoute } from '@tanstack/react-router' import { useForm } from '@tanstack/react-form' export const Route = createFileRoute('/')({ component: Home, }) import { z } from 'zod' const createDeclarationFormSchema = z.object({ consignments: z.array(z.object({ id: z.string(), goodsItems: z.array(z.object({ id: z.string(), commodityCode: z.string(), netMass: z.number(), arr2: z.array(z.object({ id: z.string(), })) })) })), }) import { generateMock } from "@anatine/zod-mock"; const mockDeclarationFormSchema = generateMock(createDeclarationFormSchema) function Home() { // const defaultValues: z.infer = { // consignments: [], // } const defaultValues = mockDeclarationFormSchema console.log(defaultValues) return ( ) } export function MyForm({ defaultValues }) { function handleSubmit(value: z.infer) { console.log(value) } const form = useForm({ defaultValues, validators: { onSubmit: createDeclarationFormSchema, }, onSubmit: ({ value }) => handleSubmit(value), }) return (
{ e.preventDefault() form.handleSubmit() }} className="space-y-8" > {/* {(field) => (
)}
*/}
) } export function Tab({ form }) { return (
) } function DocumentList({ form, }) { return ( {(field) => ( <> {field.state.value && field.state.value.length > 0 ? (
{field.state.value.map((consignment, index) => ( ))}
) : (
No consignments
)} )}
) } function ConsignmentItem({ consignment, index, form, consignmentField }: any) { return ( ) } function GoodsItems({ form, index }: any) { return (
{(goodsItemsField) => (
{goodsItemsField.state.value && goodsItemsField.state.value.length > 0 ? (
{goodsItemsField.state.value.map((item, itemIndex) => (
) )}
) : (
No goods
) }
)}
) } function GoodsItemEditor({ form, index, itemIndex, goodsField }: any) { return (
{(field) => (
{ const val = form.state.values.consignments[index].goodsItems[itemIndex] console.log(val) console.log(form.state.values.consignments[index].goodsItems) const newValues = form.state.values.consignments[index].goodsItems.filter((_, i) => i !== itemIndex) console.log(newValues) // form.setFieldValue(`consignments[${index}].goodsItems`, newValues) goodsField.removeValue(itemIndex) setTimeout(() => { console.log(form.state.values.consignments[index].goodsItems) }, 1000) }}> Commodity Code
)}
) }