HapplyUI

Contexts

Form Field Context

Shared React context that propagates FormField state (hasError, disabled, id, name) to child input components. Automatically installed with FormField and consumed by all input components.


Installation

bunx @happlyui/cli@latest add form-field-context

Usage

import { useFormField } from "@/lib/form-field-context"

function MyCustomInput({ hasError, ...props }) {
  const formField = useFormField();
  const resolvedHasError = hasError ?? formField.hasError;

  return <input aria-invalid={resolvedHasError || undefined} {...props} />;
}

API Reference

useFormField

Hook to access FormField context values. Returns default values when used outside a FormField.

PropTypeDefaultDescription

FormFieldContext

The React context object. Use FormFieldContext.Provider to wrap custom form field implementations.

PropTypeDefaultDescription
hasErrorbooleanfalseWhether the field has a validation error.
disabledbooleanfalseWhether the field is disabled.
idstring-The field ID, used for label/input association.
namestring-The field name, used for react-hook-form integration.

Previous
useTabObserver