RHFTextField
Text fields are a core component of any form, allowing users to input text into the UI. The RHFTextField component extends the functionality of TextField, accepting almost all TextField props while providing additional customization options.
Usage
import RHFTextField, { RHFTextFieldProps } from '@nish1896/rhf-mui-components/mui/textfield';To render a fully functional RHFTextField, you only need two or three essential props.
<RHFTextField
fieldName="firstName"
control={control}
customOnChange={({ rhfOnChange, newValue }) => {
/* Override default onChange to capitalize input text */
rhfOnChange(newValue.toUpperCase());
}}
/>You can extend or reuse the RHFTextField component by creating your own wrapper component.
/**
* The below code snippet illustrates how to create a reusable styled Textfield
* component using RHFTextField, which can be used throughout the application.
*
* In this example, the component accepts all the props of RHFTextField except
* 'renderError', 'variant' and 'showLabelAboveFormField', which have already
* been configured to maintain consistent styling across the application.
* Additionally, it includes a custom error message component that displays an
* error icon alongside the error message when there is an error.
*
* A similar approach can be taken to create reusable styled components for:
* - RHFNumberInput
* - RHFTagsInput
* - RHFPasswordInput
*/
import { Fragment, type ReactNode } from 'react';
import { type FieldValues } from 'react-hook-form';
import Typography from '@mui/material/Typography';
import RHFTextField, { type RHFTextFieldProps } from '@nish1896/rhf-mui-components/mui/textfield';
import PriorityHighIcon from '@mui/icons-material/PriorityHigh';
type StyledRHFTextFieldProps<T extends FieldValues> = Omit<
RHFTextFieldProps<T>,
'renderError' | 'showLabelAboveFormField' | 'variant'
>;
type StyledErrorMsgProps = {
errorMessage: ReactNode;
};
const StyledErrorMsg = ({ errorMessage }: StyledErrorMsgProps) => {
return (
<Fragment>
{!!errorMessage && (
<Typography
variant="body2"
sx={{
alignItems: 'center',
display: 'flex',
gap: 0.5
}}
>
<PriorityHighIcon color="error" fontSize="small" />
{errorMessage}
</Typography>
)}
</Fragment>
);
};
const StyledRHFTextField = <T extends FieldValues>(
props: StyledRHFTextFieldProps<T>
) => {
const { formHelperTextProps, ...rest } = props;
const {
sx: helperTextSx,
...otherFormHelperTextProps
} = formHelperTextProps ?? {};
const helperTextSxList = Array.isArray(helperTextSx)
? helperTextSx
: [];
if (helperTextSx && !Array.isArray(helperTextSx)) {
helperTextSxList.push(helperTextSx);
}
return (
<RHFTextField
{...rest}
variant="standard"
showLabelAboveFormField
formHelperTextProps={{
...otherFormHelperTextProps,
sx: [
...helperTextSxList,
{ ml: 0 }
]
}}
renderError={error => (
<StyledErrorMsg errorMessage={error?.message} />
)}
/>
);
};
export default StyledRHFTextField;
Examples
API
The RHFTextFieldProps interface extends TextFieldProps
and includes the following additional props.
Props marked with * are required.
| Name | Type | Description |
|---|---|---|
fieldName* | string | Name of the field registered with React Hook Form. This prop is required for all components. |
control* | UseFormControl | |
registerOptions | RegisterOptions | React Hook Form validation rules. Useful when not using a schema validation library such as Yup or Joi. |
customOnChange | ({ rhfOnChange, newValue, event }) => void | Override the default onChange behavior of the input. You must pass the updated newValue to the rhfOnChange function to update the field value. |
onValueChange | ({ newValue, event }) => void | Callback function triggered when the field value changes. |
label | ReactNode | The text to render in the FormLabel component. By default, the value of fieldName is transformed (e.g., "firstName" to "First Name") using the fieldNameToLabel function. |
showLabelAboveFormField | boolean | Render form label above the form field in FormLabel component. |
formLabelProps | FormLabelProps | FormLabelProps to customise FormLabel component for a field. Multiple fields can be configured using the ConfigProvider component. |
hideLabel | boolean | Hides the FormLabel component if you don’t want to display the default form label component or prefer to render a fully custom label instead. |
renderError | (error: FieldError) => ReactNode | Custom renderer for the React Hook Form field error. Receives the current field error and returns the content to display, such as error.message or a custom React element in the HelperText component.Added in v4.1.0. |
hideErrorMessage | boolean | A flag to prevent replacement of helper text of a field by the errorMessage when the validation is triggered. |
helperText | ReactNode | The content to display within the FormHelperText component below the field. If the field validation fails, this content will be overridden by the corresponding error message. |
formHelperTextProps | FormHelperTextProps | FormHelperTextProps to customise FormHelperText component for a field. Multiple fields can be configured using the ConfigProvider component. |
customIds | { field, label, helperText, error } | Overrides the default field, label, helper text, and error IDs used for accessibility. |

