UNPKG

@payfit/unity-components

Version:

73 lines (72 loc) • 3.17 kB
import { ReactNode } from 'react'; import { LabelProps } from '../../../../core/components/label/Label.js'; import { CheckboxGroupProps } from '../checkbox-group/CheckboxGroup.js'; export interface FieldProps extends Pick<LabelProps, 'isRequired' | 'requiredVariant'> { /** Helper text to display below the group. */ helperText?: ReactNode; /** A contextual link to display below the group. */ contextualLink?: ReactNode; } export type CheckboxGroupFieldProps = FieldProps & CheckboxGroupProps; /** * `CheckboxGroupField` is a composite field for Form that renders a * complete checkbox group (label, helper/error, group) and automatically connects to * the field context provided by `useUnityForm` and `<form.AppField name="…">`. * * Behavior: * - Group rendering: relies on `CheckboxGroup` to manage the value (array of * strings), focus, and events. * - Messaging: renders helper text (`FormHelperText` via the `helperText` prop) * and error feedback (`FormFeedbackText`) according to the form state. * - Contextual link: allows displaying an additional link below the field via the * `contextualLink` prop. * * Accessibility: * - ARIA attributes (`aria-labelledby`, `aria-describedby`, `aria-details`) are wired by * `FormField` and its sub-parts to associate the label, helper, and feedback * with the control. * * Key props: * - `label: string` — group label text. * - `helperText?: ReactNode` — helper text displayed above the feedback. * - `contextualLink?: ReactNode` — contextual link (referenced via `aria-details`). * - Inherits the props from `CheckboxGroup` (except direct value management). * - `isRequired?`, `requiredVariant?` — control how the required/optional indicator is displayed. * * Example: * ```tsx * import { CheckboxGroupField } from '@/components/checkbox-group-field/CheckboxGroupField' * import { Checkbox } from '@/components/checkbox/Checkbox' * import { useUnityForm } from '../../hooks/use-form.js' * import * as z from 'zod' * * const schema = z.object({ * preferences: z.array(z.enum(['email','sms','push'])).min(1), * }) * * function Example() { * const form = useUnityForm({ validators: { onBlur: schema } }) * return ( * <form.AppForm> * <form.Form> * <form.AppField name="preferences"> * {() => ( * <CheckboxGroupField label="Preferences" helperText="Choose at least one option"> * <Checkbox value="email">Email</Checkbox> * <Checkbox value="sms">SMS</Checkbox> * <Checkbox value="push">Push</Checkbox> * </CheckboxGroupField> * )} * </form.AppField> * </form.Form> * </form.AppForm> * ) * } * ``` * * Migration notes from a non‑TanStack field: * - Do not pass a `name` prop to the component — use `<form.AppField name="…">`. * - `value`, `defaultValue`, and `isInvalid` are derived from the TanStack context. */ declare const CheckboxGroupField: import('react').ForwardRefExoticComponent<FieldProps & CheckboxGroupProps & import('react').RefAttributes<HTMLDivElement>>; export { CheckboxGroupField };