본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

필드셋

원문 보기 위키 갱신

필드셋 (Fieldset)

쉽게 스타일링할 수 있는 legend(범례)를 가진 고품질 비스타일(unstyled) React fieldset 컴포넌트예요. 관련 컨트롤을 하나의 그룹으로 묶고 공유 legend를 제공해요.

출처: Base UI Fieldset

본문

스크린 리더 등 접근성 높은 방식으로 관련 컨트롤을 묶는 데 쓰는 컴포넌트예요. <fieldset> 요소와 접근 가능한 legend를 렌더링해요.

데모 (Demo)

Tailwind

이 예시는 Tailwind CSS로 컴포넌트를 구현하는 방법을 보여줘요.

/* index.tsx */
import { Field } from '@base-ui/react/field';
import { Fieldset } from '@base-ui/react/fieldset';

export default function ExampleField() {
  return (
    <Fieldset.Root className="flex w-full max-w-64 flex-col gap-4">
      <Fieldset.Legend className="border-b border-neutral-950 text-base font-bold text-neutral-950 dark:border-white dark:text-white">
        Billing details
      </Fieldset.Legend>

      <Field.Root className="flex flex-col items-start gap-1">
        <Field.Label className="text-sm font-bold text-neutral-950 dark:text-white">
          Company
        </Field.Label>
        <Field.Control
          placeholder="Enter company name"
          className="h-8 w-full border border-neutral-950 bg-white dark:bg-neutral-950 px-2 text-sm any-pointer-coarse:text-base font-normal text-neutral-950 placeholder:text-neutral-500 focus:outline-2 focus:-outline-offset-1 focus:outline-neutral-950 dark:focus:outline-white dark:border-white dark:text-white dark:placeholder:text-neutral-400"
        />
      </Field.Root>

      <Field.Root className="flex flex-col items-start gap-1">
        <Field.Label className="text-sm font-bold text-neutral-950 dark:text-white">
          Tax ID
        </Field.Label>
        <Field.Control
          placeholder="Enter fiscal number"
          className="h-8 w-full border border-neutral-950 bg-white dark:bg-neutral-950 px-2 text-sm any-pointer-coarse:text-base font-normal text-neutral-950 placeholder:text-neutral-500 focus:outline-2 focus:-outline-offset-1 focus:outline-neutral-950 dark:focus:outline-white dark:border-white dark:text-white dark:placeholder:text-neutral-400"
        />
      </Field.Root>
    </Fieldset.Root>
  );
}

CSS Modules

이 예시는 CSS Modules로 컴포넌트를 구현하는 방법을 보여줘요.

/* index.module.css */
.Fieldset {
  border: 0;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  width: 100%;
  max-width: 16rem;
}

.Legend {
  border-bottom: 1px solid oklch(14.5% 0 0deg);
  font-weight: 700;
  font-size: 1rem;
  line-height: 1.5rem;
  color: oklch(14.5% 0 0deg);

  @media (prefers-color-scheme: dark) {
    border-bottom: 1px solid white;
    color: white;
  }
}

.Field {
  display: flex;
  flex-direction: column;
  align-items: start;
  gap: 0.25rem;
}

.Label {
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 700;
  color: oklch(14.5% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: white;
  }
}

