Open reboottime opened 1 year ago
Now let's explore some common form usage in frontend development:
Form Input and Validation: Users interact with the form by filling in various fields, and we validate these fields based on specific criteria. For instance:
Form Submission: After the user has filled out the form, we submit the form data to our backend services.
Loading Form Initial Data: In some cases, we may need to load initial data into the form from backend services.
Manipulate Form Programmatically, for example:
The section covers three topics:
Afterward, we will delve into the details of the above points.
The most common pattern for designing highly customized components has two forms:
A hook-based API that covers customization/initialization and exposes state management functionalities to developers. For instance:
function Demo() {
const form = useForm({
initialValues: {
email: '',
termsOfService: false,
},
validate: {
email: (value) => (/^\S+@\S+$/.test(value) ? null : 'Invalid email'),
},
});
return (
<form onSubmit={form.onSubmit((values) => console.log(values))}>
<TextInput
withAsterisk
label="Email"
placeholder="your@email.com"
{...form.getInputProps('email')}
/>
<Checkbox
mt="md"
label="I agree to sell my privacy"
{...form.getInputProps('termsOfService', { type: 'checkbox' })}
/>
<Group position="right" mt="md">
<Button type="submit">Submit</Button>
</Group>
</form>
);
}
A component that accepts the state initialization/customization. For instance:
<Form onSubmit={submit} initialValues={initialValues} validators={validators}>
{(form) => <>{/**a set of form fields managed by the form**/}</>}
</Form>;
From an end-users' perspective, the key aspects of the form state that we care about are:
const [touched, setTouched] = useState(initialTouched);
const [dirty, setDirty] = useState(initialDirty);
const [values, _setValues] = useState(initialValues);
const [errors, _setErrors] = useState(filterErrors(initialErrors));
Correspondingly,
Form
and useForm
and provide programmers the interface to extend form validation via validate
// The params available to initialize for the form
export function useForm<
Values = Record<string, unknown>,
TransformValues extends _TransformValues<Values> = (values: Values) => Values
>({
initialValues = {} as Values,
initialErrors = {},
initialDirty = {},
initialTouched = {},
clearInputErrorOnChange = true,
validateInputOnChange = false,
validateInputOnBlur = false,
validate: rules,
}
// The values/functions exposed for the `useForm` hook
return {
values,
errors,
setValues,
setErrors,
setFieldValue,
setFieldError,
clearFieldError,
clearErrors,
reset,
validate,
onSubmit,
onReset,
isDirty,
isTouched,
setTouched,
setDirty,
resetTouched,
resetDirty,
isValid,
};
Introduction
This article discuss
Mantine UI Form
component in React. The main topics covered include:Given the fact that form solutions are highly customized based on real-world use cases, our analysis starts with examining usage scenarios.
The best practices are covered in Mantine UI Form Part II