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.

155 lines (154 loc) 6.48 kB
import { CustomComponentIds, CustomOnChangeProps, StrObjOption } from "../../types/common.js"; import { AutoCompleteTextFieldProps, CheckboxProps, FormControlLabelProps, FormHelperTextProps, FormLabelProps, MuiChipProps } from "../../types/mui.js"; import { JSX, ReactNode, Ref } from "react"; import { Control, FieldValues, Path, RegisterOptions } from "react-hook-form"; import { AutocompleteProps, AutocompleteRenderOptionState } from "@mui/material/Autocomplete"; //#region src/mui/multi-autocomplete/index.d.ts type MultiAutoCompleteProps<Option extends StrObjOption = StrObjOption, DisableClearable extends boolean = false, FreeSolo extends boolean = false> = Omit<AutocompleteProps<Option, true, DisableClearable, FreeSolo>, 'freeSolo' | 'fullWidth' | 'renderInput' | 'renderOption' | 'options' | 'value' | 'defaultValue' | 'multiple' | 'onChange' | 'getOptionKey' | 'getOptionLabel' | 'isOptionEqualToValue' | 'blurOnSelect' | 'disableClearable' | 'disableCloseOnSelect' | 'ChipProps' | 'ref'>; type OnValueChangeProps = { newValue: string[]; selectedOption?: string; }; type RHFMultiAutocompleteProps<T extends FieldValues, Option extends StrObjOption = StrObjOption, LabelKey extends Extract<keyof Option, string> = Extract<keyof Option, string>, ValueKey extends Extract<keyof Option, string> = Extract<keyof Option, string>, DisableClearable extends boolean = false, FreeSolo 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>>; /** * Options rendered by the field. */ options: Option[]; /** * Object key used to read the display label from each option. */ labelKey?: LabelKey; /** * Object key used to derive the stored field value when options are an array of objects. */ valueKey?: ValueKey; /** * When true, the user may type any value not present in `options`. * * The typed string is stored in RHF state as-is. `selectedOption` in * callbacks is of type `(Option | string)[]`. * * To keep things predictable and type-safe, `freeSolo` is not compatible with * `selectAllText` and will hide the "Select All" option. */ freeSolo?: FreeSolo; /** * Overrides the default multi-autocomplete change handling. * Receives the next string array and the option value that triggered the change. * Call `rhfOnChange` with the string array that should be stored; else the form value will not be updated. * * @param rhfOnChange - React Hook Form field change handler for the selected value array. * @param newValue - Next selected string value array. * @param selectedOption - Option value that triggered the change, or the select-all sentinel. */ customOnChange?: ({ rhfOnChange, newValue, selectedOption }: CustomOnChangeProps<OnValueChangeProps, string[]>) => void; /** * Called after the default multi-autocomplete handler stores the next string array in React Hook Form. * * ⚠️ Important: * This callback is not called when `customOnChange` is used. * * @param newValue - Next selected string value array. * @param selectedOption - Option value that triggered the change, or the select-all sentinel. */ onValueChange?: ({ newValue, selectedOption }: OnValueChangeProps) => void; /** * When true, the selected value cannot be cleared from the input. * @default false */ disableClearable?: DisableClearable; /** * Text to display for the "Select All" option. */ selectAllText?: string; /** * When true, hides the select-all option. */ hideSelectAllOption?: boolean; /** * Label content shown for the field. Defaults to a label generated from `fieldName`. */ label?: ReactNode; /** * Custom renderer for an option label. */ renderOptionLabel?: (option: Option, state: AutocompleteRenderOptionState) => 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; /** * Props forwarded to each internal MUI `Checkbox`. */ checkboxProps?: CheckboxProps; /** * Props forwarded to each internal MUI `FormControlLabel`. */ formControlLabelProps?: FormControlLabelProps; /** * 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; } & MultiAutoCompleteProps<Option, DisableClearable, FreeSolo>; declare const RHFMultiAutocomplete: <T extends FieldValues, Option extends StrObjOption = StrObjOption, LabelKey extends Extract<keyof Option, string> = Extract<keyof Option, string>, ValueKey extends Extract<keyof Option, string> = Extract<keyof Option, string>, DisableClearable extends boolean = false, FreeSolo extends boolean = false>(props: RHFMultiAutocompleteProps<T, Option, LabelKey, ValueKey, DisableClearable, FreeSolo> & { ref?: Ref<HTMLInputElement>; }) => JSX.Element; //#endregion export { RHFMultiAutocompleteProps, RHFMultiAutocomplete as default };