.Input {
  box-sizing: border-box;
  padding: 0 0.5rem;
  margin: 0;
  border-radius: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  width: 100%;
  height: 2rem;
  font-family: inherit;
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 400;
  background-color: white;
  color: oklch(14.5% 0 0deg);

  @media (any-pointer: coarse) {
    font-size: 1rem;
    line-height: 1.5rem;
  }

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  &::placeholder {
    color: oklch(55.6% 0 0deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(70.8% 0 0deg);
    }
  }

  &:focus {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Error {
  font-size: 0.875rem;
  line-height: 1.25rem;
  color: oklch(50.5% 0.213 27.518deg);

  @media (prefers-color-scheme: dark) {
    color: oklch(70.4% 0.191 22.216deg);
  }
}

.Description {
  margin: 0;
  font-size: 0.875rem;
  line-height: 1.25rem;
  color: oklch(43.9% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: oklch(70.8% 0 0deg);
  }
}
/* index.tsx */
import { Field } from '@base-ui/react/field';
import { Fieldset } from '@base-ui/react/fieldset';
import styles from './index.module.css';

export default function ExampleField() {
  return (
    <Fieldset.Root className={styles.Fieldset}>
      <Fieldset.Legend className={styles.Legend}>Billing details</Fieldset.Legend>

      <Field.Root className={styles.Field}>
        <Field.Label className={styles.Label}>Company</Field.Label>
        <Field.Control placeholder="Enter company name" className={styles.Input} />
      </Field.Root>

      <Field.Root className={styles.Field}>
        <Field.Label className={styles.Label}>Tax ID</Field.Label>
        <Field.Control placeholder="Enter fiscal number" className={styles.Input} />
      </Field.Root>
    </Fieldset.Root>
  );
}

구조 (Anatomy)

컴포넌트를 임포트하고 파트를 조립해요.

import { Fieldset } from '@base-ui/react/fieldset';

<Fieldset.Root>
  <Fieldset.Legend />
</Fieldset.Root>;

API 레퍼런스

Root

공유 legend를 관련 컨트롤과 함께 묶어요. <fieldset> 요소를 렌더링해요.

Root Props:

Prop Type Default Description
className string \| ((state: Fieldset.Root.State) => string \| undefined) - 요소에 적용되는 CSS 클래스, 또는 컴포넌트 상태에 따라 클래스를 반환하는 함수.
style React.CSSProperties \| ((state: Fieldset.Root.State) => React.CSSProperties \| undefined) - 요소에 적용되는 스타일, 또는 컴포넌트 상태에 따라 스타일 객체를 반환하는 함수.
render ReactElement \| ((props: HTMLProps, state: Fieldset.Root.State) => ReactElement) - 컴포넌트의 HTML 요소를 다른 태그로 대체하거나 다른 컴포넌트와 합성할 수 있게 해줘요. ReactElement 또는 렌더링할 요소를 반환하는 함수를 받아요.

Root.Props

Root props의 재내보내기(re-export)예요.

Root.State

type FieldsetRootState = {
  /** Whether the component should ignore user interaction. */
  disabled: boolean;
};

Legend

필드셋과 자동으로 연결되는 접근 가능한 라벨이에요. <div> 요소를 렌더링해요.

Legend Props:

Prop Type Default Description
className string \| ((state: Fieldset.Legend.State) => string \| undefined) - 요소에 적용되는 CSS 클래스, 또는 컴포넌트 상태에 따라 클래스를 반환하는 함수.
style React.CSSProperties \| ((state: Fieldset.Legend.State) => React.CSSProperties \| undefined) - 요소에 적용되는 스타일, 또는 컴포넌트 상태에 따라 스타일 객체를 반환하는 함수.
render ReactElement \| ((props: HTMLProps, state: Fieldset.Legend.State) => ReactElement) - 컴포넌트의 HTML 요소를 다른 태그로 대체하거나 다른 컴포넌트와 합성할 수 있게 해줘요. ReactElement 또는 렌더링할 요소를 반환하는 함수를 받아요.

Legend.Props

Legend props의 재내보내기예요.

Legend.State

type FieldsetLegendState = {
  /** Whether the component should ignore user interaction. */
  disabled: boolean;
};

Export Groups

  • Fieldset.Root: Fieldset.Root, Fieldset.Root.State, Fieldset.Root.Props
  • Fieldset.Legend: Fieldset.Legend, Fieldset.Legend.State, Fieldset.Legend.Props
  • Default: FieldsetRootState, FieldsetRootProps, FieldsetLegendState, FieldsetLegendProps

Canonical Types

Canonical: Alias 매핑 — Canonical의 네임스페이스가 이미 임포트되어 있으면 Canonical을, 아니면 Alias를 사용하세요.

  • Fieldset.Root.State: FieldsetRootState
  • Fieldset.Root.Props: FieldsetRootProps
  • Fieldset.Legend.State: FieldsetLegendState
  • Fieldset.Legend.Props: FieldsetLegendProps

더 알아보기 (Learn more)