Pole
Pola na etykiety, elementy sterujące, teksty pomocy i komunikaty o błędach w panelach ustawień, formularzach płatności i procesach rejestracji — w stylu neobrutalistycznym.
import { Button } from "@/components/ui/button"
import { Checkbox } from "@/components/ui/checkbox"
import {Field spina etykietę, kontrolkę, tekst pomocniczy i komunikat błędu w jeden wyrównany blok, a dzięki FieldGroup i FieldSet skaluje się do pogrupowanych sekcji. Pod spodem nie ma prymitywu — to zwykła semantyka fieldset, legend i role="group" złożona z komponentów Label i Separator, ostylowana zgodnie z neobrutalistyczną recepturą: wyrazista typografia nagłówkowa na legendach i etykietach, a grube obramowania i twarde cienie wnoszą opakowane kontrolki.
Sięgnij po ten komponent, gdy:
- Strony ustawień — układaj sekcje profilu, powiadomień i rozliczeń z legendami
FieldSeti separatoremFieldSeparatormiędzy grupami. - Formularze płatności i rejestracji — utrzymuj spójne wyrównanie etykiet, opisów i błędów w polach tekstowych, selectach i przełącznikach.
- Karty wyboru — opakuj przyciski radio, checkboxy lub przełączniki w zaznaczalne karty
FieldLabeldo wyboru planu i paneli preferencji.
Instalacja#
pnpm dlx shadcn@latest add https://neobrutalism.com/r/base/field.jsonnpx shadcn@latest add https://neobrutalism.com/r/base/field.jsonyarn dlx shadcn@latest add https://neobrutalism.com/r/base/field.jsonbunx --bun shadcn@latest add https://neobrutalism.com/r/base/field.json
Skopiuj i wklej poniższy kod do swojego projektu.
"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,
}
Zaktualizuj ścieżki importu, aby pasowały do konfiguracji twojego projektu.
Użycie#
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>Kompozycja#
Field#
Pojedyncza kontrolka z etykietą, tekstem pomocy i walidacją.
Field
├── FieldLabel
├── Input / Textarea / Switch / Select
├── FieldDescription
└── FieldErrorFieldGroup#
Powiązane pola w jednej grupie. Użyj FieldSeparator między sekcjami, gdy jest to potrzebne.
FieldGroup
├── Field
│ ├── FieldLabel
│ ├── Input / Textarea / Switch / Select
│ ├── FieldDescription
│ └── FieldError
├── FieldSeparator
└── Field
├── FieldLabel
└── Input / Textarea / Switch / SelectFieldSet#
Semantyczne grupowanie z legendą i opisem, zwykle zawierające FieldGroup.
FieldSet
├── FieldLegend
├── FieldDescription
└── FieldGroup
├── Field
│ ├── FieldLabel
│ ├── Input / Textarea / Switch / Select
│ ├── FieldDescription
│ └── FieldError
└── Field
├── FieldLabel
└── Input / Textarea / Switch / SelectAnatomia#
Rodzina Field została zaprojektowana do tworzenia dostępnych formularzy. Typowe pole ma następującą strukturę:
<Field>
<FieldLabel htmlFor="input-id">Label</FieldLabel>
{/* Input, Select, Switch, etc. */}
<FieldDescription>Optional helper text.</FieldDescription>
<FieldError>Validation message.</FieldError>
</Field>Fieldto podstawowy wrapper dla pojedynczego pola.FieldContentto kolumna flex, która grupuje etykietę i opis. Nie jest wymagana, jeśli nie masz opisu.- Otaczaj powiązane pola komponentem
FieldGroupi używajFieldSetzFieldLegenddo grupowania semantycznego.
Formularz#
Zobacz dokumentację Form, aby tworzyć formularze z komponentem Field oraz React Hook Form, Tanstack Form lub Formisch.
Przykłady#
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"
Karta wyboru#
Otaczaj komponenty Field komponentem FieldLabel, aby tworzyć wybieralne grupy pól. Działa to z komponentami RadioItem, Checkbox i Switch.
import {
Field,
FieldContent,Grupa pól#
Układaj komponenty Field za pomocą FieldGroup. Dodaj FieldSeparator, aby je oddzielić.
import { Checkbox } from "@/components/ui/checkbox"
import {
Field,RTL#
Aby włączyć obsługę RTL w Neobrutalism, zobacz przewodnik konfiguracji RTL.
"use client"
import * as React from "react"Układ responsywny#
- Pola pionowe: Domyślna orientacja układa etykietę, kontrolkę i tekst pomocy jedno pod drugim — idealne dla układów mobile-first.
- Pola poziome: Ustaw
orientation="horizontal"naField, aby wyrównać etykietę i kontrolkę obok siebie. Połącz zFieldContent, aby utrzymać wyrównanie opisów. - Pola responsywne: Ustaw
orientation="responsive", aby uzyskać automatyczne układy kolumnowe wewnątrz elementów nadrzędnych rozpoznających kontener. Zastosuj klasy@container/field-groupnaFieldGroup, aby przełączać orientacje w określonych punktach granicznych.
import { Button } from "@/components/ui/button"
import {
Field,Walidacja i błędy#
- Dodaj
data-invaliddoField, aby przełączyć cały blok w stan błędu. - Dodaj
aria-invalidna samym polu wprowadzania dla technologii wspomagających. - Renderuj
FieldErrorbezpośrednio po kontrolce lub wewnątrzFieldContent, aby utrzymać wyrównanie komunikatów o błędach z polem.
<Field data-invalid>
<FieldLabel htmlFor="email">Email</FieldLabel>
<Input id="email" type="email" aria-invalid />
<FieldError>Enter a valid email address.</FieldError>
</Field>Dostępność#
Rodzina Field stawia semantykę na pierwszym miejscu: FieldSet i FieldLegend renderują natywne fieldset/legend, Field renderuje role="group", a FieldError — role="alert", więc komunikaty walidacji są ogłaszane w chwili pojawienia się.
- Łącz każdy
FieldLabelz jego kontrolką przezhtmlFor/id— kliknięcie etykiety fokusuje kontrolkę, a czytniki ekranu ogłaszają powiązanie. - Przy nieprawidłowych danych ustawiaj
aria-invalidna samej kontrolce;data-invalidnaFieldsteruje wyłącznie stylami błędu. - Zachowania klawiaturowe należą do kontrolek w środku — opakowania nie dodają własnych punktów fokusu ani skrótów.
Referencja API#
FieldSet#
Kontener, który renderuje semantyczny fieldset z predefiniowanymi odstępami.
| Prop | Type | Default |
|---|---|---|
className | string |
<FieldSet>
<FieldLegend>Delivery</FieldLegend>
<FieldGroup>{/* Fields */}</FieldGroup>
</FieldSet>FieldLegend#
Element legendy dla FieldSet. Przełącz na wariant label, aby dopasować do rozmiaru etykiety.
| Prop | Type | Default |
|---|---|---|
variant | "legend" | "label" | "legend" |
className | string |
<FieldLegend variant="label">Notification Preferences</FieldLegend>FieldLegend ma dwa warianty: legend i label. Wariant label stosuje rozmiar i wyrównanie etykiety. Przydatne, jeśli masz zagnieżdżone FieldSet.
FieldGroup#
Wrapper układu, który układa komponenty Field i włącza zapytania kontenerowe dla responsywnych orientacji.
| Prop | Type | Default |
|---|---|---|
className | string |
<FieldGroup className="@container/field-group flex flex-col gap-6">
<Field>{/* ... */}</Field>
<Field>{/* ... */}</Field>
</FieldGroup>Field#
Podstawowy wrapper dla pojedynczego pola. Zapewnia kontrolę orientacji, stylizację stanu nieprawidłowego i odstępy.
| Prop | Type | Default |
|---|---|---|
orientation | "vertical" | "horizontal" | "responsive" | "vertical" |
className | string | |
data-invalid | boolean |
<Field orientation="horizontal">
<FieldLabel htmlFor="remember">Remember me</FieldLabel>
<Switch id="remember" />
</Field>FieldContent#
Kolumna flex, która grupuje kontrolkę i opisy, gdy etykieta znajduje się obok kontrolki. Nie jest wymagana, jeśli nie masz opisu.
| Prop | Type | Default |
|---|---|---|
className | string |
<Field>
<Checkbox id="notifications" />
<FieldContent>
<FieldLabel htmlFor="notifications">Notifications</FieldLabel>
<FieldDescription>Email, SMS, and push options.</FieldDescription>
</FieldContent>
</Field>FieldLabel#
Etykieta stylizowana zarówno dla bezpośrednich pól wprowadzania, jak i zagnieżdżonych elementów podrzędnych Field.
| Prop | Type | Default |
|---|---|---|
className | string | |
asChild | boolean | false |
<FieldLabel htmlFor="email">Email</FieldLabel>FieldTitle#
Renderuje tytuł ze stylizacją etykiety wewnątrz FieldContent.
| Prop | Type | Default |
|---|---|---|
className | string |
<FieldContent>
<FieldTitle>Enable Touch ID</FieldTitle>
<FieldDescription>Unlock your device faster.</FieldDescription>
</FieldContent>FieldDescription#
Slot na tekst pomocy, który automatycznie równoważy długie wiersze w układach poziomych.
| Prop | Type | Default |
|---|---|---|
className | string |
<FieldDescription>We never share your email with anyone.</FieldDescription>FieldSeparator#
Wizualny separator do oddzielania sekcji wewnątrz FieldGroup. Akceptuje opcjonalną zawartość inline.
| Prop | Type | Default |
|---|---|---|
className | string |
<FieldSeparator>Or continue with</FieldSeparator>FieldError#
Dostępny kontener błędów, który akceptuje elementy podrzędne lub tablicę errors (np. z react-hook-form).
| Prop | Type | Default |
|---|---|---|
errors | Array<{ message?: string } | undefined> | |
className | string |
<FieldError errors={errors.username} />Gdy tablica errors zawiera wiele komunikatów, komponent automatycznie renderuje listę.
FieldError akceptuje również problemy generowane przez dowolny walidator implementujący Standard Schema, w tym Zod, Valibot i ArkType. Przekaż tablicę issues z wyniku schematu bezpośrednio, aby wyrenderować ujednoliconą listę błędów między bibliotekami.