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.

146 lines (145 loc) 6.22 kB
import { CustomComponentIds, CustomOnChangeProps, KeyValueOption } from "../../types/common.js"; import { AutoCompleteTextFieldProps, CheckboxProps, FormControlLabelProps, FormHelperTextProps, FormLabelProps, MuiChipProps } from "../../types/mui.js"; import { selectAllOptionValue } from "../../common/constants.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-object/index.d.ts type MultiAutoCompleteProps<Option extends KeyValueOption = KeyValueOption, DisableClearable extends boolean = false> = Omit<AutocompleteProps<Option, true, DisableClearable, false>, 'freeSolo' | 'fullWidth' | 'renderInput' | 'renderOption' | 'options' | 'value' | 'defaultValue' | 'multiple' | 'onChange' | 'getOptionKey' | 'getOptionLabel' | 'isOptionEqualToValue' | 'blurOnSelect' | 'disableClearable' | 'disableCloseOnSelect' | 'ChipProps' | 'ref'>; type OnValueChangeProps<Option extends KeyValueOption = KeyValueOption> = { newValue: Option[]; selectedOption?: Option | typeof selectAllOptionValue; }; type RHFMultiAutocompleteObjectProps<T extends FieldValues, Option extends KeyValueOption = KeyValueOption, LabelKey extends Extract<keyof Option, string> = Extract<keyof Option, string>, ValueKey extends Extract<keyof Option, string> = Extract<keyof Option, string>, 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>>; /** * 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; /** * Text to display for the "Select All" option. */ selectAllText?: string; /** * When true, hides the select-all option. */ hideSelectAllOption?: boolean; /** * Overrides the default object multi-autocomplete change handling. * Receives the next selected object array and the option that triggered the change. * Call `rhfOnChange` with the object array that should be stored; else the form value will not be updated. * * @param rhfOnChange - React Hook Form field change handler for the selected object array. * @param newValue - Next selected option object array. * @param selectedOption - Option object that triggered the change, or the select-all sentinel. */ customOnChange?: ({ rhfOnChange, newValue, selectedOption }: CustomOnChangeProps<OnValueChangeProps<Option>, Option[]>) => void; /** * Called after the default object multi-autocomplete handler stores the next selected object array in React Hook Form. * * ⚠️ Important: * This callback is not called when `customOnChange` is used. * * @param newValue - Next selected option object array. * @param selectedOption - Option object that triggered the change, or the select-all sentinel. */ onValueChange?: ({ newValue, selectedOption }: OnValueChangeProps<Option>) => 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; /** * Props forwarded to each internal MUI `Checkbox`. */ checkboxProps?: CheckboxProps; /** * Custom renderer for an option label. */ renderOptionLabel?: (option: Option, state: AutocompleteRenderOptionState) => ReactNode; /** * 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>; declare const RHFMultiAutocompleteObject: <T extends FieldValues, Option extends KeyValueOption = KeyValueOption, LabelKey extends Extract<keyof Option, string> = Extract<keyof Option, string>, ValueKey extends Extract<keyof Option, string> = Extract<keyof Option, string>, DisableClearable extends boolean = false>(props: RHFMultiAutocompleteObjectProps<T, Option, LabelKey, ValueKey, DisableClearable> & { ref?: Ref<HTMLInputElement>; }) => JSX.Element; //#endregion export { RHFMultiAutocompleteObjectProps, RHFMultiAutocompleteObject as default };