Files
explore/toolkit/components/forms/fields/FormFieldSwitch.tsx
T
tom goriunovandGitHub cb42262b5e Update UI toolkit (#2741)
* add more form fields and fix link props

* extend select component to render custom item and add lg size to switch

* fix build

* fix button border radius

* fix ts
2025-06-04 14:30:49 +02:00

60 lines
1.5 KiB
TypeScript

import React from 'react';
import type { FieldValues, Path } from 'react-hook-form';
import { useController, useFormContext } from 'react-hook-form';
import type { FormFieldPropsBase } from './types';
import { Switch } from '../../../chakra/switch';
import type { SwitchProps } from '../../../chakra/switch';
export type FormFieldSwitchProps<
FormFields extends FieldValues,
Name extends Path<FormFields>,
> = Pick<
FormFieldPropsBase<FormFields, Name>,
'name' | 'placeholder' | 'rules' | 'controllerProps'
> &
SwitchProps;
const FormFieldSwitchContent = <
FormFields extends FieldValues,
Name extends Path<FormFields>,
>({
name,
placeholder,
onCheckedChange,
rules,
controllerProps,
disabled,
...rest
}: FormFieldSwitchProps<FormFields, Name>) => {
const { control } = useFormContext<FormFields>();
const { field, formState } = useController<FormFields, Name>({
control,
name,
rules,
...controllerProps,
});
const handleCheckedChange = React.useCallback(({ checked }: { checked: boolean }) => {
field.onChange(checked);
onCheckedChange?.({ checked });
}, [ field, onCheckedChange ],
);
return (
<Switch
name={ field.name }
checked={ field.value }
onCheckedChange={ handleCheckedChange }
disabled={ formState.isSubmitting || disabled }
inputProps={{ onBlur: field.onBlur }}
{ ...rest }
>
{ placeholder }
</Switch>
);
};
export const FormFieldSwitch = React.memo(FormFieldSwitchContent) as typeof FormFieldSwitchContent;