@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.
150 lines (149 loc) • 6.99 kB
TypeScript
import { CustomComponentIds, CustomOnChangeProps, KeyValueOption } from "../../types/common.js";
import { AutoCompleteTextFieldProps, FormHelperTextProps, FormLabelProps, MuiChipProps } from "../../types/mui.js";
import { JSX, ReactNode, Ref, SyntheticEvent } from "react";
import { Control, FieldValues, Path, RegisterOptions } from "react-hook-form";
import { AutocompleteChangeDetails, AutocompleteChangeReason, AutocompleteProps, AutocompleteValue } from "@mui/material/Autocomplete";
//#region src/mui/autocomplete-object/index.d.ts
type OmittedAutocompleteProps<Option extends KeyValueOption = KeyValueOption, Multiple extends boolean = false, DisableClearable extends boolean = false> = Omit<AutocompleteProps<Option, Multiple, DisableClearable, false>, 'freeSolo' | 'multiple' | 'fullWidth' | 'renderInput' | 'options' | 'value' | 'defaultValue' | 'onChange' | 'getOptionKey' | 'getOptionLabel' | 'isOptionEqualToValue' | 'blurOnSelect' | 'disableCloseOnSelect' | 'ChipProps' | 'ref' | 'disableClearable'>;
type OnValueChangeProps<Option extends KeyValueOption = KeyValueOption, Multiple extends boolean = false, DisableClearable extends boolean = false> = {
newValue: AutocompleteValue<Option, Multiple, DisableClearable, false>;
event: SyntheticEvent<Element, Event>;
reason: AutocompleteChangeReason;
details?: AutocompleteChangeDetails<Option>;
};
type RHFAutocompleteObjectProps<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>, 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>>;
/**
* Options rendered by the field.
*/
options: Option[];
/**
* When true, allows selecting multiple values.
*/
multiple?: Multiple;
/**
* 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;
/**
* Overrides the default object autocomplete change handling.
* Receives the selected object value from MUI without reducing it to `valueKey`.
* Call `rhfOnChange` with the object, object array, or `null` value that should be stored; else the form value will not be updated.
*
* @param rhfOnChange - React Hook Form field change handler for the selected object value.
* @param newValue - Selected value(s) stored in the form: `object[]` when `multiple` is true, otherwise `object`.
* Includes `null` only when clearing is allowed (`disableClearable` is false).
* @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<Option, Multiple, DisableClearable>, AutocompleteValue<Option, Multiple, DisableClearable, false>>) => void;
/**
* Called after the default object autocomplete handler stores the selected object value in React Hook Form.
*
* ⚠️ Important:
* This callback is not called when `customOnChange` is used.
*
* @param newValue - Selected value(s) stored in the form: `object[]` when `multiple` is true, otherwise `object`.
* Includes `null` only when clearing is allowed (`disableClearable` is false).
* @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<Option, 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;
/**
* 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;
} & OmittedAutocompleteProps<Option, Multiple, DisableClearable>;
/**
* The component is designed to store complete option object(s) in form state.
*
* `freeSolo` is not supported in `RHFAutocompleteObject` as it would introduce
* string values alongside objects (`Option | string`), making the field value
* less predictable and type-safe.
*
* Use `RHFAutocomplete` instead when `freeSolo` behavior is required.
*/
declare const RHFAutocompleteObject: <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>, Multiple extends boolean = false, DisableClearable extends boolean = false>(props: RHFAutocompleteObjectProps<T, Option, LabelKey, ValueKey, Multiple, DisableClearable> & {
ref?: Ref<HTMLInputElement>;
}) => JSX.Element;
//#endregion
export { RHFAutocompleteObjectProps, RHFAutocompleteObject as default };