FormField

단일 폼 컨트롤을 위한 Label + HelperText + ErrorText 구성 래퍼입니다.

개요

FormField는 단일 폼 컨트롤 주위에 Label + HelperText + ErrorText를 구성하고, 플랫폼이 허용하는 곳에서 aria-describedby/aria-invalid를 자동으로 연결해요. 다가오는 M12 폼 패턴 마일스톤의 기반이에요 — 컨트롤에 라벨, 도움말 메시지, 또는 오류가 필요할 때마다 화면마다 id/ARIA 연결을 직접 만드는 대신 이걸 사용하세요.

React에서는 래핑된 Input/Textarea에 label/error를 직접 전달하지 마세요 — FormField가 그것들의 렌더링을 담당해요; 래핑된 컨트롤은 context에서 id와 ARIA 속성을 읽어요.

구조

손그림 박스는 없어요 — FormField는 순수 레이아웃과 타이포그래피예요. 다음 순서로 렌더해요: 선택적 라벨, 래핑된 컨트롤(React/React Native에서는 children, Web Components에서는 <slot>), 선택적 도움말 텍스트 줄, 선택적 오류 줄. helperText와 error가 모두 제공되면 둘 다 동시에 렌더돼요 — 오류 시 숨김 전환이 없어요.

변형 및 상태

State Status Notes
라벨만 Implemented —
도움말 텍스트 Implemented —
오류 Implemented role="alert"로 렌더돼요.
도움말 텍스트 + 오류 함께 Implemented 둘 다 동시에 렌더 — 오류 시 숨김 전환 없음.
라벨/도움말/오류 없음 Implemented 래핑된 컨트롤만 렌더 — no-op 장식.

사용법

Do

  • 라벨, 도움말 텍스트, 또는 오류가 필요한 단일 Input/Textarea를 감쌀 때 FormField를 사용하세요.
  • React Native에서 Input에 라벨을 부여하려면 FormField의 label을 사용하세요(Input 자체에는 없어요) — 자체 라벨을 렌더하는 컨트롤(Textarea, Checkbox, Radio, Switch, Select, Slider)을 감쌀 때는 생략하세요.
  • React에서는 래핑된 컨트롤이 아닌 FormField 자체에 label/error를 전달하는 걸 선호하세요 — FormField가 주지 않으면 래핑된 컨트롤이 자체 것을 렌더하므로 컨트롤 수준의 라벨/오류가 조용히 사라지지 않아요.

Don't

  • 어떤 플랫폼에서든 단일 FormField에 두 개 이상의 컨트롤을 감싸지 마세요 — 정확히 하나만 감싸요.

접근성

이것이 가장 중요한 섹션이에요 — 연결 메커니즘이 플랫폼마다 진짜로 달라요:

콘텐츠

라벨은 수집하는 값을 나타내요(“Email”), 지시가 아니에요. 도움말 텍스트는 사용자가 실수하기 전에 형식이나 제약을 설명해요(“Letters, numbers, and underscores only”); 오류 텍스트는 검증 후 무엇이 잘못되었고 어떻게 고치는지 설명해요.

Props API

React@ghds/react

PropTypeDefaultDescription
labelstring—래핑된 컨트롤의 보이는 라벨.
helperTextstring—오류가 아닌 설명 텍스트, 오류 상태와 무관하게 항상 표시돼요.
errorstring—오류 메시지. 설정 시 래핑된 컨트롤이 무효로 표시돼요.
idstring—래핑된 컨트롤에 적용되는 id(도움말/오류 id 파생에도 사용).
childrenReactNode—단일 폼 컨트롤 — context에서 id/aria-invalid/aria-describedby를 읽어요.

Web Components@ghds/web-components

PropTypeDefaultDescription
forstring—래핑된 컨트롤의 id — 네이티브 <label for>와 동일. 없으면 슬롯된 컨트롤에 자동 할당돼요.
labelstring—래핑된 컨트롤의 보이는 라벨.
helper-textstring—오류가 아닌 설명 텍스트(프로퍼티: helperText).
errorstring—오류 메시지. 설정 시 role="alert"로 렌더; 슬롯된 컨트롤에 aria-invalid도 설정.
helperIdstring (readonly getter)—`${baseId}-helper`로 계산되며, `baseId`는 `for`가 설정되면 그것, 그렇지 않으면 내부 생성 id.
errorIdstring (readonly getter)—`${baseId}-error`로 계산되며, `baseId`는 `for`가 설정되면 그것, 그렇지 않으면 내부 생성 id.

React Native@ghds/react-native

PropTypeDefaultDescription
labelstring—보이는 라벨. 주로 자체 라벨 prop이 없는 Input을 감쌀 때 유용.
helperTextstring—오류가 아닌 설명 텍스트, 오류 상태와 무관하게 항상 표시돼요.
errorstring—오류 메시지, 둘 다 설정 시 helperText와 함께 표시.
childrenReactNode—단일 폼 컨트롤.
testIDstring—쿼리용 테스트 핸들.

코드 예시

// React — @ghds/react (컨트롤 하나를 감싸요. label과 error는 FormField가 렌더해요)
import { FormField } from '@ghds/react/form-field';
import { Input } from '@ghds/react/input';

<FormField label="Password" error="Must be at least 8 characters">
  <Input type="password" />
</FormField>;
<!-- Web Components — @ghds/web-components (슬롯에 넣은 컨트롤에 id와 aria 속성을 자동으로 연결해요) -->
<script type="module">
  import '@ghds/web-components/form-field';
</script>
<gh-form-field for="password" label="Password" error="Must be at least 8 characters">
  <input type="password" />
</gh-form-field>
// React Native — @ghds/react-native (시각적인 구성만 해요. 이 플랫폼에는 ARIA 연결이 없어요)
import { FormField } from '@ghds/react-native/form-field';
import { Input } from '@ghds/react-native/input';

<FormField label="Email" error={emailError}>
  <Input value={email} onChangeText={setEmail} />
</FormField>;

라이브 데모

React와 Web Components가 아래에 라이브로 렌더돼요. React Native는 추가 react-native-web 연결 없이는 브라우저에서 실행할 수 없어요(이 사이트에 설정되지 않음), 따라서 사용법은 코드 샘플로 보여줘요.

React @ghds/react

Web Components @ghds/web-components