-
Notifications
You must be signed in to change notification settings - Fork 3
체크박스 컴포넌트 #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
체크박스 컴포넌트 #10
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7cacef0
feat: 체크박스 컴포넌트 추가
Jieunsse c31fd23
chore: 체크박스 아이콘 파일명 오타 수정
Jieunsse f625fc2
chore: constants.ts -> styleConstants.ts로 수정해서 역할 명시
Jieunsse acd5087
style: 체크박스 사이즈 동적으로 처리하도록 변경
Jieunsse 3acae7a
refactor: 체크박스 동적으로 처리 가능하도록 타입 개선
Jieunsse bdde855
refactor: use client 적용, jsDoc 설명 개선, 체크박스 타입 변경
Jieunsse 4bed37c
refactor: 제미나이 코드리뷰 반영
Jieunsse 952a494
fix: vercel 배포간 타입체크 오류 해결을 위한 설정 추가
Jieunsse b9ac972
fix: vercel 배포 이슈 해결을 위한 타입 추가
Jieunsse 1dd4c2d
refactor: 라벨 관련 로직 런타임에러 방지
Jieunsse File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes
File renamed without changes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| 'use client'; | ||
|
|
||
| import clsx from 'clsx'; | ||
| import Image from 'next/image'; | ||
| import type { ChangeEvent, CSSProperties, KeyboardEvent, MouseEvent } from 'react'; | ||
|
|
||
| import styles from './styles/CheckBox.module.css'; | ||
| import { CHECKBOX_STYLE } from './constants/styleConstants'; | ||
| import type { CheckBoxProps } from './types/types'; | ||
|
|
||
| /** | ||
| * 체크박스 컴포넌트. | ||
| * @param checked 체크 여부 | ||
| * @param onCheckedChange 체크 상태 변경 콜백 | ||
| * @param size 체크박스 크기('large' | 'small') | ||
| * @param label 접근성 용도의 라벨(없으면 options.ariaLabel 필수) | ||
| * @param options 고급 옵션(ariaLabel/readOnly/icons) | ||
| */ | ||
| export default function CheckBox({ | ||
| checked, | ||
| size = 'large', | ||
| label, | ||
| id, | ||
| name, | ||
| value, | ||
| disabled = false, | ||
| className, | ||
| options, | ||
| onCheckedChange, | ||
| }: CheckBoxProps) { | ||
| const hasLabel = label != null && (typeof label === 'string' ? label.trim() !== '' : true); | ||
| const isReadOnly = options?.readOnly || !onCheckedChange; | ||
| const isDisabled = disabled; | ||
| const iconSrc = checked | ||
| ? CHECKBOX_STYLE.icons.checked[size] | ||
| : CHECKBOX_STYLE.icons.unchecked[size]; | ||
| const boxSize = CHECKBOX_STYLE.boxSize[size]; | ||
| const inputAriaLabel = hasLabel ? undefined : options?.ariaLabel; | ||
| const checkboxStyle = { | ||
| '--checkbox-box-size': `${boxSize}px`, | ||
| } as CSSProperties; | ||
| const iconNode = options?.icons ? ( | ||
| checked ? ( | ||
| options.icons.checked | ||
| ) : ( | ||
| options.icons.unchecked | ||
| ) | ||
| ) : ( | ||
| <Image className={styles.icon} src={iconSrc} alt="" width={boxSize} height={boxSize} /> | ||
| ); | ||
|
|
||
| const handleClick = (event: MouseEvent<HTMLInputElement>) => { | ||
| if (isReadOnly) { | ||
| event.preventDefault(); | ||
| } | ||
| }; | ||
|
|
||
| const handleKeyDown = (event: KeyboardEvent<HTMLInputElement>) => { | ||
| if (isReadOnly && (event.key === ' ' || event.key === 'Enter')) { | ||
| event.preventDefault(); | ||
| } | ||
| }; | ||
|
|
||
| const handleChange = (event: ChangeEvent<HTMLInputElement>) => { | ||
| if (isReadOnly || isDisabled) { | ||
| event.preventDefault(); | ||
| return; | ||
| } | ||
| onCheckedChange?.(event.target.checked); | ||
| }; | ||
|
|
||
| if (process.env.NODE_ENV !== 'production' && !hasLabel && !options?.ariaLabel) { | ||
| console.warn('CheckBox: label이 비어있다면 ariaLabel이 필요합니다.'); | ||
| } | ||
|
|
||
| return ( | ||
| <label | ||
| className={clsx(styles.checkbox, styles[size], isDisabled && styles.disabled, className)} | ||
| style={checkboxStyle} | ||
| > | ||
| <input | ||
| className={styles.input} | ||
| type="checkbox" | ||
| checked={checked} | ||
| aria-label={inputAriaLabel} | ||
| aria-disabled={isReadOnly || isDisabled ? true : undefined} | ||
| id={id} | ||
| name={name} | ||
| value={value} | ||
| disabled={isDisabled} | ||
| onClick={handleClick} | ||
| onKeyDown={handleKeyDown} | ||
| onChange={handleChange} | ||
| /> | ||
| <span className={styles.box} aria-hidden="true"> | ||
| {iconNode} | ||
| </span> | ||
| {hasLabel ? <span className={styles.label}>{label}</span> : null} | ||
| </label> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import checkedLarge from '@/assets/icons/check/checkedLarge.svg'; | ||
| import checkedSmall from '@/assets/icons/check/checkedSmall.svg'; | ||
| import nonCheckedLarge from '@/assets/icons/check/nonCheckedLarge.svg'; | ||
| import nonCheckedSmall from '@/assets/icons/check/nonCheckedSmall.svg'; | ||
|
|
||
| export const CHECKBOX_STYLE = { | ||
| icons: { | ||
| checked: { | ||
| large: checkedLarge, | ||
| small: checkedSmall, | ||
| }, | ||
| unchecked: { | ||
| large: nonCheckedLarge, | ||
| small: nonCheckedSmall, | ||
| }, | ||
| }, | ||
| boxSize: { | ||
| large: 18, | ||
| small: 16, | ||
| }, | ||
| } as const; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| .checkbox { | ||
| display: inline-flex; | ||
| align-items: center; | ||
| gap: 8px; | ||
| cursor: pointer; | ||
| } | ||
|
|
||
| .large { | ||
| font-size: 16px; | ||
| } | ||
|
|
||
| .small { | ||
| font-size: 14px; | ||
| } | ||
|
|
||
| .input { | ||
| position: absolute; | ||
| opacity: 0; | ||
| width: 1px; | ||
| height: 1px; | ||
| pointer-events: none; | ||
| } | ||
|
|
||
| .box { | ||
| display: inline-flex; | ||
| align-items: center; | ||
| justify-content: center; | ||
| width: var(--checkbox-box-size, 18px); | ||
| height: var(--checkbox-box-size, 18px); | ||
| flex-shrink: 0; | ||
| } | ||
|
|
||
| .icon { | ||
| width: 100%; | ||
| height: 100%; | ||
| display: block; | ||
| } | ||
|
|
||
| .label { | ||
| line-height: 1.4; | ||
| } | ||
|
|
||
| .disabled { | ||
| cursor: not-allowed; | ||
| opacity: 0.5; | ||
| } | ||
|
|
||
| @media (max-width: 480px) { | ||
| .checkbox { | ||
| gap: 6px; | ||
| } | ||
|
|
||
| .large { | ||
| font-size: 15px; | ||
| } | ||
|
|
||
| .small { | ||
| font-size: 13px; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import type { ReactNode } from 'react'; | ||
|
|
||
| export type CheckBoxSize = 'large' | 'small'; | ||
|
|
||
| export type CheckBoxIconSet = { | ||
| checked: ReactNode; | ||
| unchecked: ReactNode; | ||
| }; | ||
|
|
||
| export type CheckBoxOptions = { | ||
| ariaLabel?: string; | ||
| readOnly?: boolean; | ||
| icons?: CheckBoxIconSet; | ||
| }; | ||
|
|
||
| export type CheckBoxOptionsWithAriaLabel = Omit<CheckBoxOptions, 'ariaLabel'> & { | ||
| ariaLabel: string; | ||
| }; | ||
|
|
||
| interface CheckBoxBaseProps { | ||
| checked: boolean; | ||
| size?: CheckBoxSize; | ||
| id?: string; | ||
| name?: string; | ||
| value?: string; | ||
| disabled?: boolean; | ||
| className?: string; | ||
| onCheckedChange?: (checked: boolean) => void; | ||
| } | ||
|
|
||
| export type CheckBoxProps = | ||
| | (CheckBoxBaseProps & { | ||
| label: ReactNode; | ||
| options?: CheckBoxOptions; | ||
| }) | ||
| | (CheckBoxBaseProps & { | ||
| label?: undefined; | ||
| options: CheckBoxOptionsWithAriaLabel; | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| declare module '*.svg' { | ||
| import type { StaticImageData } from 'next/image'; | ||
|
|
||
| const src: StaticImageData; | ||
| export default src; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,7 @@ | |
| }, | ||
| "include": [ | ||
| "next-env.d.ts", | ||
| "**/*.d.ts", | ||
| "**/*.ts", | ||
| "**/*.tsx", | ||
| ".next/types/**/*.ts", | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.