{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "field",
  "type": "registry:ui",
  "title": "Field",
  "description": "Lays out a label, a control, a description and an error message, and connects them (htmlFor, id, aria-describedby, aria-invalid) without hand-written ids.",
  "categories": [
    "ui"
  ],
  "dependencies": [
    "radix-ui"
  ],
  "registryDependencies": [
    "@rivier-ui/theme",
    "@rivier-ui/cn",
    "@rivier-ui/label"
  ],
  "files": [
    {
      "path": "registry/ui/field.tsx",
      "type": "registry:ui",
      "target": "components/ui/field.tsx",
      "content": "\"use client\";\n/**\n * @name field\n * @category ui\n * @description Lays out a label, a control, a description and an error message, and connects them (htmlFor, id, aria-describedby, aria-invalid) without hand-written ids.\n * @when Every control in a form. Put the control inside FieldControl; render FieldError only when there is a message and the field turns invalid by itself. Use orientation=\"horizontal\" for a checkbox or switch beside its label, and FieldControl group for a radio group, toggle group or slider.\n * @example\n * <Field>\n *   <FieldLabel>Email</FieldLabel>\n *   <FieldControl><Input type=\"email\" /></FieldControl>\n *   <FieldDescription>We only use it to reply.</FieldDescription>\n *   <FieldError>{error}</FieldError>\n * </Field>\n * <Field orientation=\"horizontal\">\n *   <FieldControl><Switch /></FieldControl>\n *   <FieldLabel>Weekly summary</FieldLabel>\n * </Field>\n * <Field>\n *   <FieldLabel>Plan</FieldLabel>\n *   <FieldControl group>\n *     <RadioGroup defaultValue=\"pro\">\n *       <Label><RadioGroupItem value=\"free\" /> Free</Label>\n *       <Label><RadioGroupItem value=\"pro\" /> Pro</Label>\n *     </RadioGroup>\n *   </FieldControl>\n * </Field>\n */\nimport { Slot } from \"radix-ui\";\nimport { createContext, use, useEffect, useId, useMemo, useState, type ComponentProps, type Dispatch, type SetStateAction } from \"react\";\nimport { Label } from \"@/components/ui/label\";\nimport { cn } from \"@/lib/cn\";\n\ntype FieldContextValue = {\n  /** The control's id; the label points at it. */\n  id: string;\n  labelId: string;\n  descriptionId: string;\n  errorId: string;\n  /** True when `invalid` is set on the field, or a FieldError with a message is rendered. */\n  invalid: boolean;\n  hasDescription: boolean;\n  hasError: boolean;\n  setHasDescription: Dispatch<SetStateAction<boolean>>;\n  setHasError: Dispatch<SetStateAction<boolean>>;\n};\n\nconst FieldContext = createContext<FieldContextValue | null>(null);\n\n/** The ids and state of the surrounding field, for wiring a control that FieldControl cannot wrap. */\nexport function useField(): FieldContextValue {\n  const field = use(FieldContext);\n  if (!field) throw new Error(\"Field parts (FieldLabel, FieldControl, FieldDescription, FieldError) must be inside <Field>.\");\n  return field;\n}\n\nexport type FieldProps = Omit<ComponentProps<\"div\">, \"id\"> & {\n  /** The id given to the control. Generated when left out. */\n  id?: string;\n  /** Force the invalid state. Left out, the field is invalid while a FieldError with a message is rendered. */\n  invalid?: boolean;\n  /** \"vertical\": label above the control. \"horizontal\": a small control (checkbox, switch) beside its label. */\n  orientation?: \"vertical\" | \"horizontal\";\n};\n\nexport function Field({ className, id: idProp, invalid: invalidProp, orientation = \"vertical\", ...props }: FieldProps) {\n  const generatedId = useId();\n  const id = idProp ?? generatedId;\n  const [hasDescription, setHasDescription] = useState(false);\n  const [hasError, setHasError] = useState(false);\n  const invalid = invalidProp ?? hasError;\n  const value = useMemo<FieldContextValue>(\n    () => ({\n      id,\n      labelId: `${id}-label`,\n      descriptionId: `${id}-description`,\n      errorId: `${id}-error`,\n      invalid,\n      hasDescription,\n      hasError,\n      setHasDescription,\n      setHasError,\n    }),\n    [id, invalid, hasDescription, hasError],\n  );\n  return (\n    <FieldContext value={value}>\n      <div\n        data-slot=\"field\"\n        data-orientation={orientation}\n        data-invalid={invalid || undefined}\n        className={cn(\n          \"group/field min-w-0\",\n          orientation === \"vertical\" ? \"flex flex-col gap-2\" : \"grid grid-cols-[auto_minmax(0,1fr)] items-center gap-x-3 gap-y-1\",\n          className,\n        )}\n        {...props}\n      />\n    </FieldContext>\n  );\n}\n\nexport function FieldLabel({ className, ...props }: ComponentProps<typeof Label>) {\n  const { id, labelId } = useField();\n  return <Label data-slot=\"field-label\" id={labelId} htmlFor={id} className={className} {...props} />;\n}\n\nexport type FieldControlProps = ComponentProps<typeof Slot.Root> & {\n  /**\n   * For a control that is a group rather than one focusable element (radio group, toggle group, slider):\n   * names it with aria-labelledby, since a label's htmlFor cannot point at a group.\n   */\n  group?: boolean;\n};\n\n/** Passes the field's id, aria-describedby and aria-invalid to its one child: the control. Renders no element. */\nexport function FieldControl({ group = false, ...props }: FieldControlProps) {\n  const { id, labelId, descriptionId, errorId, invalid, hasDescription, hasError } = useField();\n  const describedBy = [hasDescription ? descriptionId : null, hasError ? errorId : null].filter(Boolean).join(\" \");\n  return (\n    <Slot.Root\n      id={id}\n      aria-labelledby={group ? labelId : undefined}\n      aria-describedby={describedBy || undefined}\n      aria-invalid={invalid || undefined}\n      {...props}\n    />\n  );\n}\n\nexport function FieldDescription({ className, ...props }: ComponentProps<\"p\">) {\n  const { descriptionId, setHasDescription } = useField();\n  useEffect(() => {\n    setHasDescription(true);\n    return () => setHasDescription(false);\n  }, [setHasDescription]);\n  return (\n    <p\n      data-slot=\"field-description\"\n      id={descriptionId}\n      className={cn(\"text-sm text-muted-foreground group-data-[orientation=horizontal]/field:col-start-2\", className)}\n      {...props}\n    />\n  );\n}\n\n/** Renders nothing without a message, so it can stay in the markup: `<FieldError>{errors.email}</FieldError>`. */\nexport function FieldError({ className, children, ...props }: ComponentProps<\"p\">) {\n  const { errorId, setHasError } = useField();\n  const present = children !== null && children !== undefined && children !== false && children !== \"\";\n  useEffect(() => {\n    if (!present) return;\n    setHasError(true);\n    return () => setHasError(false);\n  }, [present, setHasError]);\n  if (!present) return null;\n  return (\n    <p\n      data-slot=\"field-error\"\n      id={errorId}\n      role=\"alert\"\n      className={cn(\"text-sm text-destructive group-data-[orientation=horizontal]/field:col-start-2\", className)}\n      {...props}\n    >\n      {children}\n    </p>\n  );\n}\n"
    }
  ],
  "meta": {
    "category": "ui",
    "when": "Every control in a form. Put the control inside FieldControl; render FieldError only when there is a message and the field turns invalid by itself. Use orientation=\"horizontal\" for a checkbox or switch beside its label, and FieldControl group for a radio group, toggle group or slider.",
    "example": "<Field>\n  <FieldLabel>Email</FieldLabel>\n  <FieldControl><Input type=\"email\" /></FieldControl>\n  <FieldDescription>We only use it to reply.</FieldDescription>\n  <FieldError>{error}</FieldError>\n</Field>\n<Field orientation=\"horizontal\">\n  <FieldControl><Switch /></FieldControl>\n  <FieldLabel>Weekly summary</FieldLabel>\n</Field>\n<Field>\n  <FieldLabel>Plan</FieldLabel>\n  <FieldControl group>\n    <RadioGroup defaultValue=\"pro\">\n      <Label><RadioGroupItem value=\"free\" /> Free</Label>\n      <Label><RadioGroupItem value=\"pro\" /> Pro</Label>\n    </RadioGroup>\n  </FieldControl>\n</Field>",
    "client": true
  }
}
