דילוג לתוכן

שדה

משבצות לתוויות, כפתורי בקרה, טקסט עזרה והודעות שגיאה עבור לוחות הגדרות, טפסי תשלום ותהליכי הרשמה — בעיצוב טפסים בסגנון "ניאו-ברוטליסטי".

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

ה-field עוטף לייבל, בקרה, טקסט עזרה והודעת שגיאה לבלוק מיושר אחד, ומתרחב למקטעים מקובצים עם FieldGroup ו-FieldSet. אין פרימיטיב מתחת — סמנטיקה של fieldset, legend ו-role="group" בהרכבה עם Label ו-Separator, מעוצבת לפי המתכון הנאו-ברוטליסטי: טיפוגרפיית כותרת מודגשת על legends ולייבלים, בעוד הבקרות העטופות מביאות את המסגרות העבות והצללים החדים.

השתמשו בו כש:

  • עמודי הגדרות — עורמים מקטעי פרופיל, התראות וחיוב עם legends של FieldSet ו-FieldSeparator בין קבוצות.
  • טפסי checkout והרשמה — שומרים על יישור עקבי של לייבל, תיאור ושגיאה בין inputs, selects ו-switches.
  • כרטיסי בחירה — עוטפים radios, checkboxes או switches בכרטיסי 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

שדות קשורים בקבוצה אחת. השתמשו ב-FieldSeparator בין המקטעים לפי הצורך.

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

FieldSet

קיבוץ סמנטי עם כותרת (legend) ותיאור, שמכיל בדרך כלל 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, והשתמשו ב-FieldSet יחד עם FieldLegend לקיבוץ סמנטי.

טופס

עיינו בתיעוד של Form לבניית טפסים עם רכיב Field ועם React Hook Form, Tanstack Form או Formisch.

דוגמאות

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 כדי ליצור קבוצות שדות שניתן לבחור. זה עובד עם הרכיבים RadioItem, Checkbox ו-Switch.

import {
  Field,
  FieldContent,

קבוצת שדות

ערמו רכיבי Field בעזרת FieldGroup. הוסיפו FieldSeparator כדי להפריד ביניהם.

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

RTL

כדי להפעיל RTL ב-Neobrutalism, עיינו במדריך ההגדרה של RTL.

"use client"

import * as React from "react"

פריסה רספונסיבית

  • שדות אנכיים: האוריינטציה שברירת המחדל עורמת את התווית, הפקד וטקסט העזרה — אידיאלי לפריסות שמתוכננות קודם למובייל.
  • שדות אופקיים: הגדירו orientation="horizontal" על Field כדי ליישר את התווית והפקד זה לצד זה. שלבו עם FieldContent כדי לשמור על יישור התיאורים.
  • שדות רספונסיביים: הגדירו orientation="responsive" לפריסות עמודות אוטומטיות בתוך הורים שמודעים לקונטיינר. החילו את המחלקות @container/field-group על FieldGroup כדי להחליף אוריינטציה בנקודות שבירה מסוימות.
import { Button } from "@/components/ui/button"
import {
  Field,

אימות ושגיאות

  • הוסיפו data-invalid ל-Field כדי להעביר את כל הבלוק למצב שגיאה.
  • הוסיפו 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>

נגישות

  • FieldSet ו-FieldLegend שומרים על פקדים קשורים מקובצים עבור משתמשי מקלדת וטכנולוגיה מסייעת.
  • Field מפיק role="group", כך שפקדים מקוננים יורשים את התיוג מ-FieldLabel ו-FieldLegend כשהם משולבים.
  • השתמשו ב-FieldSeparator במשורה כדי להבטיח שקוראי מסך ייתקלו בגבולות מקטע ברורים.

נגישות

משפחת ה-field היא סמנטיקה קודם: FieldSet ו-FieldLegend מרנדרים fieldset/legend מקוריים, Field מרנדר role="group", ו-FieldError מרנדר role="alert" כך שהודעות ולידציה מוכרזות ברגע שהן מופיעות.

  • חברו כל FieldLabel לבקרה שלו דרך htmlFor/id — לחיצה על הלייבל ממקדת את הבקרה וקוראי מסך מכריזים על הקשר.
  • בקלט לא תקין, הגדירו aria-invalid על הבקרה עצמה; data-invalid על Field מניע רק את עיצוב השגיאה.
  • התנהגות מקלדת שייכת לבקרות בפנים — העטיפות לא מוסיפות עצירות פוקוס או קישורי מקשים משלהן.

תיעוד ה-API

FieldSet

מכל שמרנדר fieldset סמנטי עם ערכות ריווח מוגדרות מראש.

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

FieldLegend

רכיב legend עבור FieldSet. עברו לווריאנט label כדי להתיישר עם גודל התוויות.

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

ל-FieldLegend יש שני וריאנטים: legend ו-label. הווריאנט label מחיל את הגודל והיישור של תווית. שימושי כשיש לכם FieldSet מקונן.

FieldGroup

עוטף פריסה שעורם רכיבי Field ומאפשר container queries לאוריינטציות רספונסיביות.

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

מכל שגיאות נגיש שמקבל צאצאים או מערך errors (למשל מ-react-hook-form).

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

כשמערך errors מכיל כמה הודעות, הרכיב מרנדר רשימה אוטומטית.

FieldError מקבל גם בעיות (issues) שמופקות על ידי כל ולידטור שמממש את Standard Schema, כולל Zod, Valibot ו-ArkType. העבירו את מערך issues מתוצאת הסכימה ישירות כדי לרנדר רשימת שגיאות מאוחדת בין הספריות.