Generic bridge between React Hook Form (RHF) and Semantic UI React controls. It wires a field via RHF’s Controller and renders a Semantic UI Form.Field
, surfacing validation errors in the usual Semantic way.
Highlights
{ checked }
vs { value }
).import { useForm } from 'react-hook-form';
import { Form, Input } from 'semantic-ui-react';
import { HookFormField } from '@karmaniverous/hook-form-semantic';
type FormData = { email: string };
export function ProfileEmailForm() {
const { control, handleSubmit } = useForm<FormData>({
defaultValues: { email: '' },
mode: 'onTouched',
});
return (
<Form onSubmit={handleSubmit(console.log)}>
<HookFormField<FormData, { value: string }>
control={Input}
hookControl={control}
hookName="email"
hookRules={{ required: 'Email is required' }}
label="Email"
placeholder="you@example.com"
/>
<Form.Button primary>Save</Form.Button>
</Form>
);
}
Use this when your control reports state via something other than data.value
.
import { useForm, ControllerRenderProps, Path } from 'react-hook-form';
import { Checkbox, Form } from 'semantic-ui-react';
import { HookFormField } from '@karmaniverous/hook-form-semantic';
type FormData = { subscribed: boolean };
export function MarketingPrefs() {
const { control, handleSubmit } = useForm<FormData>({
defaultValues: { subscribed: false },
});
return (
<Form onSubmit={handleSubmit(console.log)}>
<HookFormField<FormData, { checked: boolean }>
hookControl={control}
hookName="subscribed"
label="Marketing emails"
>
{(field) => {
const f = field as ControllerRenderProps<FormData, Path<FormData>> & {
checked?: boolean;
};
return (
<Checkbox
label="Subscribe me"
checked={!!f.checked}
onChange={(_, data) => f.onChange(_, { checked: !!data.checked })}
/>
);
}}
</HookFormField>
<Form.Button primary>Update</Form.Button>
</Form>
);
}
Tips
control={Input}
form when the widget’s change handler already uses { value }
.checked
, tuples, complex objects).