Field

フォーム入力フィールドのレイアウトとセマンティクスを担当するコンポーネント群。

Installation

npx shadcn@latest add https://kasumi-ui.vercel.app/r/field.json

Usage

import { Field, FieldLabel } from "@/components/ui/field";
import { Input } from "@/components/ui/input";

<Field>
<FieldLabel>メールアドレス</FieldLabel>
<Input placeholder="you@example.com" />
</Field>

Description

フィールドに補足説明を追加できます。

3文字以上の英数字で入力してください。

<Field>
<FieldLabel>ユーザー名</FieldLabel>
<FieldDescription>3文字以上の英数字で入力してください。</FieldDescription>
<Input placeholder="username" />
</Field>

Error

data-invalid="true" を Field に指定すると、FieldLabel が自動で赤色に変わります。FieldError はエラーメッセージを表示します。

<Field data-invalid="true">
<FieldLabel>メールアドレス</FieldLabel>
<Input error placeholder="you@example.com" />
<FieldError errors={[{ message: "メールアドレスの形式が正しくありません。" }]} />
</Field>

FieldGroup

複数のフィールドをまとめるコンテナです。

<FieldGroup>
<Field>
  <FieldLabel>姓</FieldLabel>
  <Input placeholder="田中" />
</Field>
<Field>
  <FieldLabel>名</FieldLabel>
  <Input placeholder="太郎" />
</Field>
</FieldGroup>

FieldSet & FieldLegend

セマンティックな <fieldset><legend> でフォームセクションをグルーピングできます。

通知設定
import { FieldSet, FieldLegend, Field, FieldLabel } from "@/components/ui/field";
import { Checkbox } from "@/components/ui/checkbox";

<FieldSet>
<FieldLegend>通知設定</FieldLegend>
<Field>
  <div className="flex items-center gap-2">
    <Checkbox id="email-notify" />
    <FieldLabel htmlFor="email-notify">メール通知を受け取る</FieldLabel>
  </div>
</Field>
<Field>
  <div className="flex items-center gap-2">
    <Checkbox id="push-notify" />
    <FieldLabel htmlFor="push-notify">プッシュ通知を受け取る</FieldLabel>
  </div>
</Field>
</FieldSet>

Props

PropTypeDefaultDescription
FieldComponentProps<"div">フィールドのルート要素。data-invalid でエラー状態を子に伝播
FieldLabelComponentProps<typeof Label>フィールドのラベル。エラー時に自動で赤色になる
FieldDescriptionComponentProps<"p">フィールドの補足説明テキスト
FieldError{ errors?: ({ message?: string } | undefined)[] }エラーメッセージ表示。errors 配列から最初のメッセージを表示
FieldGroupComponentProps<"div">複数のフィールドをグルーピングするコンテナ
FieldSetComponentProps<"fieldset">セマンティックなフィールドセット要素
FieldLegendComponentProps<"legend">FieldSet の見出し