@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.
91 lines (90 loc) • 3.19 kB
TypeScript
import { CustomComponentIds, CustomOnChangeProps } from "../../types/common.js";
import { FormControlLabelProps, FormHelperTextProps } from "../../types/mui.js";
import { ChangeEvent, JSX, ReactNode, Ref } from "react";
import { Control, FieldValues, Path, RegisterOptions } from "react-hook-form";
import { SwitchProps } from "@mui/material/Switch";
//#region src/mui/switch/index.d.ts
type OnValueChangeProps = {
newValue: boolean;
event: ChangeEvent<HTMLInputElement>;
};
type RHFSwitchProps<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>>;
/**
* Overrides the default switch change handling.
* Receives the next checked state and the original switch change event.
* Call `rhfOnChange` with the boolean value that should be stored; else the form value will not be updated.
*
* @param rhfOnChange - React Hook Form field change handler for the checked value.
* @param newValue - Next checked state.
* @param event - Original switch change event.
*/
customOnChange?: ({
rhfOnChange,
newValue,
event
}: CustomOnChangeProps<OnValueChangeProps, boolean>) => void;
/**
* Called after the default switch handler stores the next checked state in React Hook Form.
*
* ⚠️ Important:
* This callback is not called when `customOnChange` is used.
*
* @param newValue - Next checked state.
* @param event - Original switch change event.
*/
onValueChange?: ({
newValue,
event
}: OnValueChangeProps) => void;
/**
* Label content shown for the field. Defaults to a label generated from `fieldName`.
*/
label?: ReactNode;
/**
* Props forwarded to the switch `FormControlLabel`.
*/
formControlLabelProps?: FormControlLabelProps;
/**
* 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;
} & Omit<SwitchProps, 'name' | 'value' | 'checked' | 'defaultChecked' | 'onChange'>;
declare const RHFSwitch: <T extends FieldValues>(props: RHFSwitchProps<T> & {
ref?: Ref<HTMLInputElement>;
}) => JSX.Element;
//#endregion
export { RHFSwitchProps, RHFSwitch as default };