mango-v4-ui/components/forms/Switch.tsx

51 lines
1.3 KiB
TypeScript
Raw Normal View History

2022-07-22 08:40:53 -07:00
import { FunctionComponent, ReactNode, memo } from 'react'
2022-07-17 04:48:33 -07:00
interface SwitchProps {
checked: boolean
className?: string
onChange: (x: boolean) => void
children: ReactNode
2022-07-25 20:59:46 -07:00
disabled?: boolean
2022-07-17 04:48:33 -07:00
}
const Switch: FunctionComponent<SwitchProps> = ({
checked = false,
className = '',
children,
onChange,
2022-07-25 20:59:46 -07:00
disabled,
2022-07-17 04:48:33 -07:00
}) => {
const handleClick = () => {
onChange(!checked)
}
return (
<div className={`flex items-center ${className}`}>
<span className="mr-2">{children}</span>
<button
type="button"
className={`${
checked ? 'bg-th-primary' : 'bg-th-bkg-button'
} relative inline-flex h-6 w-11 flex-shrink-0 cursor-pointer rounded-full
border-2 border-transparent transition-colors duration-200 ease-in-out
2022-07-25 20:59:46 -07:00
focus:outline-none ${disabled ? 'opacity-60' : ''}`}
2022-07-17 04:48:33 -07:00
role="switch"
aria-checked={checked}
onClick={handleClick}
2022-07-25 20:59:46 -07:00
disabled={disabled}
2022-07-17 04:48:33 -07:00
>
<span className="sr-only">{children}</span>
<span
aria-hidden="true"
className={`${
checked ? 'translate-x-5' : 'translate-x-0'
} pointer-events-none inline-block h-5 w-5 transform rounded-full
bg-white shadow ring-0 transition duration-200 ease-in-out`}
></span>
</button>
</div>
)
}
2022-07-22 08:40:53 -07:00
export default memo(Switch)