-
Notifications
You must be signed in to change notification settings - Fork 0
UI v0.19.2 Field접근성개선 #36
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
The head ref may contain hidden characters: "ui-v0.19.2-Field\uC811\uADFC\uC131\uAC1C\uC120"
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
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
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
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 |
|---|---|---|
| @@ -1,10 +1,17 @@ | ||
| /** | ||
| * 작성자: KYD | ||
| * 기능: 폼 입력 하나를 라벨 + 입력 + 설명/에러 메시지의 표준 구조로 묶는 컴포넌트 | ||
| * 프로세스 설명: label 요소로 감싸므로 네이티브 input 은 라벨 클릭 시 자동으로 포커스됩니다 | ||
| * 프로세스 설명: label 은 htmlFor 로 입력과 연결하고(블록 요소 children 을 label 로 감싸는 HTML 표준 위반 방지), | ||
| * 단일 입력 요소에는 id / aria-describedby / aria-invalid 를 자동 주입해 스크린 리더가 설명·에러를 읽을 수 있게 합니다 | ||
| */ | ||
|
|
||
| import { useId } from 'react' | ||
| import { useId, Children, isValidElement, cloneElement } from 'react' | ||
|
|
||
| type InjectedInputProps = { | ||
| id?: string | ||
| 'aria-describedby'?: string | ||
| 'aria-invalid'?: boolean | ||
| } | ||
|
|
||
| type FieldProps = { | ||
| /** 입력 위에 표시되는 라벨 */ | ||
|
|
@@ -15,24 +22,43 @@ type FieldProps = { | |
| error?: string | ||
| /** 필수 입력 표시(*) 여부 */ | ||
| required?: boolean | ||
| /** 라벨이 감쌀 입력 요소 */ | ||
| /** 라벨과 연결되는 입력 요소 */ | ||
| children: React.ReactNode | ||
| } & Omit<React.ComponentPropsWithoutRef<'label'>, 'children'> | ||
| } & React.ComponentPropsWithoutRef<'div'> | ||
|
|
||
| export function Field({ label, description, error, required, children, className = '', ...props }: FieldProps) { | ||
| const messageId = useId() | ||
| const uniqueId = useId() | ||
| const messageId = `${uniqueId}-message` | ||
| const hasMessage = Boolean(error || description) | ||
|
|
||
| // 단일 입력 요소면 id / aria 속성을 주입해 라벨·메시지와 연결 (복수/비요소 children 은 그대로 렌더링) | ||
| let inputId = `${uniqueId}-input` | ||
| let content = children | ||
| try { | ||
| const child = Children.only(children) | ||
| if (isValidElement<InjectedInputProps>(child)) { | ||
| inputId = child.props.id ?? inputId | ||
| content = cloneElement(child, { | ||
| id: inputId, | ||
| 'aria-describedby': hasMessage ? messageId : undefined, | ||
| 'aria-invalid': error ? true : undefined, | ||
| }) | ||
| } | ||
|
Comment on lines
+38
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 개선 제안 및 고려사항
|
||
| } catch { | ||
| // children 이 단일 요소가 아닌 경우 주입 없이 그대로 반환 | ||
| } | ||
|
|
||
| return ( | ||
| <label className={`yds-field ${className}`} {...props}> | ||
| <span className="yds-field-label"> | ||
| <div className={`yds-field ${className}`} {...props}> | ||
| <label htmlFor={inputId} className="yds-field-label"> | ||
| {label} | ||
| {required && ( | ||
| <span className="yds-field-required" aria-hidden="true"> | ||
| * | ||
| </span> | ||
| )} | ||
| </span> | ||
| {children} | ||
| </label> | ||
| {content} | ||
| {error ? ( | ||
| <span id={messageId} className="yds-field-error" role="alert"> | ||
| {error} | ||
|
|
@@ -44,7 +70,7 @@ export function Field({ label, description, error, required, children, className | |
| </span> | ||
| ) | ||
| )} | ||
| </label> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
|
|
||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
React.Fragment가 children으로 전달될 때의 런타임 경고를 방지하기 위해
Fragment타입을 체크할 수 있도록Fragment를 import 목록에 추가합니다.