@payfit/unity-components
Version:
62 lines (61 loc) • 2.86 kB
TypeScript
import { StandardSchemaV1 } from '@standard-schema/spec';
import { ForwardedRef, JSX, ReactNode } from 'react';
import { FieldPath, FieldValues } from 'react-hook-form';
import { Schema } from '../../hooks/use-form.types.js';
import { SelectableButtonGroupProps } from '../selectable-button-group/SelectableButtonGroup.js';
import { LabelProps } from '../label/Label.js';
interface FieldProps<TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>> {
/** The name of the field, which should match the form schema. */
name: TName;
/** The label for the field. */
label: string;
/** The required variant for the label. */
requiredVariant?: LabelProps['requiredVariant'];
/** Helper text to display below the field. */
helperText?: ReactNode;
/** A contextual link to display below the field. */
contextualLink?: ReactNode;
/** Whether the checkbox group is disabled. */
isDisabled?: boolean;
/** Whether the checkbox group is invalid. */
isInvalid?: boolean;
}
export type SelectableButtonGroupFieldProps<TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>> = FieldProps<TFieldValues, TName> & Omit<SelectableButtonGroupProps, 'isDisabled' | 'children'> & {
/** The options to render as SelectableButton children. */
children: ReactNode;
};
type SelectableButtonGroupFieldComponent = (<TSchema extends Schema>(props: SelectableButtonGroupFieldProps<StandardSchemaV1.InferOutput<TSchema>> & {
ref?: ForwardedRef<HTMLInputElement | HTMLTextAreaElement>;
}) => JSX.Element) & {
displayName?: string;
};
/**
* The `SelectableButtonGroupField` component integrates the SelectableButtonGroup with Unity's form system, providing validation, accessibility, and helper/feedback text.
* @example
* ```tsx
* const schema = z.object({
* choice: z.string()
* })
*
* function MyForm() {
* const { Form } = useUnityForm(schema)
*
* return (
* <Form action={handleSubmit}>
* <SelectableButtonGroupField<typeof schema>
* name="choice"
* label="Choose an option"
* >
* <SelectableButton value="a">A</SelectableButton>
* <SelectableButton value="b">B</SelectableButton>
* </SelectableButtonGroupField>
* </Form>
* )
* }
* ```
* @note The schema type parameter is needed to ensure type safety with the form's schema. If you omit it, the `name` prop will not be type-safe.
* @deprecated React Hook Form components are deprecated. Use the TanStack Form version instead.
* @see Storybook docs: https://unity-components.payfit.io/?path=/docs/forms-introduction-to-unity-forms--docs
*/
declare const SelectableButtonGroupField: SelectableButtonGroupFieldComponent;
export { SelectableButtonGroupField };