Die Field-Component nutzt dieselben Properties wie die Controller-Component von React Hook Form – mit Ausnahme von render
.
Playground
import { useForm } from "react-hook-form"; import { Field, Form, } from "@mittwald/flow-react-components/react-hook-form"; import { FieldDescription, Label, TextField, } from "@mittwald/flow-react-components"; export default () => { interface Values { name: string; } const form = useForm<Values>(); return ( <Form form={form} onSubmit={console.log}> <Field name="name" rules={{ required: "The project name is required", }} > <TextField> <Label>Name</Label> <FieldDescription> The name of the project </FieldDescription> </TextField> </Field> </Form> ); }
typedField
Die typedField(form)
-Factory erzeugt eine Field-Component, die an den Typ des jeweiligen Forms angepasst ist. Dadurch führen beispielsweise unbekannte Field-Namen zu einem TypeScript-Fehler.
import { useForm } from "react-hook-form"; import { Form, typedField, } from "@mittwald/flow-react-components/react-hook-form"; import { Label, TextField, } from "@mittwald/flow-react-components"; export default () => { interface Values { myField: string; } const form = useForm<Values>(); // Only `myField` is allowed for name prop const Field = typedField(form); return ( <Form form={form} onSubmit={console.log}> <Field name="myField"> <TextField> <Label>Name</Label> </TextField> </Field> </Form> ); }