Skip to main content

<Form>

The <Form> component is the alternative way to initialize a @per-form/react form (it uses useForm and <FormProvider> internally).

Props

<Form> takes the following props:

PropertyTypeDefaultDescription
childrenReactNode | ((data: IFormContext) => ReactNode)The <Form> children. Can be used as a render props to get the form context
defaultValuesRecord<string, unknown>Form default values (see default values guide for more information)
filterLocalErrorsbooleantrueFilter local errors from the global form errors (see local fields guide for more information)
focusOnErrorbooleantrueFocus the first field error or not
formHTMLFormElementnullForm ref
messagesRecord<string, string>Custom messages (see messages guide for more information)
mode'all' | 'blur' | 'change' | 'submit''submit'Validation strategy (see mode guide for more information)
onBlurOptOutstring[] | stringOpt-out for native onBlur event
onChangeOptOutstring[] | stringOpt-out for native onChange event (see controlled components guide for more information)
onResetIResetHandlerReset callback
onSubmitISubmitHandlerSubmit callback
onSubmitErrorISubmitErrorHandlerSubmit error callback (only when useNativeValidation=false)
revalidateMode'blur' | 'change' | 'submit''submit'Re-validation strategy (see mode guide for more information)
transformersRecord<string, (value: unknown) => unknown>Transformers for type casting see type casting guide for more information)
useNativeValidationbooleanUse native browser validation or use error state
validatorsRecord<string, IValidator | IValidatorObject>Custom field validators (see validation guide for more information)
...restunknown[]All other props are forwarded to the <form> element

See here for the types.

Usage

0
import type { FormEvent } from 'react';
import type { IProps } from '../types';
import { Form, type IFormContext, type IFormValues } from '@per-form/react';

export default function Demo(props: IProps) {
function handleSubmit(_e: FormEvent<HTMLFormElement>, values: IFormValues) {
console.log(values);
}

return (
<Form {...props} onSubmit={handleSubmit}>
{({ errors }: IFormContext) => (
<>
<input name="text" required />
{errors.all.text && <div className="error">{errors.all.text}</div>}
<button type="submit">Submit</button>
</>
)}
</Form>
);
}