Form Component
The <Form> component provides a structured layout for forms, seamlessly integrating with the useForm hook. It handles the form submission, displays submit and optional cancel buttons.
Basic Usage
The component renders a <form> element. Wrap your form fields within the <Form> component.
- Basic
- With useForm
- With Cancel
import { Form, InputField } from "react-better-html";
function LoginForm() {
return (
<Form submitButtonText="Login" gap={10} onSubmit={(event) => {}}>
<InputField label="Username" name="username" />
<InputField.password label="Password" name="password" />
</Form>
);
}
import { Form, InputField, useForm } from "react-better-html";
function LoginForm() {
const form = useForm({
defaultValues: {
username: "",
password: "",
},
requiredFields: ["username", "password"],
onSubmit: async (values) => {
await login(values);
},
});
return (
<Form form={form} submitButtonText="Login" gap={10}>
<InputField label="Username" {...form.getInputFieldProps("username")} />
<InputField.password label="Password" {...form.getInputFieldProps("password")} />
</Form>
);
}
The submit button is disabled while a required field is empty and is in loading state while the onSubmit promise is pending.
import { Form, InputField } from "react-better-html";
function EditForm() {
return (
<Form
submitButtonText="Save"
cancelButtonText="Discard"
onClickCancel={() => {}}
withDividers
>
<InputField label="First name" />
<InputField label="Last name" />
</Form>
);
}
Common Props
form- the object returned by theuseFormhook.name/id- passed to the<form>element.gap- the gap between the child elements within the form.withDividers- iftrue, a<Divider.horizontal />will be rendered between the child elements.actionButtonsLocation- controls the horizontal alignment of the action buttons -left,centerorright(default).isDestructive- iftrue, the submit button will be rendered using theButton.destructivevariant.renderActionButtons- additional elements rendered next to the action buttons.onSubmit- aonSubmithandler for the form. If provided, this will be used instead of theonSubmitfunction from theformprop.
Submit button
submitButtonText- the text to display on the submit button. If provided, a submit button will be rendered.submitButtonLoaderName- the name of the loader to display on the submit button.submitButtonIsLoading- renders the submit button in loading state. When theformprop is passed, itsisSubmittingstate is used as well.submitButtonIsDisabled- a boolean to manually disable the submit button. It is also disabled automatically when a required field from theformprop is empty.submitButtonId- theidattribute for the submit button.
Cancel button
onClickCancel- if provided, a cancel button will be rendered. The callback function will be called when the button is clicked.cancelButtonText- the text of the cancel button (defaults toCancel).cancelButtonLoaderName/cancelButtonIsLoading- the loading state of the cancel button.cancelButtonIsDisabled- disables the cancel button.cancelButtonId- theidattribute for the cancel button.