Todae

Todae

Fieldset

체크박스 묶음처럼 관련된 컨트롤을 하나의 <legend> 아래로 묶는 네이티브 <fieldset>입니다. 포커스가 그룹 안으로 들어가면 스크린 리더가 legend를 그룹 이름으로 읽습니다.

이 페이지의 내용
import { Checkbox, Description, ErrorMessage, Fieldset } from '@tounsoo/todae';
import { useId, useState } from 'react';

export default function FieldsetCheckboxes() {
  const [topics, setTopics] = useState<string[]>([]);
  const hintId = useId();
  const errorId = useId();
  const invalid = topics.length === 0;
  const toggle = (topic: string) => (checked: boolean) =>
    setTopics((prev) => (checked ? [...prev, topic] : prev.filter((t) => t !== topic)));
  return (
    <Fieldset.Root invalid={invalid} aria-describedby={invalid ? `${hintId} ${errorId}` : hintId}>
      <Fieldset.Legend>Email topics</Fieldset.Legend>
      <Description.Root id={hintId}>We send at most one email a week.</Description.Root>
      <Checkbox.Root checked={topics.includes('product')} onCheckedChange={toggle('product')}>
        Product news
      </Checkbox.Root>
      <Checkbox.Root checked={topics.includes('security')} onCheckedChange={toggle('security')}>
        Security notices
      </Checkbox.Root>
      <Checkbox.Root checked={topics.includes('events')} onCheckedChange={toggle('events')}>
        Events
      </Checkbox.Root>
      {invalid ? <ErrorMessage.Root id={errorId}>Pick at least one topic.</ErrorMessage.Root> : null}
    </Fieldset.Root>
  );
}
Email topicsWe send at most one email a week.Pick at least one topic.

invalid는 안의 모든 Todae 컨트롤을 invalid로 표시합니다. Checkbox, Switch, RadioGroup, Field가 aria-invalid와 invalid 스타일을 받습니다. fieldset에는 자체 aria-invalid가 없으므로, 이유는 Description이나 ErrorMessage로 알리고 Root의 aria-describedby로 그것을 가리키세요.

disabled는 네이티브 속성입니다. 브라우저가 안의 모든 폼 컨트롤을 비활성화하고, Todae 컨트롤은 비활성 스타일을 보여 줍니다. 비활성 fieldset 안의 fieldset도 비활성입니다. invalid fieldset 안에서는 컨트롤이나 중첩된 fieldset이 invalid={false}를 주지 않는 한 invalid입니다. 올바르게 입력한 필드에 쓰세요. 첫 번째 Fieldset.Legend 안의 컨트롤은 브라우저가 그러듯이 활성 상태로 남습니다.

import { Checkbox, Fieldset, Stack, Switch } from '@tounsoo/todae';
import { useState } from 'react';

export default function FieldsetDisabled() {
  const [paused, setPaused] = useState(true);
  return (
    <Stack.Root>
      <Switch.Root checked={paused} onCheckedChange={setPaused}>
        Pause notifications
      </Switch.Root>
      <Fieldset.Root disabled={paused}>
        <Fieldset.Legend>Channels</Fieldset.Legend>
        <Stack.Root direction="row">
          <Checkbox.Root defaultChecked>Email</Checkbox.Root>
          <Checkbox.Root>Push</Checkbox.Root>
          <Checkbox.Root>SMS</Checkbox.Root>
        </Stack.Root>
      </Fieldset.Root>
    </Stack.Root>
  );
}
Channels

파트는 fieldset.gap 간격으로 쌓입니다. 컨트롤을 나란히 두려면 Stack.Root direction="row" 안에 넣으세요.

Form.Root 안에서 Fieldset.Root는 검증 범위로, 안에 있는 컨트롤의 문제를 id 없는 ErrorMessage.Root에 보여 줍니다. customValidity prop은 브라우저가 확인할 수 없는 규칙을 위해, 자기 컨트롤 중 검증에 참여하는 첫 컨트롤에, 컨트롤이 모두 중첩된 그룹 안에 있으면 어느 깊이든 첫 컨트롤에 브라우저의 setCustomValidity로 메시지를 설정합니다. 그런 컨트롤이 없으면 개발 중에 경고합니다.

접근성을 지키는 조합

Todae가 맡는 것: Fieldset.Root는 네이티브 <fieldset>이므로 group role을 가지며, 첫 Fieldset.Legend가 그룹의 이름입니다. disabled는 네이티브 속성입니다. 안의 모든 폼 컨트롤이 비활성화되고 탭 순서에서 빠지지만, 첫 legend 안의 컨트롤은 예외입니다. invalid는 안에 있는 Todae 컨트롤마다 aria-invalid를 설정합니다. Checkbox, Switch, RadioGroup.Root와 그 라디오, Field.Root의 컨트롤입니다. 첫 legend 안의 컨트롤은 invalid에서도 빠집니다. 중첩된 fieldset은 둘 다 물려받습니다.

직접 제공할 것: 첫 자식으로 둔 Fieldset.Legend와, 질문을 이름 짓는 텍스트. fieldset에는 자체 aria-invalid가 없으므로, 루트의 aria-describedby가 이유를 말하는 ErrorMessage나 Description을 가리키게 하세요. 사용자가 맞게 입력한 컨트롤에는 invalid={false}를 주세요.

주의할 점: 다른 요소로 감싸거나 다른 legend 뒤에 둔 Legend는 그룹 이름이 되지 않고, 그 안의 컨트롤은 fieldset의 disabled를 따릅니다. disabled는 읽기 전용이 아닙니다. 비활성 컨트롤은 포커스를 받지 못하므로 키보드 사용자는 그 값에 닿을 수 없습니다. fieldset 대신 스타일을 입힌 div를 쓰면 그룹과 그 이름이 사라집니다.

언제 쓰나요

언제: 여러 컨트롤이 하나의 질문에 답할 때. 체크박스 그룹, 스위치 묶음, 주소 입력 필드들. 그룹 이름은 Fieldset.Legend로 붙입니다.

쓰지 않을 때: 필드 하나 → Field.Root와 Label. 여러 옵션 중 하나 고르기 → 그 자체가 그룹인 RadioGroup. 각자 제목이 있는 긴 폼의 구획 → 제목.

Props

Fieldset.Root props
이름타입설명
disabledboolean

네이티브 disabled: 안의 모든 폼 컨트롤이 비활성화됩니다. 안의 Todae 컨트롤도 비활성 스타일을 보여 줍니다.

기본값: false

invalidboolean

안의 Todae 컨트롤을 invalid로 표시합니다(aria-invalid와 invalid 스타일). invalid={false}를 준 컨트롤은 제외됩니다. fieldset에는 자체 aria-invalid가 없으므로 aria-describedby로 ErrorMessage를 가리킵니다.

기본값: false

customValiditystring

브라우저가 검사할 수 없는 규칙의 메시지입니다. fieldset 자기 컨트롤 중 검증에 참여하는 첫 컨트롤에, 컨트롤이 모두 중첩된 그룹 안에 있으면 어느 깊이든 첫 컨트롤(오류가 보이는 곳)에 setCustomValidity로 설정합니다. 빈 문자열이나 undefined는 규칙이 지켜졌다는 뜻입니다. 그런 컨트롤이 없으면 개발 중에 경고합니다.

aria-describedbystring

그룹 전체에 대한 Description 또는 ErrorMessage의 id.

childrenReact.ReactNode

먼저 Fieldset.Legend, 그다음 컨트롤.

Fieldset.Legend props
이름타입설명
childrenReact.ReactNode

그룹 이름.