@payfit/unity-components
Version:
53 lines (52 loc) • 2.29 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 { LabelProps } from '../label/Label.js';
import { SelectProps } from '../select/Select.js';
interface SelectFieldProps<TItems extends object, TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>> extends Pick<LabelProps, 'isRequired' | 'requiredVariant'>, Omit<SelectProps<TItems>, 'name' | 'value'> {
/** The name of the field, which should match the form schema. */
name: TName;
/** The label for the select field. */
label: string;
/** Helper text to display below the text field. */
helperText?: ReactNode;
/** Feedback text to display below the text field. */
feedbackText?: ReactNode;
}
type SelectFieldComponent = (<TItems extends object, TSchema extends Schema>(props: SelectFieldProps<TItems, StandardSchemaV1.InferOutput<TSchema>> & {
ref?: ForwardedRef<HTMLDivElement>;
}) => JSX.Element) & {
displayName?: string;
};
/**
* The `SelectField` component renders a list of options to select one, it is fully integrated with the `Form` component.
* @example
* ```tsx
* const schema = z.object({
* vegetable: z.string()
* })
*
* function MyForm() {
* const { Form } = useUnityForm(schema)
*
* return (
* <Form action={handleSubmit}>
* <SelectField<{id?: string, name:string}, typeof schema>
* name="vegetable"
* label="Vegetables"
* >
* <SelectOption id="Cabbage">Cabbage</SelectOption>
* <SelectOption id="Broccoli">Broccoli</SelectOption>
* <SelectOption id="Carrots">Carrots</SelectOption>
* </SelectField>
* </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 SelectField: SelectFieldComponent;
export { SelectField };