forked from Smart-Beaver/wizard-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwitch.tsx
More file actions
53 lines (50 loc) · 1.81 KB
/
Switch.tsx
File metadata and controls
53 lines (50 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { Switch as HeadlessSwitch } from '@headlessui/react';
import { useController } from 'react-hook-form';
import { ErrorMessageForm } from '@/components/data-entry/ErrorMessageForm';
import { dictionary } from '@/libs/en';
import { cn } from '@/utils/helpers/cn';
interface SwitchProps {
label?: string;
name: string;
disabled?: boolean;
}
export default function Switch({ name, label, disabled }: SwitchProps) {
const {
fieldState: { error },
field: { onChange, ...restField }
} = useController({ name });
return (
<>
<HeadlessSwitch.Group as="div" className="items-left flex gap-8 py-6">
<HeadlessSwitch
{...restField}
onChange={(e) => {
onChange(e);
}}
disabled={disabled}
className={cn(
'border-transparent relative inline-flex h-6 w-11 flex-shrink-0 cursor-pointer rounded-full border-2 transition-colors duration-200 ease-in-out focus:outline-none focus:ring-2 focus:ring-teal-700 focus:ring-offset-2 ',
restField.value ? 'bg-teal-600' : 'bg-transparent',
disabled && 'opacity-50'
)}
>
<span
aria-hidden="true"
className={cn(
'pointer-events-none inline-block h-5 w-5 transform rounded-full bg-white shadow ring-0 transition duration-200 ease-in-out',
restField.value ? 'translate-x-5' : 'translate-x-0'
)}
/>
</HeadlessSwitch>
{label && (
<HeadlessSwitch.Label as="div">
<div className="font-medium text-teal-600">{label}</div>
</HeadlessSwitch.Label>
)}
</HeadlessSwitch.Group>
{error && (
<ErrorMessageForm errorMessage={error.message ? error.message : dictionary.validation.defaultErrorMsg} />
)}
</>
);
}