MuiFormLabelElement
Use MUI FormLabel with react-hook-form without writing a Controller. MuiFormLabelElement turns red and marks required fields from the field’s state.
Import
import { MuiFormLabelElement } from '@piplup/rhf-adapters/mui-material';Usage
'use client';import * as React from 'react';import FormControl from '@mui/material/FormControl';import Stack from '@mui/material/Stack';import { MuiFormHelperTextElement, MuiOutlinedInputElement, MuiFormLabelElement, MuiButtonElement,} from '@piplup/rhf-adapters/mui-material';import { useForm } from 'react-hook-form';export default function Page() { const inputId = React.useId(); const helperTextId = `${inputId}-helper-text`; const { control, handleSubmit } = useForm({ defaultValues: { email: '' } }); return ( <form onSubmit={handleSubmit((values) => alert(JSON.stringify(values, null, 2)))} noValidate> <Stack direction="column" spacing={2}> <FormControl variant="outlined"> <MuiFormLabelElement control={control} htmlFor={inputId} name="email" sx={{ mb: 0.5 }}> Email </MuiFormLabelElement> <MuiOutlinedInputElement aria-describedby={helperTextId} control={control} id={inputId} name="email" type="email" required /> <MuiFormHelperTextElement control={control} id={helperTextId} name="email" renderOnError /> </FormControl> <div> <MuiButtonElement control={control} type="submit" variant="contained"> Submit </MuiButtonElement> </div> </Stack> </form> );}Props
Prop
Type
MuiFormLabelElement supports props of the MUI FormLabel component, along with additional adapter-specific props. For a complete list of available FormLabel properties, refer to the MUI API documentation.
The
refis forwarded to the underlying<FormLabel />component.
useMuiFormLabelAdapter
useMuiFormLabelAdapter returns props for MUI's FormLabel that reflect the state of a field registered elsewhere. It's what MuiFormLabelElement uses internally.
Use the hook when the label is part of a custom field component, so the label, input and helper text all live in one place, as in the example below. For a label placed directly in a form, the element is shorter.
Import
import { useMuiFormLabelAdapter } from '@piplup/rhf-adapters/mui-material';BioField is built from three hooks: useMuiInputBaseAdapter for the input, this hook for the label, and useMuiFormHelperTextAdapter for the message below. Submit it empty and the label, border and helper text all switch to the error state.
'use client';import * as React from 'react';import FormHelperText from '@mui/material/FormHelperText';import FormLabel from '@mui/material/FormLabel';import InputBase from '@mui/material/InputBase';import Stack from '@mui/material/Stack';import { MuiButtonElement, useMuiFormHelperTextAdapter, useMuiFormLabelAdapter, useMuiInputBaseAdapter,} from '@piplup/rhf-adapters/mui-material';import { useForm } from 'react-hook-form';import type { Control } from 'react-hook-form';type FormValues = { bio: string };// A field built from three hooks. Only the input registers the value.// The label and helper text read the same field's error state by name.function BioField({ control }: { control: Control<FormValues> }) { const inputId = React.useId(); const helperId = `${inputId}-helper`; const { helperText: inputHelperText, ref, ...input } = useMuiInputBaseAdapter({ control, maxLength: 160, messages: { maxLength: (max) => `Keep it under ${max} characters`, required: 'Tell us a little about yourself', }, name: 'bio', required: true, }); const { helperText: labelHelperText, ...label } = useMuiFormLabelAdapter< FormValues, 'bio', HTMLLabelElement >({ control, name: 'bio', }); const helper = useMuiFormHelperTextAdapter<FormValues, 'bio', HTMLParagraphElement>({ children: 'Shown on your public profile', composeHelperText: true, control, name: 'bio', }); return ( <Stack spacing={0.5}> <FormLabel {...label} htmlFor={inputId} required> Bio </FormLabel> <InputBase {...input} sx={{ border: 1, borderColor: input.error ? 'error.main' : 'divider', borderRadius: 1, p: 1, }} id={inputId} inputRef={ref} minRows={3} slotProps={{ input: { 'aria-describedby': helperId } }} multiline /> <FormHelperText {...helper} id={helperId} /> </Stack> );}export default function Page() { const { control, handleSubmit } = useForm<FormValues>({ defaultValues: { bio: '' }, }); return ( <form onSubmit={handleSubmit((values) => alert(JSON.stringify(values, null, 2)))} noValidate> <Stack direction="column" spacing={2}> <BioField control={control} /> <div> <MuiButtonElement control={control} type="submit" variant="contained"> Save </MuiButtonElement> </div> </Stack> </form> );}How it works
Read-only. The hook doesn't call useController, so it registers nothing: no value, no rules, no onChange. It subscribes to the form state with useFormState and looks up the field by name. You can render several labels or helper texts for one field, and none of them changes what is submitted. The name must match a field that some input registers.
It needs control, or a surrounding FormProvider/FormContainer.
Returned props. error, disabled, className, style, classes and ref, plus a helperText that FormLabel doesn't accept. Remove it before spreading, as the demo does.
erroristruewhile the field has a validation error, or whatever you pass aserror.disabledistruewhen you passdisabledor the whole form is disabled (useForm({ disabled: true })). If you pass nothing and the label is inside a MUIFormControl, the control'sdisabledis used.
Both are passed to FormLabel explicitly, even when false. That means they override the error and disabled of a surrounding FormControl for this label.
The label doesn't know when the input itself is disabled through its own disabled prop. Pass the
same disabled to both hooks, or disable the whole form.
useMuiInputLabelAdapter is the same hook for InputLabel, but it doesn't read FormControl's disabled. The generic reading of field state is described in useFieldStateAdapter.
Props
Prop
Type
Return value
Prop
Type
See also
MuiFormHelperTextElement
Use MUI FormHelperText with react-hook-form without writing a Controller. MuiFormHelperTextElement shows a field’s validation error or your hint text.
MuiIconButtonElement
Use MUI IconButton with react-hook-form without writing a Controller. MuiIconButtonElement handles submit, reset and form-state-aware icon button actions.