|
| 1 | +import { |
| 2 | + IcCheckOff, |
| 3 | + IcCheckOn, |
| 4 | + IcCheckboxOff, |
| 5 | + IcCheckboxOn, |
| 6 | +} from '@/assets/IconList' |
1 | 7 | import clsx from 'clsx' |
| 8 | +import { twMerge } from 'tailwind-merge' |
| 9 | + |
| 10 | +import { handleKeyDown } from '@/utils/handleKeyDown' |
| 11 | +import { toggleCheckbox } from '@/utils/toggleCheckbox' |
2 | 12 |
|
3 | 13 | interface CheckboxInputProps |
4 | 14 | extends React.InputHTMLAttributes<HTMLInputElement> { |
5 | 15 | label: string |
| 16 | + variant: 'checkbox' | 'check' |
6 | 17 | } |
7 | 18 |
|
8 | 19 | export const CheckboxInput = ({ |
9 | 20 | label, |
| 21 | + variant = 'checkbox', |
10 | 22 | className = '', |
11 | | - checked, |
| 23 | + checked = false, |
12 | 24 | disabled, |
13 | 25 | onChange, |
14 | 26 | ...props |
15 | 27 | }: CheckboxInputProps): JSX.Element => { |
| 28 | + const toggleIconState = () => { |
| 29 | + if (variant === 'checkbox') { |
| 30 | + return checked ? ( |
| 31 | + <IcCheckboxOn width={24} height={24} alt='체크된 체크박스' /> |
| 32 | + ) : ( |
| 33 | + <IcCheckboxOff width={24} height={24} alt='체크 안 된 체크박스' /> |
| 34 | + ) |
| 35 | + } |
| 36 | + return checked ? ( |
| 37 | + <IcCheckOn width={24} height={24} alt='체크된 체크' /> |
| 38 | + ) : ( |
| 39 | + <IcCheckOff width={24} height={24} alt='체크 안 된 체크' /> |
| 40 | + ) |
| 41 | + } |
| 42 | + const labelClass = clsx( |
| 43 | + 'flex items-center', |
| 44 | + disabled && 'cursor-not-allowed opacity-50' |
| 45 | + ) |
16 | 46 | return ( |
17 | | - <label> |
| 47 | + <label className={labelClass}> |
18 | 48 | <input |
19 | 49 | type='checkbox' |
20 | 50 | checked={checked} |
21 | 51 | disabled={disabled} |
22 | 52 | {...props} |
| 53 | + className='hidden' |
23 | 54 | /> |
24 | 55 | <span |
25 | 56 | role='checkbox' |
26 | 57 | tabIndex={0} |
27 | 58 | aria-checked={checked} |
28 | 59 | aria-label={'checkbox button'} |
29 | | - className='custom-checkbox' |
30 | | - /> |
31 | | - {label} |
| 60 | + className='cursor-pointer' |
| 61 | + onKeyDown={e => |
| 62 | + handleKeyDown( |
| 63 | + e, |
| 64 | + () => toggleCheckbox(checked, onChange, props.value), |
| 65 | + disabled |
| 66 | + ) |
| 67 | + } |
| 68 | + onClick={() => toggleCheckbox(checked, onChange, props.value)} |
| 69 | + > |
| 70 | + {toggleIconState()} |
| 71 | + </span> |
| 72 | + <span className={twMerge('ml-10 h-22', className)}>{label}</span> |
32 | 73 | </label> |
33 | 74 | ) |
34 | 75 | } |
0 commit comments