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

66 lines
1.6 KiB
TypeScript
Raw Normal View History

2022-07-21 22:09:04 -07:00
import React, { ChangeEvent, ReactNode } from 'react'
2022-09-06 21:36:35 -07:00
import { CheckIcon } from '@heroicons/react/20/solid'
2022-07-21 22:09:04 -07:00
interface CheckboxProps {
checked: boolean
children: ReactNode
onChange: (x: ChangeEvent<HTMLInputElement>) => void
disabled?: boolean
halfState?: boolean
2022-09-29 20:22:55 -07:00
labelClass?: string
2022-07-21 22:09:04 -07:00
}
const Checkbox = ({
checked,
children,
disabled = false,
halfState = false,
2022-09-29 20:22:55 -07:00
labelClass,
2022-07-21 22:09:04 -07:00
...props
}: CheckboxProps) => (
2023-04-19 18:12:45 -07:00
<label className="flex cursor-pointer items-center text-th-fgd-3 hover:text-th-fgd-2">
2022-07-21 22:09:04 -07:00
<input
checked={checked}
{...props}
disabled={disabled}
type="checkbox"
style={{
border: '0',
clip: 'rect(0 0 0 0)',
clipPath: 'inset(50%)',
height: '1px',
margin: '-1px',
overflow: 'hidden',
padding: '0',
position: 'absolute',
whiteSpace: 'nowrap',
width: '1px',
}}
/>
<div
className={`${
2023-01-04 01:20:53 -08:00
checked && !disabled && !halfState ? 'bg-th-active' : 'bg-th-bkg-4'
2023-04-19 18:12:45 -07:00
} flex h-4 w-4 flex-shrink-0 cursor-pointer items-center justify-center rounded`}
2022-07-21 22:09:04 -07:00
>
{halfState ? (
2023-01-04 01:20:53 -08:00
<div className="mb-0.5 font-bold text-th-bkg-1"></div>
2022-07-21 22:09:04 -07:00
) : (
<CheckIcon
className={`${checked ? 'block' : 'hidden'} h-4 w-4 ${
2023-01-04 01:20:53 -08:00
disabled ? 'text-th-fgd-4' : 'text-th-bkg-1'
2022-07-21 22:09:04 -07:00
}`}
/>
)}
</div>
<span
2022-09-29 20:22:55 -07:00
className={`ml-2 whitespace-nowrap text-xs ${labelClass} ${
2022-07-21 22:09:04 -07:00
checked && !disabled ? 'text-th-fgd-2' : ''
}`}
>
{children}
</span>
</label>
)
export default Checkbox