コンテンツへスキップ

フィールド

設定パネル、チェックアウトフォーム、登録フロー向けのラベル、コントロール、ヘルプテキスト、エラー表示欄――ネオブルータリズム風のフォームデザインを採用しています。

import { Button } from "@/components/ui/button"
import { Checkbox } from "@/components/ui/checkbox"
import {

Field はラベル、コントロール、ヘルパーテキスト、エラーメッセージを1つの揃ったブロックにまとめ、FieldGroupFieldSet でグループ化したセクションにも拡張できます。下層にプリミティブはなく、プレーンな fieldsetlegendrole="group" のセマンティクスを LabelSeparator と組み合わせています。レジェンドとラベルは太字の見出しタイポグラフィ、包むコントロール側が太い枠線と硬い影を担います。

次のような場面で使います:

  • 設定ページFieldSet のレジェンドとグループ間の FieldSeparator で、プロフィール、通知、請求を積み重ねます。
  • チェックアウトとサインアップ — 入力、セレクト、スイッチでラベル・説明・エラーの揃えを統一します。
  • 選択カード — ラジオ、チェックボックス、スイッチを選択可能な FieldLabel カードで包み、プラン選択や設定パネルに使います。

インストール

pnpm dlx shadcn@latest add https://neobrutalism.com/r/base/field.json
npx shadcn@latest add https://neobrutalism.com/r/base/field.json
yarn dlx shadcn@latest add https://neobrutalism.com/r/base/field.json
bunx --bun shadcn@latest add https://neobrutalism.com/r/base/field.json

以下のコードをコピーして、プロジェクトに貼り付けます。

components/ui/field.tsx
"use client"

import { useMemo } from "react"
import { cva, type VariantProps } from "class-variance-authority"

import { cn } from "@/lib/utils"
import { Label } from "@/components/ui/label"
import { Separator } from "@/components/ui/separator"

function FieldSet({ className, ...props }: React.ComponentProps<"fieldset">) {
  return (
    <fieldset
      data-slot="field-set"
      className={cn(
        "flex flex-col gap-4 has-[>[data-slot=checkbox-group]]:gap-3 has-[>[data-slot=radio-group]]:gap-3",
        className
      )}
      {...props}
    />
  )
}

function FieldLegend({
  className,
  variant = "legend",
  ...props
}: React.ComponentProps<"legend"> & { variant?: "legend" | "label" }) {
  return (
    <legend
      data-slot="field-legend"
      data-variant={variant}
      className={cn(
        "mb-1.5 font-head font-medium data-[variant=label]:text-sm data-[variant=legend]:text-base",
        className
      )}
      {...props}
    />
  )
}

function FieldGroup({ className, ...props }: React.ComponentProps<"div">) {
  return (
    <div
      data-slot="field-group"
      className={cn(
        "group/field-group @container/field-group flex w-full flex-col gap-5 data-[slot=checkbox-group]:gap-3 *:data-[slot=field-group]:gap-4",
        className
      )}
      {...props}
    />
  )
}

const fieldVariants = cva(
  "group/field flex w-full gap-2 data-[invalid=true]:text-destructive",
  {
    variants: {
      orientation: {
        vertical: "flex-col *:w-full [&>.sr-only]:w-auto",
        horizontal:
          "flex-row items-center has-[>[data-slot=field-content]]:items-start *:data-[slot=field-label]:flex-auto has-[>[data-slot=field-content]]:[&>[role=checkbox],[role=radio]]:mt-px",
        responsive:
          "flex-col *:w-full @md/field-group:flex-row @md/field-group:items-center @md/field-group:*:w-auto @md/field-group:has-[>[data-slot=field-content]]:items-start @md/field-group:*:data-[slot=field-label]:flex-auto [&>.sr-only]:w-auto @md/field-group:has-[>[data-slot=field-content]]:[&>[role=checkbox],[role=radio]]:mt-px",
      },
    },
    defaultVariants: {
      orientation: "vertical",
    },
  }
)

function Field({
  className,
  orientation = "vertical",
  ...props
}: React.ComponentProps<"div"> & VariantProps<typeof fieldVariants>) {
  return (
    <div
      role="group"
      data-slot="field"
      data-orientation={orientation}
      className={cn(fieldVariants({ orientation }), className)}
      {...props}
    />
  )
}

function FieldContent({ className, ...props }: React.ComponentProps<"div">) {
  return (
    <div
      data-slot="field-content"
      className={cn(
        "group/field-content flex flex-1 flex-col gap-0.5 leading-snug",
        className
      )}
      {...props}
    />
  )
}

function FieldLabel({
  className,
  ...props
}: React.ComponentProps<typeof Label>) {
  return (
    <Label
      data-slot="field-label"
      className={cn(
        "group/field-label peer/field-label flex w-fit gap-2 leading-snug group-data-[disabled=true]/field:opacity-50 has-data-checked:border-primary/30 has-data-checked:bg-primary/5 has-[>[data-slot=field]]:rounded has-[>[data-slot=field]]:border-2 *:data-[slot=field]:p-2.5 dark:has-data-checked:border-primary/20 dark:has-data-checked:bg-primary/10",
        "has-[>[data-slot=field]]:w-full has-[>[data-slot=field]]:flex-col",
        className
      )}
      {...props}
    />
  )
}

function FieldTitle({ className, ...props }: React.ComponentProps<"div">) {
  return (
    <div
      data-slot="field-label"
      className={cn(
        "flex w-fit items-center gap-2 text-sm font-head font-medium group-data-[disabled=true]/field:opacity-50",
        className
      )}
      {...props}
    />
  )
}

function FieldDescription({ className, ...props }: React.ComponentProps<"p">) {
  return (
    <p
      data-slot="field-description"
      className={cn(
        "text-left text-sm leading-normal font-normal text-muted-foreground group-has-data-horizontal/field:text-balance [[data-variant=legend]+&]:-mt-1.5",
        "last:mt-0 nth-last-2:-mt-1",
        "[&>a]:underline [&>a]:underline-offset-4 [&>a:hover]:text-primary",
        className
      )}
      {...props}
    />
  )
}

function FieldSeparator({
  children,
  className,
  ...props
}: React.ComponentProps<"div"> & {
  children?: React.ReactNode
}) {
  return (
    <div
      data-slot="field-separator"
      data-content={!!children}
      className={cn(
        "relative -my-2 h-5 text-sm group-data-[variant=outline]/field-group:-mb-2",
        className
      )}
      {...props}
    >
      <Separator className="absolute inset-0 top-1/2" />
      {children && (
        <span
          className="relative mx-auto block w-fit bg-background px-2 text-muted-foreground"
          data-slot="field-separator-content"
        >
          {children}
        </span>
      )}
    </div>
  )
}

function FieldError({
  className,
  children,
  errors,
  ...props
}: React.ComponentProps<"div"> & {
  errors?: Array<{ message?: string } | undefined>
}) {
  const content = useMemo(() => {
    if (children) {
      return children
    }

    if (!errors?.length) {
      return null
    }

    const uniqueErrors = [
      ...new Map(errors.map((error) => [error?.message, error])).values(),
    ]

    if (uniqueErrors?.length == 1) {
      return uniqueErrors[0]?.message
    }

    return (
      <ul className="ml-4 flex list-disc flex-col gap-1">
        {uniqueErrors.map(
          (error, index) =>
            error?.message && <li key={index}>{error.message}</li>
        )}
      </ul>
    )
  }, [children, errors])

  if (!content) {
    return null
  }

  return (
    <div
      role="alert"
      data-slot="field-error"
      className={cn("text-sm font-normal text-destructive", className)}
      {...props}
    >
      {content}
    </div>
  )
}

export {
  Field,
  FieldLabel,
  FieldDescription,
  FieldError,
  FieldGroup,
  FieldLegend,
  FieldSeparator,
  FieldSet,
  FieldContent,
  FieldTitle,
}

インポートのパスをプロジェクトの構成に合わせて更新します。

使い方

import {
  Field,
  FieldContent,
  FieldDescription,
  FieldError,
  FieldGroup,
  FieldLabel,
  FieldLegend,
  FieldSeparator,
  FieldSet,
  FieldTitle,
} from "@/components/ui/field"
<FieldSet>
  <FieldLegend>Profile</FieldLegend>
  <FieldDescription>This appears on invoices and emails.</FieldDescription>
  <FieldGroup>
    <Field>
      <FieldLabel htmlFor="name">Full name</FieldLabel>
      <Input id="name" autoComplete="off" placeholder="Evil Rabbit" />
      <FieldDescription>This appears on invoices and emails.</FieldDescription>
    </Field>
    <Field>
      <FieldLabel htmlFor="username">Username</FieldLabel>
      <Input id="username" autoComplete="off" aria-invalid />
      <FieldError>Choose another username.</FieldError>
    </Field>
    <Field orientation="horizontal">
      <Switch id="newsletter" />
      <FieldLabel htmlFor="newsletter">Subscribe to the newsletter</FieldLabel>
    </Field>
  </FieldGroup>
</FieldSet>

コンポジション

Field

ラベル、ヘルプテキスト、バリデーションを備えた単一のコントロールです。

Field
├── FieldLabel
├── Input / Textarea / Switch / Select
├── FieldDescription
└── FieldError

FieldGroup

関連するフィールドを 1 つのグループにまとめます。必要に応じて、セクションの間に FieldSeparator を挟みます。

FieldGroup
├── Field
│   ├── FieldLabel
│   ├── Input / Textarea / Switch / Select
│   ├── FieldDescription
│   └── FieldError
├── FieldSeparator
└── Field
    ├── FieldLabel
    └── Input / Textarea / Switch / Select

FieldSet

凡例と説明を備えたセマンティックなグループで、通常は FieldGroup を含みます。

FieldSet
├── FieldLegend
├── FieldDescription
└── FieldGroup
    ├── Field
    │   ├── FieldLabel
    │   ├── Input / Textarea / Switch / Select
    │   ├── FieldDescription
    │   └── FieldError
    └── Field
        ├── FieldLabel
        └── Input / Textarea / Switch / Select

構成

Field ファミリーは、アクセシブルなフォームを構成するために設計されています。一般的なフィールドは、次のような構造になります。

<Field>
  <FieldLabel htmlFor="input-id">Label</FieldLabel>
  {/* Input, Select, Switch, etc. */}
  <FieldDescription>Optional helper text.</FieldDescription>
  <FieldError>Validation message.</FieldError>
</Field>
  • Field は、単一のフィールドの中核となるラッパーです。
  • FieldContent は、ラベルと説明をまとめる flex カラムです。説明がない場合は不要です。
  • 関連するフィールドは FieldGroup でまとめ、セマンティックなグループ化には FieldSetFieldLegend を使用します。

フォーム

Field コンポーネントと React Hook FormTanstack FormFormisch を使ってフォームを作成する方法は、Form のドキュメントをご覧ください。

使用例

Input

import {
  Field,
  FieldDescription,

Textarea

import {
  Field,
  FieldDescription,

Select

import {
  Field,
  FieldDescription,

Slider

"use client"

import * as React from "react"

Fieldset

import {
  Field,
  FieldDescription,

Checkbox

import { Checkbox } from "@/components/ui/checkbox"
import {
  Field,

Radio

import {
  Field,
  FieldDescription,

Switch

import { Field, FieldLabel } from "@/components/ui/field"
import { Switch } from "@/components/ui/switch"

選択カード

Field コンポーネントを FieldLabel で包んで、選択可能なフィールドグループを作成します。これは RadioItemCheckboxSwitch コンポーネントで機能します。

import {
  Field,
  FieldContent,

フィールドグループ

FieldGroupField コンポーネントを積み重ねます。区切るには FieldSeparator を追加します。

import { Checkbox } from "@/components/ui/checkbox"
import {
  Field,

RTL

Neobrutalism で RTL を有効にする方法は、RTL 設定ガイドをご覧ください。

"use client"

import * as React from "react"

レスポンシブレイアウト

  • 縦方向のフィールド: デフォルトの向きでは、ラベル、コントロール、ヘルプテキストが縦に積み重なります。モバイルファーストのレイアウトに最適です。
  • 横方向のフィールド: Fieldorientation="horizontal" を設定すると、ラベルとコントロールが横並びになります。FieldContent と組み合わせると、説明の位置をそろえられます。
  • レスポンシブなフィールド: コンテナを認識する親要素の内側で列レイアウトを自動化するには、orientation="responsive" を設定します。特定のブレークポイントで向きを切り替えるには、FieldGroup@container/field-group クラスを適用します。
import { Button } from "@/components/ui/button"
import {
  Field,

バリデーションとエラー

  • Fielddata-invalid を追加すると、ブロック全体がエラー状態に切り替わります。
  • 支援技術のために、入力欄そのものに aria-invalid を追加します。
  • エラーメッセージをフィールドに合わせて配置するために、FieldError はコントロールの直後、または FieldContent の内側に表示します。
<Field data-invalid>
  <FieldLabel htmlFor="email">Email</FieldLabel>
  <Input id="email" type="email" aria-invalid />
  <FieldError>Enter a valid email address.</FieldError>
</Field>

アクセシビリティ

  • FieldSetFieldLegend は、キーボードや支援技術を利用する人のために、関連するコントロールをグループ化したままにします。
  • Fieldrole="group" を出力するため、組み合わせたときに入れ子のコントロールが FieldLabelFieldLegend のラベル付けを継承します。
  • スクリーンリーダーが明確なセクションの境界を認識できるよう、FieldSeparator は控えめに使用します。

アクセシビリティ

Field ファミリーはセマンティクス優先です。FieldSetFieldLegend はネイティブの fieldsetlegendFieldrole="group"FieldErrorrole="alert" を描画し、検証メッセージは表示された瞬間に読み上げられます。

  • すべての FieldLabelhtmlForid でコントロールと結び付けます。ラベルをクリックするとフォーカスが移り、スクリーンリーダーも関連を読み上げます。
  • 無効な入力ではコントロール自体に aria-invalid を付けます。Fielddata-invalid はエラー用スタイルだけを駆動します。
  • キーボード挙動は内側のコントロール側のものです。ラッパーはフォーカスストップもキーバインドも追加しません。

APIリファレンス

FieldSet

間隔のプリセットを備えたセマンティックな fieldset をレンダリングするコンテナです。

PropTypeDefault
classNamestring
<FieldSet>
  <FieldLegend>Delivery</FieldLegend>
  <FieldGroup>{/* Fields */}</FieldGroup>
</FieldSet>

FieldLegend

FieldSet の凡例要素です。ラベルのサイズに合わせるには、label バリアントに切り替えます。

PropTypeDefault
variant"legend" | "label""legend"
classNamestring
<FieldLegend variant="label">Notification Preferences</FieldLegend>

FieldLegend には legendlabel の 2 つのバリアントがあります。label バリアントは、ラベルのサイズと配置を適用します。FieldSet を入れ子にする場合に便利です。

FieldGroup

Field コンポーネントを積み重ね、レスポンシブな向きのためにコンテナクエリを有効にするレイアウトラッパーです。

PropTypeDefault
classNamestring
<FieldGroup className="@container/field-group flex flex-col gap-6">
  <Field>{/* ... */}</Field>
  <Field>{/* ... */}</Field>
</FieldGroup>

Field

単一のフィールドの中核となるラッパーです。向きの制御、エラー状態のスタイル、間隔を提供します。

PropTypeDefault
orientation"vertical" | "horizontal" | "responsive""vertical"
classNamestring
data-invalidboolean
<Field orientation="horizontal">
  <FieldLabel htmlFor="remember">Remember me</FieldLabel>
  <Switch id="remember" />
</Field>

FieldContent

ラベルがコントロールの横に配置されるときに、コントロールと説明をまとめる flex カラムです。説明がない場合は不要です。

PropTypeDefault
classNamestring
<Field>
  <Checkbox id="notifications" />
  <FieldContent>
    <FieldLabel htmlFor="notifications">Notifications</FieldLabel>
    <FieldDescription>Email, SMS, and push options.</FieldDescription>
  </FieldContent>
</Field>

FieldLabel

直接の入力欄にも、入れ子になった Field の子要素にも対応したスタイルのラベルです。

PropTypeDefault
classNamestring
asChildbooleanfalse
<FieldLabel htmlFor="email">Email</FieldLabel>

FieldTitle

FieldContent の内側で、ラベルのスタイルを適用したタイトルをレンダリングします。

PropTypeDefault
classNamestring
<FieldContent>
  <FieldTitle>Enable Touch ID</FieldTitle>
  <FieldDescription>Unlock your device faster.</FieldDescription>
</FieldContent>

FieldDescription

横方向のレイアウトで長い行を自動的に調整するヘルプテキストのスロットです。

PropTypeDefault
classNamestring
<FieldDescription>We never share your email with anyone.</FieldDescription>

FieldSeparator

FieldGroup の内側でセクションを区切る視覚的な仕切りです。インラインのコンテンツを任意で受け取れます。

PropTypeDefault
classNamestring
<FieldSeparator>Or continue with</FieldSeparator>

FieldError

子要素、または(react-hook-form などからの)errors 配列を受け取る、アクセシブルなエラーコンテナです。

PropTypeDefault
errorsArray<{ message?: string } | undefined>
classNamestring
<FieldError errors={errors.username} />

errors 配列に複数のメッセージが含まれる場合、コンポーネントは自動的にリストをレンダリングします。

FieldError は、Zod、Valibot、ArkType など、Standard Schema を実装する任意のバリデーターが生成する issue も受け取ります。スキーマの結果の issues 配列をそのまま渡すと、ライブラリをまたいで統一されたエラーリストをレンダリングできます。