useField
useField()는 내장 파트가 가장 가까운 Field.Root에 합류하는 방식 그대로, 직접 만든 컨트롤을 합류시킵니다. 컨트롤에 getControlProps()를 펼치세요. 여기에는 Root의 id, name, disabled, aria-invalid, 병합된 aria-describedby, 컨트롤이 스스로 이름을 붙이지 않을 때의 Root 이름, 그리고 셸을 누르면 컨트롤에 포커스가 가도록 하는 ref가 담깁니다. 셸은 이 컨트롤의 포커스 링도 그립니다. 네이티브 폼 요소가 아닌 컨트롤에는 getControlProps(props, { native: false })를 호출하세요. disabled 대신 aria-disabled를 설정하고, 폼 요소만 제출하는 name은 빼 줍니다. 값을 값 입력(value input, 아래)이 대신 보내는 컨트롤에는 { sendsName: false }를 주세요. 같은 방식으로 name을 빼고 필드의 이름은 값 입력에 맡깁니다. 탭 정지를 건너뛰고 키를 무시하려면 useField()에서 Root의 disabled를 읽으세요. 직접 넘긴 disabled나 aria-invalid는 Root의 값에 더해질 뿐 그 값을 지우지 않으므로, 같은 검사에서 자신의 값과 함께 조합하세요. Label.Root htmlFor는 레이블을 붙일 수 있는 요소에만 이름을 주므로, 커스텀 요소는 Root의 aria-labelledby로 이름을 붙이세요.
이 페이지의 내용
import { Description, Field, Text, useField } from '@tounsoo/todae';
import { useId, useState } from 'react';
// A company control that is not a native input, joined to Field.Root with useField.
function Stars({ value, onChange }: { value: number; onChange: (value: number) => void }) {
const field = useField();
const { disabled } = field;
const step = (delta: number) => onChange(Math.min(5, Math.max(0, value + delta)));
return (
<span
{...field.getControlProps({}, { native: false })}
role="slider"
tabIndex={disabled ? -1 : 0}
aria-valuemin={0}
aria-valuemax={5}
aria-valuenow={value}
aria-valuetext={`${value} of 5 stars`}
// Field.Root's frame draws the focus ring, so the control draws none.
style={{ letterSpacing: '0.1em', outline: 'none' }}
onKeyDown={(event) => {
if (disabled) return;
if (event.key === 'ArrowRight' || event.key === 'ArrowUp') step(1);
else if (event.key === 'ArrowLeft' || event.key === 'ArrowDown') step(-1);
else return;
event.preventDefault();
}}
>
{'★'.repeat(value)}
{'☆'.repeat(5 - value)}
</span>
);
}
// A span is not labelable, so the caption names it through aria-labelledby.
export default function FieldUseField() {
const captionId = useId();
const hintId = useId();
const [stars, setStars] = useState(3);
return (
<Field.Group>
<Text.Root as="span" id={captionId} weight="medium">
Rating
</Text.Root>
<Field.Root aria-labelledby={captionId} aria-describedby={hintId}>
<Stars value={stars} onChange={setStars} />
</Field.Root>
<Description.Root id={hintId}>Arrow keys change the rating.</Description.Root>
</Field.Group>
);
}
값 입력
네이티브 폼 요소가 아닌 컨트롤은 폼과 함께 아무것도 보내지 않고 검증도 받지 않습니다. getValueInputProps는 그 값을 담는 네이티브 <input>의 props를 주므로, 브라우저가 required, min, max를 검사하고 Form.Root가 문제를 보여 주며 컨트롤로 포커스를 옮깁니다.
function Stars({ value, onChange, required }: StarsProps) {
const field = useField();
const control = field.getControlProps({}, { native: false });
return (
<>
<span {...control} role="slider" tabIndex={0} aria-valuenow={value} />
<input
{...field.getValueInputProps({ type: 'number', value: value ? String(value) : '', required, min: '1', max: '5' }, control)}
/>
</>
);
}
type은 text, number, date, time, datetime-local 중 하나이고, value는 그 타입의 형식입니다. date라면 2026-10-07처럼 쓰고, 없으면 ''입니다. step은 그 타입 고유의 값입니다. bound는 필드가 보여 주는 { min, max }, 예를 들어 "Oct 7, 2026"으로, 경계를 이름으로 부르는 메시지에 쓰입니다. 날짜, 시각, 날짜와 시각은 "Enter Oct 7, 2026 or later"처럼 읽히고, bound가 없으면 메시지는 원래 값을 그대로 부릅니다. 이 입력은 getControlProps가 돌려준 props를 따르므로 그것을 두 번째 인자로 넘기세요. 타입이 이를 요구합니다. 필드와 함께, 또는 disabled나 aria-disabled인 컨트롤과 함께 비활성화되고, 컨트롤의 form에 속하며, 필드의 name으로, 또는 name 옵션을 주었다면 그 이름으로 값을 보냅니다. 네이티브 요소가 아닌 컨트롤이 자기 이름을 가지려면 이 옵션이 필요합니다. 컨트롤이 이름을 가졌거나 필드의 다른 컨트롤이 필드의 name을 가졌으면 name 없이 렌더링됩니다. 커스텀 컨트롤은 역할이 허용하지 않으면 aria-required를 쓸 수 없으므로, 직접 설정하거나 필수라고 설명해 주세요. 브라우저가 검증에서 제외하는 읽기 전용이나 type="hidden"이 아닙니다. 화면과 보조 기술에서 숨겨지고 탭 정지점이 아니며, 필드 셸을 눌러도 포커스를 받지 않습니다. 같은 Field.Root 안의 컨트롤 옆에 렌더링하세요. 이 입력의 문제는 Form.Root에서도 Form.Summary 링크에서도, Form.Root 밖의 브라우저 자체 검사에서도 getControlProps로 연결한 컨트롤로 포커스를 옮기고, 그런 컨트롤이 없으면 포커스를 갖지 않습니다.
이 입력은 제어 컴포넌트이므로 코드가 설정한 값은 입력을 바꾸되 아무것도 일으키지 않습니다. change 이벤트도, 수정도 없습니다. 사람이 값을 고르거나 칩을 지우는 등 값을 바꾸는 핸들러에서 notifyValueChange()를 호출하면, Form.Root가 필드의 name에 대한 서버 오류를 숨기고 응답 대기를 끝내며, 바깥의 Field.Group은 텍스트 입력에 타이핑한 것처럼 필드를 수정된 것으로 셉니다. 네이티브 요소인 컨트롤은 자체 change 이벤트가 같은 일을 하므로 호출이 필요 없습니다.
접근성을 지키는 조합
Todae가 맡는 것: getControlProps()는 컨트롤에 Root의 id, 직접 준 값과 합친 Root의 aria-describedby, Root가 invalid인 동안의 aria-invalid, 컨트롤이 스스로 이름을 갖지 않으면 Root의 aria-label이나 aria-labelledby, 그리고 disabled({ native: false }이면 aria-disabled)를 줍니다. 함께 주는 ref 덕분에 셸을 누르면 컨트롤에 포커스가 가고, 컨트롤이나 그 안의 요소가 focus-visible인 동안 셸이 포커스 링을 그립니다. Root의 disabled와 invalid에는 바깥 Fieldset의 값이 포함됩니다.
직접 제공할 것: 컨트롤이 무엇이고 무엇을 하는지. 훅은 role을 설정하지 않으므로 role, 그리고 tabIndex, 키보드 처리, slider라면 aria-valuenow, aria-valuemin, aria-valuemax 같은 role의 상태를 주세요. 이름은 Field.Root의 aria-labelledby나 aria-label로 주세요. field.disabled가 true인 동안에는 탭 정지를 빼고 키를 무시하세요.
주의할 점: getControlProps() 뒤에 펼친 aria-describedby, name, ref는 필드가 모르는 채로 연결된 값을 대체합니다. 대신 getControlProps(props)로 넘기세요. { native: false }가 설정하는 aria-disabled는 포커스나 키를 막지 않습니다. getControlProps()를 부르는 컨트롤은 모두 같은 id를 받으므로, Field.Root 하나에 컨트롤 하나만 쓰세요.
언제 쓰나요
언제: 별점처럼 직접 만든 컨트롤이 Field.Root 안에 있고, 그 id, 이름, 설명, invalid 상태가 필요할 때.
쓰지 않을 때: 맞는 내장 파트가 이미 있을 때 → 그 파트를 쓰세요. 어떤 Field.Root에도 속하지 않는 컨트롤 → 자체 id를 연결하세요.
Props
| 이름 | 타입 | 설명 |
|---|