Playground
import { useForm } from "react-hook-form"; import { Field, Form, } from "@mittwald/flow-react-components/react-hook-form"; import { ActionGroup, Button, FieldDescription, Label, Section, TextField, } from "@mittwald/flow-react-components"; export default () => { interface Values { name: string; } const form = useForm<Values>(); const handleOnSubmit = (values: Values) => alert(JSON.stringify(values)); return ( <Form form={form} onSubmit={handleOnSubmit}> <Section> <Field name="name" rules={{ required: "The project name is required", }} > <TextField> <Label>Name</Label> <FieldDescription> The name of the project </FieldDescription> </TextField> </Field> <ActionGroup> <Button type="submit">Save</Button> </ActionGroup> </Section> </Form> ); }
Aktionen nach Submit
Aktionen, die nach dem Submit ausgeführt werden sollen – zum Beispiel ein Form-Reset oder einen Redirect – sollten im Return-Callback des Submit Handlers erfolgen. Dieser Callback kann auch asynchron sein.
const submitHandler = () => {
// submit logic
return () => {
form.reset();
};
};
import { sleepLong } from "@/content/03-components/actions/action/examples/lib"; import { ActionGroup, Button, FieldDescription, Label, TextField, } from "@mittwald/flow-react-components"; import { Field, Form, } from "@mittwald/flow-react-components/react-hook-form"; import { useForm } from "react-hook-form"; export default () => { interface Values { name: string; } const form = useForm<Values>({ defaultValues: { name: "", }, }); return ( <Form form={form} onSubmit={async () => { await sleepLong(); // recommended way to reset forms after submit return () => { form.reset(); }; }} > <Field name="name" rules={{ required: "The project name is required", }} > <TextField> <Label>Name</Label> <FieldDescription> The name of the project </FieldDescription> </TextField> </Field> <ActionGroup> <Button type="submit">Save</Button> </ActionGroup> </Form> ); }