Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/components/common/input/RadioInput.tsx
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://juhyejin.tistory.com/54

원래 input 의 display 를 none 으로 하면서, 접근성 면에서 손해를 보는 현상이 있는거 같아요. 해당 아티클 참고하면 좋을거 같습니다 !

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import clsx from 'clsx'

import { handleKeyDown } from '@/utils/handleKeyDown'

interface RadioInputProps extends React.InputHTMLAttributes<HTMLInputElement> {
label: string
}

export const RadioInput = ({
label,
className = '',
checked,
disabled,
onChange,
...props
}: RadioInputProps): JSX.Element => {
const labelClass = clsx(
'flex cursor-pointer items-center',
disabled && 'cursor-not-allowed opacity-50',
className
)

return (
<label className={labelClass}>
<input type='radio' checked={checked} disabled={disabled} {...props} />
<span
role='radio'
tabIndex={0}
aria-checked={checked}
aria-label={'radio button'}
onKeyDown={e =>
handleKeyDown(
e,
() =>
onChange?.({
target: { checked: true, value: props.value },
} as any),
disabled
)
}
className='custom-radio'
/>
<span className='text-body-2 ml-4 h-22 font-normal text-gray-800'>
{label}
</span>
</label>
)
}
21 changes: 21 additions & 0 deletions src/stories/common/input/RadioInput.stories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { fn } from '@storybook/test'

import { RadioInput } from '@/components/common/input/RadioInput'

export default {
title: 'Common/Input/RadioInput',
component: RadioInput,
parameters: {
layout: 'centered',
},
tags: ['common', 'radio'],
args: {
onChange: fn(),
},
}

export const Default = {
args: {
label: '회사 ‧ 학교',
},
}
22 changes: 22 additions & 0 deletions src/styles/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,25 @@ select {
outline: 0;
cursor: pointer;
}

input[type='radio'] {
display: none;
}
.custom-radio {
@apply relative inline-block h-20 w-20 cursor-pointer rounded-full border-[1.4px] border-solid border-gray-300;
}
.custom-radio::before {
content: '';
@apply absolute left-1/2 top-1/2 block h-12 w-12 -translate-x-1/2 -translate-y-1/2 rounded-full;
}
input[type='radio']:checked + .custom-radio {
@apply border-primary-normal;
}

input[type='radio']:checked + .custom-radio::before {
@apply bg-primary-normal;
}

label:focus-within .custom-radio {
@apply ring-1 ring-primary-normal;
}
9 changes: 9 additions & 0 deletions src/utils/handleKeyDown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const handleKeyDown = (
e: React.KeyboardEvent,
callback: () => void,
disabled?: boolean
): void => {
if (e.key === 'Enter' && !disabled) {
callback()
}
}