field
lyse/fieldFreeComponent
lyse publishes no description for this item.
Live preview
live · rendered by the registry
Rendered by lyse on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://ui.getlyse.com/r/field.jsonSource
1import * as React from "react"2import { Slot } from "@radix-ui/react-slot"34import { cn } from "@/lib/utils"5import "./field.css"67/* ------------------------------------------------------------------ */8/* Context */9/* ------------------------------------------------------------------ */1011interface FieldContextValue {12 id: string13 error: boolean14 disabled: boolean15 required: boolean16}1718const FieldContext = React.createContext<FieldContextValue | null>(null)1920function useFieldContext() {21 const context = React.useContext(FieldContext)22 if (context === null && process.env.NODE_ENV !== "production") {23 console.warn(24 "Field compound components (FieldLabel, FieldControl, FieldDescription, FieldError) must be rendered inside a <Field>. Falling back to safe defaults."25 )26 }27 return (28 context ?? { id: "", error: false, disabled: false, required: false }29 )30}3132/* ------------------------------------------------------------------ */33/* Field */34/* ------------------------------------------------------------------ */3536function Field({37 className,38 id: idProp,39 error = false,40 disabled = false,41 required = false,42 children,43 ...props44}: React.ComponentProps<"div"> & {45 error?: boolean46 disabled?: boolean47 required?: boolean48}) {49 const generatedId = React.useId()50 const id = idProp ?? generatedId5152 const contextValue = React.useMemo(53 () => ({ id, error, disabled, required }),54 [id, error, disabled, required]55 )5657 return (58 <FieldContext value={contextValue}>59 <div60 data-slot="field"61 className={cn(62 "flex flex-col gap-[var(--layout-gap-md)] w-full",63 className64 )}65 {...props}66 >67 {children}68 </div>69 </FieldContext>70 )71}7273/* ------------------------------------------------------------------ */74/* FieldLabel */75/* ------------------------------------------------------------------ */7677function FieldLabel({78 className,79 htmlFor: htmlForProp,80 children,81 ...props82}: React.ComponentProps<"label">) {83 const { id, required, disabled } = useFieldContext()8485 return (86 <label87 data-slot="field-label"88 htmlFor={htmlForProp ?? (id || undefined)}89 data-disabled={disabled || undefined}90 className={cn(91// … truncated
What it pulls in
npm packages
Other registry items
Files it writes
More from lyse
All 33 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| action-cardaction-card | lyse | Components | Free | no deps · 2 files | |
| alertalert | lyse | Components | Free | 2 deps · 2 files | |
| alert-dialogalert-dialog | lyse | Components | Free | 2 deps · 2 files | |
| avataravatar | lyse | Components | Free | 3 deps · 2 files | |
| badgebadge | lyse | Components | Free | 1 dep · 2 files | |
| banner-infobanner-info | lyse | Components | Free | 2 deps · 2 files | |
| breadcrumbbreadcrumb | lyse | Components | Free | 2 deps · 2 files | |
| buttonbutton | lyse | Components | Free | 2 deps · 2 files |
