Skip to content

Commit 529351b

Browse files
committed
[#45] ♻️ Refactor functions and improve accessibility
1 parent ef4e1f5 commit 529351b

File tree

1 file changed

+33
-22
lines changed

1 file changed

+33
-22
lines changed

src/components/common/input/CheckboxInput.tsx

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,41 @@ export const CheckboxInput = ({
2525
onChange,
2626
...props
2727
}: 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-
}
28+
const getCheckboxIcon = (checked: boolean) => {
29+
return checked ? (
30+
<IcCheckboxOn width={24} height={24} alt='체크된 체크박스' />
31+
) : (
32+
<IcCheckboxOff width={24} height={24} alt='체크 안 된 체크박스' />
33+
)
34+
}
35+
36+
const getCheckIcon = (checked: boolean) => {
3637
return checked ? (
3738
<IcCheckOn width={24} height={24} alt='체크된 체크' />
3839
) : (
3940
<IcCheckOff width={24} height={24} alt='체크 안 된 체크' />
4041
)
4142
}
42-
const labelClass = clsx(
43-
'flex items-center',
43+
44+
const getIconForState = (variant: string, checked: boolean) => {
45+
if (variant === 'checkbox') {
46+
return getCheckboxIcon(checked)
47+
}
48+
return getCheckIcon(checked)
49+
}
50+
51+
const handleToggle = () => {
52+
if (!disabled) {
53+
toggleCheckbox(checked, onChange, props.value)
54+
}
55+
}
56+
57+
const labelClass = clsx('flex items-center', disabled && 'opacity-50')
58+
const buttonClass = twMerge(
59+
'focus:outline-none focus:ring-1 focus:ring-primary-normal',
4460
disabled && 'cursor-not-allowed opacity-50'
4561
)
62+
4663
return (
4764
<label className={labelClass}>
4865
<input
@@ -52,23 +69,17 @@ export const CheckboxInput = ({
5269
{...props}
5370
className='hidden'
5471
/>
55-
<span
72+
<button
5673
role='checkbox'
5774
tabIndex={0}
5875
aria-checked={checked}
5976
aria-label={'checkbox button'}
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)}
77+
onKeyDown={e => handleKeyDown(e, handleToggle, disabled)}
78+
onClick={handleToggle}
79+
className={buttonClass}
6980
>
70-
{toggleIconState()}
71-
</span>
81+
{getIconForState(variant, checked)}
82+
</button>
7283
<span className={twMerge('ml-10 h-22', className)}>{label}</span>
7384
</label>
7485
)

0 commit comments

Comments
 (0)