ErrorMessage
컨트롤 아래에 두는 검증 텍스트입니다. Field 파트가 아닌 형제 요소입니다. Form 밖에서는 id가 필수입니다. 기본 role="alert"도 aria-live도 없습니다. invalid는 컨트롤이나 Field.Root에 둡니다.
이 페이지의 내용
오류는 해당될 때만 마운트하고, 그때만 그 id를 aria-describedby에 포함하세요. 마운트될 때 field.error.motion.duration.enter와 field.error.motion.easing.enter로 서서히 나타나며, 동작 줄이기에서도 그렇고, 사라질 때는 바로 사라집니다. 텍스트는 첫 프레임부터 페이지에 있으므로 페이드가 스크린 리더가 읽는 내용을 늦추지 않습니다.
ErrorMessage는 자신의 간격을 직접 관리합니다. 컨트롤 다음에서는 margin-block-start가 field.label.gap(0.4rem)입니다. Description 다음에서는 형제 규칙이 이를 field.messages.gap(0.2rem)으로 줄입니다. margin-block-end는 0입니다.
import { Description, ErrorMessage, Field, Label } from '@tounsoo/todae';
import { useId, useState } from 'react';
export default function ErrorMessageSibling() {
const id = useId();
const hintId = useId();
const errorId = useId();
const [email, setEmail] = useState('not-an-email');
const invalid = email !== '' && !email.includes('@');
const describedBy = invalid ? `${hintId} ${errorId}` : hintId;
return (
<Field.Group>
<Label.Root htmlFor={id}>Work email</Label.Root>
<Field.Root id={id} invalid={invalid} aria-describedby={describedBy}>
<Field.TextInput value={email} onChange={(event) => setEmail(event.target.value)} />
</Field.Root>
<Description.Root id={hintId}>We only use this for billing receipts.</Description.Root>
{invalid ? <ErrorMessage.Root id={errorId}>Enter an address that contains an @.</ErrorMessage.Root> : null}
</Field.Group>
);
}
제출 시점의 오류를 알리려면 이 컴포넌트에 role="alert"를 넘기세요. 패키지는 알림 정책을 정하지 않습니다.
Form 안에서
Form 안에서는 Field.Group이나 Fieldset.Root가 검증 범위이고, ErrorMessage.Root는 id를 받지 않습니다. 범위가 문제를 보여 줄 때만 렌더링되며, children을 넘기지 않으면 문제의 텍스트를 보여 줍니다. 범위는 컨트롤에 aria-invalid를 설정하고, 메시지가 렌더링되는 동안 그 id를 컨트롤의 aria-describedby에 더합니다. 함수 자식은 오류의 kind와 message를 받습니다.
const id = useId();
<Field.Group>
<Label.Root htmlFor={id}>Work email</Label.Root>
<Field.Root id={id} name="email">
<Field.TextInput type="email" required value={email} onValueChange={setEmail} />
</Field.Root>
<ErrorMessage.Root />
</Field.Group>
접근성을 지키는 조합
Todae가 맡는 것: ErrorMessage.Root는 넘긴 id(Form 안에서는 범위의 id), data-todae-error, 오류 텍스트 색상을 가진 <span>을 렌더링합니다. role과 aria-live가 없고, 컨트롤에는 아무것도 쓰지 않습니다. aria-invalid는 이 컴포넌트가 아니라 컨트롤, Field.Root, RadioGroup.Root, 바깥 Fieldset의 invalid에서 옵니다. Form 안에서는 범위가 aria-invalid와 aria-describedby도 연결합니다.
직접 제공할 것: 오류가 해당될 때만 마운트하고, 그때만 컨트롤의 aria-describedby(Field 안에서는 Field.Root)에 id를 넣고 invalid를 켜세요. 무엇이 잘못됐고 어떻게 고치는지 쓰세요. 제출할 때 나타나는 오류에는 role="alert"를 넘겨 알려지게 하세요. 그룹이라면 Fieldset.Root의 aria-describedby가 이 메시지를 가리키게 하세요.
주의할 점: invalid는 컨트롤을 유효하지 않다고 표시하지만 이유는 말하지 않으므로, 무엇이 잘못됐는지 알려 주는 것은 메시지 텍스트입니다. 입력하는 동안 바뀌는 메시지에 role="alert"를 주면 바뀔 때마다 알립니다. CSS로 숨겨도 id가 aria-describedby에 남아 있으면 메시지는 여전히 읽힙니다.
언제 쓰나요
언제: 컨트롤 아래, Description 다음에 오는 검증 텍스트. 유효하지 않을 때만 마운트하고 그 id를 aria-describedby에 더하세요. invalid는 컨트롤이나 Field.Root에 둡니다.
쓰지 않을 때: 늘 보이는 힌트 → Description. 유효할 때 보여 주기. 자동 알림을 기대할 때(기본으로 role="alert"가 없습니다. 제출 시점의 오류에는 넘기세요).
Props
| 이름 | 타입 | 설명 |
|---|---|---|
id | string | 작성자가 컨트롤이나 Field.Root의 aria-describedby에 넣는 id. 검증 범위 밖에서는 필수입니다. 범위 안에서는 생략하면 범위의 오류를 보여 줍니다. |
children | React.ReactNode| ((error: FormFieldError) => React.ReactNode) | 오류 텍스트. id가 없고 검증 범위 안이면 오류를 받는 함수도 쓸 수 있으며, children이 없으면 오류의 메시지를 보여 줍니다. |
className | string | span에 붙는 선택적 클래스. |