UNPKG

@nish1896/rhf-mui-components

Version:

A suite of 25+ production-ready react-hook-form components built with material-ui. Fully typed, tree-shakable, and optimized for enterprise-grade forms.

159 lines (158 loc) 6.93 kB
import { CustomComponentIds, CustomOnChangeProps } from "../../types/common.js"; import { CountryDetails, CountryISO } from "../../types/countries.js"; import { AutoCompleteTextFieldProps, FormHelperTextProps, FormLabelProps, MuiChipProps } from "../../types/mui.js"; import { countryList } from "./countries.js"; import { JSX, ReactNode, Ref, SyntheticEvent } from "react"; import { Control, FieldValues, Path, RegisterOptions } from "react-hook-form"; import { AutocompleteChangeDetails, AutocompleteChangeReason, AutocompleteProps } from "@mui/material/Autocomplete"; //#region src/mui/country-select/index.d.ts type CountrySelectStoredPrimitive = CountryDetails[keyof Omit<CountryDetails, 'emoji'>]; type CountrySelectStoredItem = CountryDetails | CountrySelectStoredPrimitive; type CountrySelectStoredValue<Multiple extends boolean, DisableClearable extends boolean> = [Multiple] extends [true] ? CountrySelectStoredItem[] : [DisableClearable] extends [true] ? CountrySelectStoredItem : CountrySelectStoredItem | null; type OnValueChangeProps<Multiple extends boolean, DisableClearable extends boolean> = { newValue: CountrySelectStoredValue<Multiple, DisableClearable>; event: SyntheticEvent; reason: AutocompleteChangeReason; details?: AutocompleteChangeDetails<CountryDetails>; }; type AutoCompleteProps<Multiple extends boolean = false, DisableClearable extends boolean = false> = Omit<AutocompleteProps<CountryDetails, Multiple, DisableClearable, false>, 'freeSolo' | 'multiple' | 'fullWidth' | 'renderInput' | 'renderOption' | 'options' | 'value' | 'defaultValue' | 'onChange' | 'getOptionLabel' | 'isOptionEqualToValue' | 'blurOnSelect' | 'disableClearable' | 'disableCloseOnSelect' | 'ChipProps' | 'loading' | 'ref'>; type RHFCountrySelectProps<T extends FieldValues, Multiple extends boolean = false, DisableClearable extends boolean = false> = { /** * Name/path of the React Hook Form field this component controls. */ fieldName: Path<T>; /** * React Hook Form control object returned by `useForm`. */ control: Control<T>; /** * Validation rules passed to React Hook Form for this field. */ registerOptions?: RegisterOptions<T, Path<T>>; /** * List of countries to display in the country selector. * * Defaults to all countries from `countryList`. */ countries?: CountryDetails[]; /** * When true, allows selecting multiple countries. */ multiple?: Multiple; /** * List of country ISO codes to pin at the top of the dropdown. * * Countries are displayed in the same order as provided in this array, * followed by the remaining countries sorted in their default order. * * @example * preferredCountries={['US', 'CA', 'IN']} */ preferredCountries?: CountryISO[]; /** * - When `valueKey` is provided, selected value(s) are stored using the * specified country property. * - When `valueKey` is omitted, selected value(s) are stored as complete * country objects. */ valueKey?: keyof Omit<CountryDetails, 'emoji'>; /** * Overrides the default country select change handling. * Receives the normalized country value plus the raw MUI Autocomplete change metadata. * Call `rhfOnChange` with the country value that should be stored; else the form value will not be updated. * * @param rhfOnChange - React Hook Form field change handler for the stored country value. * @param newValue - Normalized country value, or country value array when `multiple` is true. * @param event - Original MUI Autocomplete change event. * @param reason - MUI Autocomplete reason for the change. * @param details - Additional MUI Autocomplete change details, when available. */ customOnChange?: ({ rhfOnChange, newValue, event, reason, details }: CustomOnChangeProps<OnValueChangeProps<Multiple, DisableClearable>, CountrySelectStoredValue<Multiple, DisableClearable>>) => void; /** * Called after the default country select handler stores the normalized country value in React Hook Form. * * ⚠️ Important: * This callback is not called when `customOnChange` is used. * * @param newValue - Normalized country value, or country value array when `multiple` is true. * @param event - Original MUI Autocomplete change event. * @param reason - MUI Autocomplete reason for the change. * @param details - Additional MUI Autocomplete change details, when available. */ onValueChange?: ({ newValue, event, reason, details }: OnValueChangeProps<Multiple, DisableClearable>) => void; /** * When true, the selected value cannot be cleared from the input. * @default false */ disableClearable?: DisableClearable; /** * Label content shown for the field. Defaults to a label generated from `fieldName`. */ label?: ReactNode; /** * When true, renders the field label above the form field instead of inside or beside it. */ showLabelAboveFormField?: boolean; /** * Props forwarded to the internal `FormLabel`. The `id` is managed by the component. */ formLabelProps?: Omit<FormLabelProps, 'id'>; /** * When true, hides the rendered field label while preserving accessible labeling where possible. */ hideLabel?: boolean; /** * Customize how each country option is displayed in the dropdown menu. */ renderOptionLabel?: (option: CountryDetails) => ReactNode; /** * When true, marks the field as required in the UI and accessibility attributes. */ required?: boolean; /** * Helper text shown below the field when there is no visible validation error. */ helperText?: ReactNode; /** * @deprecated * Field error message is now automatically derived from form state. * Passing this prop is no longer necessary and it will be removed in the next major version. */ errorMessage?: ReactNode; /** * If true, hides the error message text while keeping the field in an error state. */ hideErrorMessage?: boolean; /** * Props forwarded to the internal `FormHelperText`. The `id` is managed by the component. */ formHelperTextProps?: Omit<FormHelperTextProps, 'id'>; /** * Props forwarded to the internal MUI `TextField`. */ textFieldProps?: AutoCompleteTextFieldProps; /** * Props forwarded to chips rendered for selected values. */ ChipProps?: MuiChipProps; /** * Custom ids for generated field, label, helper text, and error elements. */ customIds?: CustomComponentIds; } & AutoCompleteProps<Multiple, DisableClearable>; declare const RHFCountrySelect: <T extends FieldValues, Multiple extends boolean = false, DisableClearable extends boolean = false>(props: RHFCountrySelectProps<T, Multiple, DisableClearable> & { ref?: Ref<HTMLInputElement>; }) => JSX.Element; //#endregion export { type CountryDetails, type CountryISO, RHFCountrySelectProps, countryList, RHFCountrySelect as default };