Files
aso/frontend/hospital-portal/src/components/hook-form/RHFSwitch.tsx
2023-07-03 11:39:08 +07:00

56 lines
1.3 KiB
TypeScript

// form
import { useFormContext, Controller } from 'react-hook-form';
// @mui
import { Switch, FormControlLabel, FormControlLabelProps } from '@mui/material';
import { useEffect, useState } from 'react';
// ----------------------------------------------------------------------
type IProps = Omit<FormControlLabelProps, 'control'>;
interface Props extends IProps {
name: string;
}
export default function RHFSwitch({ name, ...other }: Props) {
const { control } = useFormContext();
return (
<FormControlLabel
control={
<Controller
name={name}
control={control}
render={({ field }) => <Switch {...field} checked={field.value} />}
/>
}
{...other}
/>
);
}
// interface Props extends IProps {
// valueTrue: any,
// valueFalse: any,
// }
// export function RHFSwitchEnum({ name, valueTrue, valueFalse, ...other}: Props) {
// const { control } = useFormContext();
// const [isChecked, setIsChecked] = useState(valueTrue === field.value);
// useEffect(() => {
// // setIsChecked()
// }, [])
// return (
// <FormControlLabel
// control={
// <Controller
// name={name}
// control={control}
// render={({ field }) => <Switch {...field} checked={field.value} />}
// />
// }
// {...other}
// />
// );
// }