2023-05-15 08:51:32 +08:00
|
|
|
'use client'
|
2023-06-06 14:58:56 +08:00
|
|
|
import React, { useEffect, useState } from 'react'
|
2023-05-15 08:51:32 +08:00
|
|
|
import { Switch as OriginalSwitch } from '@headlessui/react'
|
2024-07-09 15:05:40 +08:00
|
|
|
import classNames from '@/utils/classnames'
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
type SwitchProps = {
|
2024-06-05 00:13:29 +08:00
|
|
|
onChange?: (value: boolean) => void
|
2024-05-10 18:14:05 +08:00
|
|
|
size?: 'sm' | 'md' | 'lg' | 'l'
|
2023-05-15 08:51:32 +08:00
|
|
|
defaultValue?: boolean
|
|
|
|
disabled?: boolean
|
2024-06-05 00:13:29 +08:00
|
|
|
className?: string
|
2023-05-15 08:51:32 +08:00
|
|
|
}
|
|
|
|
|
2024-10-21 10:32:37 +08:00
|
|
|
const Switch = ({ onChange, size = 'md', defaultValue = false, disabled = false, className }: SwitchProps) => {
|
2023-05-15 08:51:32 +08:00
|
|
|
const [enabled, setEnabled] = useState(defaultValue)
|
2023-06-06 14:58:56 +08:00
|
|
|
useEffect(() => {
|
|
|
|
setEnabled(defaultValue)
|
|
|
|
}, [defaultValue])
|
2023-05-15 08:51:32 +08:00
|
|
|
const wrapStyle = {
|
|
|
|
lg: 'h-6 w-11',
|
2023-08-12 00:57:13 +08:00
|
|
|
l: 'h-5 w-9',
|
2023-06-06 14:58:56 +08:00
|
|
|
md: 'h-4 w-7',
|
2024-05-10 18:14:05 +08:00
|
|
|
sm: 'h-3 w-5',
|
2023-05-15 08:51:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const circleStyle = {
|
|
|
|
lg: 'h-5 w-5',
|
2023-08-12 00:57:13 +08:00
|
|
|
l: 'h-4 w-4',
|
2023-06-06 14:58:56 +08:00
|
|
|
md: 'h-3 w-3',
|
2024-05-10 18:14:05 +08:00
|
|
|
sm: 'h-2 w-2',
|
2023-05-15 08:51:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const translateLeft = {
|
|
|
|
lg: 'translate-x-5',
|
2023-08-12 00:57:13 +08:00
|
|
|
l: 'translate-x-4',
|
2023-06-06 14:58:56 +08:00
|
|
|
md: 'translate-x-3',
|
2024-05-10 18:14:05 +08:00
|
|
|
sm: 'translate-x-2',
|
2023-05-15 08:51:32 +08:00
|
|
|
}
|
|
|
|
return (
|
|
|
|
<OriginalSwitch
|
|
|
|
checked={enabled}
|
|
|
|
onChange={(checked: boolean) => {
|
2023-06-06 14:58:56 +08:00
|
|
|
if (disabled)
|
|
|
|
return
|
2023-05-15 08:51:32 +08:00
|
|
|
setEnabled(checked)
|
2024-06-05 00:13:29 +08:00
|
|
|
onChange?.(checked)
|
2023-05-15 08:51:32 +08:00
|
|
|
}}
|
|
|
|
className={classNames(
|
|
|
|
wrapStyle[size],
|
2024-07-19 16:39:49 +08:00
|
|
|
enabled ? 'bg-components-toggle-bg' : 'bg-components-toggle-bg-unchecked',
|
|
|
|
'relative inline-flex flex-shrink-0 cursor-pointer rounded-[5px] border-2 border-transparent transition-colors duration-200 ease-in-out',
|
2023-05-15 08:51:32 +08:00
|
|
|
disabled ? '!opacity-50 !cursor-not-allowed' : '',
|
2024-06-05 00:13:29 +08:00
|
|
|
className,
|
2023-05-15 08:51:32 +08:00
|
|
|
)}
|
|
|
|
>
|
|
|
|
<span
|
|
|
|
aria-hidden="true"
|
|
|
|
className={classNames(
|
|
|
|
circleStyle[size],
|
|
|
|
enabled ? translateLeft[size] : 'translate-x-0',
|
2024-07-19 16:39:49 +08:00
|
|
|
'pointer-events-none inline-block transform rounded-[3px] bg-components-toggle-knob shadow ring-0 transition duration-200 ease-in-out',
|
2023-05-15 08:51:32 +08:00
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</OriginalSwitch>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
export default React.memo(Switch)
|