This library allows components composed with @truefit/bach to idiomatically use React Hook Form
npm install @truefit/bach-react-hook-form react-hook-form
or
yarn add @truefit/bach-react-hook-form react-hook-form
Allows you access to the form methods from react hook form.
Helper Signature
Parameter | Type | Description |
---|---|---|
formConfig | js object (optional) | a js object containing the props to pass to the useForm hook - see documentation for full list |
Example
import React from 'react';
import {compose, withCallback} from '@truefit/bach';
import {withForm, UseFormReturn} from '@truefit/bach-react-hook-form';
type FormValues = {
name: string;
address: string;
age: number;
};
type Props = {
formContext: UseFormReturn<FormValues>;
onSubmit: (values: FormValues) => void;
};
const WithForm = ({formContext: {register, handleSubmit}, onSubmit}: Props) => (
<div>
<form onSubmit={handleSubmit(onSubmit)}>
<input {...register('name')} />
<input {...register('address')} />
<input {...register('age')} />
<button type="submit">
Submit
</button>
</form>
</div>
);
const onSubmit = () => (values: FormValues) => {
console.log(values);
};
export default compose(
withCallback('onSubmit', onSubmit),
withForm({
defaultValues: {name: 'John Doe', address: '', age: 0},
}),
)(WithForm);
import React from 'react';
import {compose, withCallback} from '@truefit/bach';
import {withForm} from '@truefit/bach-react-hook-form';
const WithForm = ({formContext: {register, handleSubmit}, onSubmit}) => (
<div>
<form onSubmit={handleSubmit(onSubmit)}>
<input {...register('name')} />
<input {...register('address')} />
<input {...register('age')} />>
<button type="submit">
Submit
</button>
</form>
</div>
);
const onSubmit = () => (values) => {
console.log(values);
};
export default compose(
withCallback('onSubmit', onSubmit),
withForm({
defaultValues: {name: 'John Doe', address: '', age: 0},
}),
)(WithForm);
React Hook Form Hook useForm
Allows you access to the form methods from a component that is nested lower in the structure than the declaration of the form.
Example
import React from 'react';
import {compose} from '@truefit/bach';
import {withFormContext, UseFormReturn} from '@truefit/bach-react-hook-form';
type FormValues = {
name: string;
address: string;
age: number;
};
type Props = {
formContext: UseFormReturn<FormValues>;
};
const WithFormContext = ({formContext: {register}}: Props) => (
<div>
<input {...register('name')} />
<input {...register('address')} />
<input {...register('age')} />
</div>
);
export default compose(
withFormContext(),
)(WithFormContext);
import React from 'react';
import {compose} from '@truefit/bach';
import {withFormContext} from '@truefit/bach-react-hook-form';
const WithFormContext = ({formContext: {register}}) => (
<div>
<input {...register('name')} />
<input {...register('address')} />
<input {...register('age')} />
</div>
);
export default compose(
withFormContext(),
)(WithFormContext);
React Hook Form Hook useFormContext
Wraps useWatch ... see useWatch.
Accepts static configuration or a lazy eval function
Wraps useFieldArray ... see useFieldArray.
Accepts static configuration or a lazy eval function