@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.
100 lines (99 loc) • 3.58 kB
TypeScript
import { CustomComponentIds, CustomOnChangeProps } from "../../types/common.js";
import { FormHelperTextProps, FormLabelProps } from "../../types/mui.js";
import { JSX, ReactNode, Ref, SyntheticEvent } from "react";
import { Control, FieldValues, Path, RegisterOptions } from "react-hook-form";
import { RatingProps } from "@mui/material/Rating";
//#region src/mui/rating/index.d.ts
type OnValueChangeProps = {
newValue: number | null;
event: SyntheticEvent<Element, Event>;
};
type InputRatingProps = Omit<RatingProps, 'name' | 'onChange' | 'error' | 'value' | 'defaultValue'>;
type RHFRatingProps<T extends FieldValues> = {
/**
* 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>>;
/**
* When true, marks the field as required in the UI and accessibility attributes.
*/
required?: boolean;
/**
* Overrides the default rating change handling.
* Receives the next rating value and the original rating change event.
* Call `rhfOnChange` with the number 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 rating value.
* @param newValue - Next rating value, or `null` when cleared.
* @param event - Original rating change event.
*/
customOnChange?: ({
rhfOnChange,
newValue,
event
}: CustomOnChangeProps<OnValueChangeProps, number | null>) => void;
/**
* Called after the default rating handler stores the next rating value in React Hook Form.
*
* ⚠️ Important:
* This callback is not called when `customOnChange` is used.
*
* @param newValue - Next rating value, or `null` when cleared.
* @param event - Original rating change event.
*/
onValueChange?: ({
newValue,
event
}: OnValueChangeProps) => void;
/**
* 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;
/**
* 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'>;
/**
* Custom ids for generated field, label, helper text, and error elements.
*/
customIds?: CustomComponentIds;
} & InputRatingProps;
declare const RHFRating: <T extends FieldValues>(props: RHFRatingProps<T> & {
ref?: Ref<HTMLSpanElement>;
}) => JSX.Element;
//#endregion
export { RHFRatingProps, RHFRating as default };