Form (React Hook Form)

Die Form-Component gruppiert Formularfelder und stellt einen Callback für das Absenden bereit. Sie ist ausschließlich in Kombination mit React Hook Form nutzbar.GitHub
The name of the project

Playground

The name of the project
import { useForm } from "react-hook-form";
import {
  Field,
  Form,
  SubmitButton,
  ResetButton,
} from "@mittwald/flow-react-components/react-hook-form";
import {
  ActionGroup,
  FieldDescription,
  Label,
  Section,
  TextField,
} from "@mittwald/flow-react-components";

export default () => {
  interface Values {
    name: string;
  }
  const form = useForm<Values>();

  const handleSubmit = (values: Values) =>
    alert(JSON.stringify(values));

  return (
    <Form form={form} onSubmit={handleSubmit}>
      <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>
          <ResetButton>Reset</ResetButton>
          <SubmitButton>Save</SubmitButton>
        </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();
  };
};
The name of the project

Overview