Skip to content

Commit 135be93

Browse files
committed
[#45] ✨ Implement CheckboxInput component
1 parent d71ee51 commit 135be93

File tree

1 file changed

+46
-5
lines changed

1 file changed

+46
-5
lines changed
Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,75 @@
1+
import {
2+
IcCheckOff,
3+
IcCheckOn,
4+
IcCheckboxOff,
5+
IcCheckboxOn,
6+
} from '@/assets/IconList'
17
import clsx from 'clsx'
8+
import { twMerge } from 'tailwind-merge'
9+
10+
import { handleKeyDown } from '@/utils/handleKeyDown'
11+
import { toggleCheckbox } from '@/utils/toggleCheckbox'
212

313
interface CheckboxInputProps
414
extends React.InputHTMLAttributes<HTMLInputElement> {
515
label: string
16+
variant: 'checkbox' | 'check'
617
}
718

819
export const CheckboxInput = ({
920
label,
21+
variant = 'checkbox',
1022
className = '',
11-
checked,
23+
checked = false,
1224
disabled,
1325
onChange,
1426
...props
1527
}: 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+
)
1646
return (
17-
<label>
47+
<label className={labelClass}>
1848
<input
1949
type='checkbox'
2050
checked={checked}
2151
disabled={disabled}
2252
{...props}
53+
className='hidden'
2354
/>
2455
<span
2556
role='checkbox'
2657
tabIndex={0}
2758
aria-checked={checked}
2859
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>
3273
</label>
3374
)
3475
}

0 commit comments

Comments
 (0)