2025-03-06 08:18:36 +08:00
|
|
|
import { Field, Label, Switch as HeadlessSwitch } from '@headlessui/react'
|
|
|
|
|
import { type ReactNode } from 'react'
|
|
|
|
|
import {
|
|
|
|
|
Controller,
|
|
|
|
|
get,
|
|
|
|
|
type FieldError,
|
|
|
|
|
type FieldValues,
|
|
|
|
|
type Path,
|
|
|
|
|
type RegisterOptions,
|
|
|
|
|
} from 'react-hook-form'
|
|
|
|
|
import { useRemixFormContext } from 'remix-hook-form'
|
|
|
|
|
import { twMerge } from 'tailwind-merge'
|
|
|
|
|
|
|
|
|
|
type TSwitchProperties<T extends FieldValues> = {
|
|
|
|
|
id: string
|
|
|
|
|
label?: ReactNode
|
|
|
|
|
name: Path<T>
|
|
|
|
|
rules?: RegisterOptions
|
|
|
|
|
containerClassName?: string
|
|
|
|
|
labelClassName?: string
|
|
|
|
|
className?: string
|
|
|
|
|
inputClassName?: string
|
2025-03-07 10:30:46 +08:00
|
|
|
options?: {
|
|
|
|
|
true: string
|
|
|
|
|
false: string
|
|
|
|
|
}
|
2025-03-06 08:18:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const Switch = <TFormValues extends Record<string, unknown>>(
|
|
|
|
|
properties: TSwitchProperties<TFormValues>,
|
|
|
|
|
) => {
|
|
|
|
|
const {
|
|
|
|
|
id,
|
|
|
|
|
label,
|
|
|
|
|
name,
|
|
|
|
|
rules,
|
|
|
|
|
containerClassName,
|
|
|
|
|
labelClassName,
|
|
|
|
|
className,
|
|
|
|
|
inputClassName,
|
2025-03-07 10:30:46 +08:00
|
|
|
options,
|
2025-03-06 08:18:36 +08:00
|
|
|
} = properties
|
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
control,
|
|
|
|
|
formState: { errors },
|
|
|
|
|
} = useRemixFormContext()
|
|
|
|
|
|
|
|
|
|
const error: FieldError = get(errors, name)
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Field
|
|
|
|
|
className={twMerge('relative', containerClassName)}
|
|
|
|
|
id={id}
|
|
|
|
|
>
|
|
|
|
|
<Label className={twMerge('mb-1 block text-gray-700', labelClassName)}>
|
|
|
|
|
{label} {error && <span className="text-red-500">{error.message}</span>}
|
|
|
|
|
</Label>
|
|
|
|
|
<Controller
|
|
|
|
|
name={name}
|
|
|
|
|
control={control}
|
|
|
|
|
rules={rules}
|
|
|
|
|
render={({ field }) => (
|
2025-03-06 09:01:26 +08:00
|
|
|
<div className={twMerge('flex items-center', className)}>
|
2025-03-06 08:18:36 +08:00
|
|
|
<HeadlessSwitch
|
|
|
|
|
checked={field.value}
|
|
|
|
|
onChange={(checked) => {
|
|
|
|
|
field.onChange(checked)
|
|
|
|
|
}}
|
|
|
|
|
className={twMerge(
|
2025-03-07 10:30:46 +08:00
|
|
|
'group flex h-7 cursor-pointer items-center rounded-full bg-[#2E2F7C]/10 p-1 shadow transition-colors duration-200 ease-in-out focus:outline-none data-[checked]:bg-[#2E2F7C]/90 data-[focus]:outline-1 data-[focus]:outline-white',
|
2025-03-06 09:01:26 +08:00
|
|
|
inputClassName,
|
2025-03-06 08:18:36 +08:00
|
|
|
)}
|
|
|
|
|
>
|
2025-03-07 10:30:46 +08:00
|
|
|
<div
|
|
|
|
|
className={twMerge(
|
|
|
|
|
'order-2 text-xs transition duration-200 group-data-[checked]:order-1',
|
|
|
|
|
options?.true ? 'w-18' : 'w-8',
|
|
|
|
|
field.value ? 'text-white' : 'text-black/80',
|
|
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
{field.value ? options?.true : options?.false}
|
|
|
|
|
</div>
|
|
|
|
|
<div
|
2025-03-06 08:18:36 +08:00
|
|
|
aria-hidden="true"
|
2025-03-07 10:30:46 +08:00
|
|
|
className="pointer-events-none order-1 size-5 rounded-full bg-white ring-0 shadow-lg transition duration-200 group-data-[checked]:order-2"
|
2025-03-06 08:18:36 +08:00
|
|
|
/>
|
|
|
|
|
</HeadlessSwitch>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
</Field>
|
|
|
|
|
)
|
|
|
|
|
}
|