import { AutocompleteOptionRenderState, CustomOnChangeProps } from "../../common/types/components.js"; import { AutoCompleteTextFieldProps, FormHelperTextProps, FormLabelProps, MuiChipProps } from "../../common/types/mui.js"; import "../../common/index.js"; import { JSX, ReactNode, Ref, SyntheticEvent } from "react"; import { Control, FieldError, FieldValues, Path, RegisterOptions } from "react-hook-form"; import { AutocompleteChangeDetails, AutocompleteChangeReason, AutocompleteProps } from "@mui/material/Autocomplete"; import { CountryDetails, CountryISO, countryList } from "@nish1896/mui-components/mui/country-select"; import { CustomComponentIds } from "@nish1896/mui-components/types"; //#region src/mui/country-select/index.d.ts type CountrySelectStoredPrimitive = CountryDetails[keyof Omit]; type CountrySelectStoredItem = CountryDetails | CountrySelectStoredPrimitive; type CountrySelectStoredValue = [Multiple] extends [true] ? CountrySelectStoredItem[] : [DisableClearable] extends [true] ? CountrySelectStoredItem : CountrySelectStoredItem | null; type OnValueChangeProps = { newValue: CountrySelectStoredValue; event: SyntheticEvent; reason: AutocompleteChangeReason; details?: AutocompleteChangeDetails; }; type AutoCompleteProps = Omit, 'freeSolo' | 'multiple' | 'fullWidth' | 'renderInput' | 'renderOption' | 'options' | 'value' | 'defaultValue' | 'onChange' | 'getOptionLabel' | 'isOptionEqualToValue' | 'blurOnSelect' | 'disableClearable' | 'disableCloseOnSelect' | 'ChipProps' | 'loading' | 'ref'>; type RHFCountrySelectProps = { /** * Name/path of the React Hook Form field this component controls. */ fieldName: Path; /** * React Hook Form control object returned by `useForm`. */ control: Control; /** * Validation rules passed to React Hook Form for this field. */ registerOptions?: RegisterOptions>; /** * 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 exposed using the * specified country property. * - When `valueKey` is omitted, selected value(s) are exposed as complete * country objects. */ valueKey?: keyof Omit; /** * 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, CountrySelectStoredValue>) => 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) => 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 label above the component instead of within the field layout. */ showLabelAboveFormField?: boolean; /** * Props forwarded to the internal `FormLabel`. The `id` is managed by the component. */ formLabelProps?: Omit; /** * 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. * `state` carries MUI's option state (`selected`, `index`, `inputValue`) plus * a `disabled` flag, so the label can react to the option's status. */ renderOptionLabel?: (option: CountryDetails, state: AutocompleteOptionRenderState) => ReactNode; /** * When true, marks the field as required in the UI and accessibility attributes. */ required?: boolean; /** * Custom renderer for the React Hook Form field error. * Receives the current field error and must return renderable content, such as `error.message` or a custom element. * * @param error - React Hook Form field error for this field. */ renderError?: (error: FieldError) => ReactNode; /** * If true, hides the error message text while keeping the field in an error state. */ hideErrorMessage?: boolean; /** * Helper text shown below the field when there is no visible validation error. */ helperText?: ReactNode; /** * Props forwarded to the internal `FormHelperText`. The `id` is managed by the component. */ formHelperTextProps?: Omit; /** * 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; /** * Controlled country picker built on Material UI `Autocomplete`, wired to a * React Hook Form field via `control`. * * Renders flags and supports pinning preferred countries to the top of the list. * * Docs: [RHFCountrySelect](https://rhf-mui-components.vercel.app/components/mui/RHFCountrySelect) * * API: [RHFCountrySelectProps](https://rhf-mui-components.vercel.app/components/mui/RHFCountrySelect#api) */ declare const RHFCountrySelect: (props: RHFCountrySelectProps & { ref?: Ref; }) => JSX.Element; //#endregion export { type CountryDetails, type CountryISO, RHFCountrySelectProps, countryList, RHFCountrySelect as default };