Skip to content

Field

Form

A render-prop field wrapper connecting labels, hints, and errors.

Required fieldInline validationHint and disabled control
Open preview

Responsive viewport — resize the window to explore. Direction and density apply only to this preview. Theme applies to the docs too.

Usage

Render a native labelable control through the children function and spread the provided control props onto it. The wrapper creates stable ids and description/error links. Keep value and validation in the consuming form.

Import
import { Field } from "@/components/ui/field";

Shipped as editable source in src/components/ui/field.tsx in both apps. Copy the implementation below when moving it to another project. Copy the complete example from Source code for all imports and state. Add "use client" when using hooks or event handlers. See installation and theming for project setup.

Component source

The complete implementation is included in both apps. It uses the React, Radix, Lucide, and shared-core dependencies already installed in Apex. Keep your project’s semantic theme tokens when reusing it.

src/components/ui/field.tsx
"use client";
import { useId, type ReactNode } from "react";
import { Label } from "@dashboardpack/core/components/ui/label";
import { cn } from "@dashboardpack/core/lib/utils";
export interface FieldControlProps {
  id: string;
  required?: boolean;
  "aria-describedby"?: string;
  "aria-invalid"?: boolean;
}
export interface FieldProps {
  label: ReactNode;
  children: (control: FieldControlProps) => ReactNode;
  id?: string;
  hint?: ReactNode;
  error?: ReactNode;
  required?: boolean;
  className?: string;
}
export function Field({
  label,
  children,
  id,
  hint,
  error,
  required,
  className,
}: FieldProps) {
  const generated = useId();
  const controlId = id ?? generated;
  const description =
    [hint ? `${controlId}-hint` : "", error ? `${controlId}-error` : ""]
      .filter(Boolean)
      .join(" ") || undefined;
  return (
    <div className={cn("space-y-2", className)}>
      <Label htmlFor={controlId}>
        {label}
        {required && (
          <span aria-hidden className="ms-1 text-destructive">
            *
          </span>
        )}
      </Label>
      {children({
        id: controlId,
        required,
        "aria-describedby": description,
        "aria-invalid": error ? true : undefined,
      })}
      {hint && (
        <p
          id={`${controlId}-hint`}
          className="text-xs leading-relaxed text-muted-foreground"
        >
          {hint}
        </p>
      )}
      {error && (
        <p
          role="alert"
          id={`${controlId}-error`}
          className="text-xs text-destructive"
        >
          {error}
        </p>
      )}
    </div>
  );
}

API reference

Common props and defaults. Slot-specific props are named below; native elements also accept their standard HTML attributes. The component TypeScript definitions are the complete type reference.

Field props
PropType / DefaultDescription
labelReactNode

Default: required

Visible control label.
children(control: FieldControlProps) => ReactNode

Default: required

Render and spread the supplied control props.
hint / errorReactNode

Default: —

Supporting instructions or validation failure.
requiredboolean

Default: false

Mark the label and control as required.
idstring

Default: generated

Optional stable control id.

Build with these too