import type { ArraySchema, BaseSchema, EnumSchema, NullableSchema, NullishSchema, OptionalSchema } from "valibot"; import type { FormSchema } from "../form"; /** * Supported HTML input types for form fields. */ type InputType = "button" | "checkbox" | "color" | "date" | "datetime-local" | "email" | "file" | "hidden" | "image" | "month" | "number" | "password" | "radio" | "range" | "reset" | "search" | "submit" | "tel" | "text" | "time" | "url" | "week"; /** * Supported HTML input modes for virtual keyboard configuration. */ type InputMode = "none" | "text" | "search" | "email" | "tel" | "url" | "numeric" | "decimal" | undefined; /** * Attributes common to most input fields, derived from validation schemas. */ type InputAttributes = { /** The HTML input type. */ type: InputType; /** Whether the field is required. */ required: boolean; /** Minimum value for numeric or date inputs. */ min?: string | number; /** Maximum value for numeric or date inputs. */ max?: string | number; /** Minimum length for string inputs. */ minLength?: number; /** Maximum length for string inputs. */ maxLength?: number; /** Regex pattern for validation. */ pattern?: string; /** Step interval for numeric or date inputs. */ step?: string | number; /** File types accepted by file inputs. */ accept?: string; /** Whether multiple values are allowed (e.g., multiple files or emails). */ multiple?: boolean; /** Placeholder text. */ placeholder?: string; /** Hint for the virtual keyboard layout. */ inputMode?: InputMode; /** The default value for the input. */ defaultValue?: string; }; /** * Attributes specific to select elements. */ type SelectAttributes = { /** Unique identifier for the select element. */ id: string; /** Name attribute for the select element. */ name: string; /** Whether multiple options can be selected. */ multiple: boolean; /** Whether a selection is required. */ required: boolean; }; /** * Represents a basic form field with standard input attributes. */ type BaseField = { /** Unique identifier for the field. */ id: string; /** Name attribute for the field. */ name: string; } & InputAttributes; /** * Attributes for a single option in a select or radio group. */ type OptionAttributes = { /** The display name or label for the option. */ name: Name; /** The value of the option. */ value: Value; }; /** * A list of options derived from an enum schema. */ type SelectOptions = { [K in keyof T]: OptionAttributes; }[keyof T][]; /** * Attributes for a radio button input. */ type RadioAttributes = { /** Unique identifier for the radio button. */ id: string; /** Name attribute, shared by all radio buttons in the group. */ name: string; /** The value associated with this radio button. */ value: Value; /** Fixed type for radio buttons. */ type: "radio"; /** Whether the radio group is required. */ required: boolean; /** Whether this radio button is checked by default. */ checked?: boolean; }; /** * Represents a field derived from an EnumSchema, which can be rendered as radios or a select. */ type EnumField> = { /** Configuration for rendering as a group of radio buttons. */ radio: { [K in keyof S["enum"]]: RadioAttributes; }; /** Configuration for rendering as a select element. */ select: SelectAttributes; /** Options for the select element. */ options: SelectOptions; }; /** * Conditional type that determines the field representation based on the Valibot schema type. * * @remarks * - `EnumSchema` -> `EnumField` (Radios/Select) * - `Optional/Nullable/Nullish` -> Unwraps and recurses * - Other -> `BaseField` (Input) */ type FieldType = S extends EnumSchema ? EnumField : S extends OptionalSchema ? FieldType : S extends NullableSchema ? FieldType : S extends NullishSchema ? FieldType : BaseField; /** * Maps a FormSchema's entries to their corresponding field configurations. */ type FormFields = { [Key in keyof S["entries"]]: FieldType; }; /** * Hook to generate field configurations from a Valibot schema. * * @remarks * This hook analyzes the provided Valibot schema and produces a structured object containing * all necessary attributes (id, name, type, required, min/max, etc.) to render form fields. * It handles: * - Basic types (string, number, date, boolean, etc.) * - Enums (generating options for radios/selects) * - Validation constraints (min, max, length, regex, etc.) * - Default values * * @param schema - The Valibot object schema defining the form structure. * @returns An object mapping field names to their configuration. * * @example * ```tsx * import { object, string, number, enum_ } from 'valibot'; * import { useFormFields } from './hooks'; * * enum Role { Admin = 'admin', User = 'user' } * const schema = object({ * name: string(), * age: number(), * role: enum_(Role) * }); * * function MyForm() { * const fields = useFormFields(schema); * * return ( *
* * * *
* ); * } * ``` */ export declare function useFormFields(schema: S): FormFields; /** * Determines if a schema is required (not optional, nullable, or nullish). * * @param schema - The schema to check. * @returns `true` if the schema is required, `false` otherwise. */ export declare function isRequired>(schema: S): boolean; /** * Maps a Valibot schema type to a corresponding HTML input type. * * @remarks * Handles nested arrays and pipes to determine the most appropriate input type. * * @param schema - The schema to map. * @param currentType - The current resolved type (used for recursion). * @returns The HTML input type string. */ export declare function getType | ArraySchema>(schema: S, currentType?: InputType): InputType; /** * Extracts validation attributes (min, max, pattern, etc.) from a Valibot schema. * * @remarks * Iterates through the schema's pipe and other properties to find constraints that can be * represented as HTML attributes. * * @param schema - The schema to extract attributes from. * @returns An object containing partial InputAttributes. */ export declare function getAttributes>(schema: any): Partial; export {};