MuiInputLabelElement
Use MUI InputLabel with react-hook-form without writing a Controller. MuiInputLabelElement reflects the field’s error and required state.
Import
import { MuiInputLabelElement } 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, MuiInputLabelElement, 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"> <MuiInputLabelElement control={control} htmlFor={inputId} name="email" sx={{ mb: 0.5 }}> Email </MuiInputLabelElement> <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
MuiInputLabelElement supports props of the MUI InputLabel component, along with additional adapter-specific props. For a complete list of available InputLabel properties, refer to the MUI API documentation.
The
refis forwarded to the underlying<InputLabel />component.
useMuiInputLabelAdapter
useMuiInputLabelAdapter returns props for MUI's floating InputLabel that reflect the state of a field registered by another hook. It's what MuiInputLabelElement uses internally.
Use the hook inside a custom field component built from FormControl, InputLabel, an input and FormHelperText, so the floating label turns red with the input without passing error around by hand.
Import
import { useMuiInputLabelAdapter } from '@piplup/rhf-adapters/mui-material';PromoCodeField uses useMuiInputAdapterProps for the input and this hook for the label. The code is uppercased as you type. Enter SAVE24 and submit to see the label, underline and helper text switch to the error state.
'use client';import * as React from 'react';import FormControl from '@mui/material/FormControl';import FormHelperText from '@mui/material/FormHelperText';import Input from '@mui/material/Input';import InputLabel from '@mui/material/InputLabel';import Stack from '@mui/material/Stack';import { MuiButtonElement, useMuiInputAdapterProps, useMuiInputLabelAdapter,} from '@piplup/rhf-adapters/mui-material';import { useForm } from 'react-hook-form';import type { Control } from 'react-hook-form';type FormValues = { promoCode: string };// The floating label turns red when the promo code is invalid. The label// hook does not register anything; it only reads the field's error.function PromoCodeField({ control }: { control: Control<FormValues> }) { const id = React.useId(); const { helperText: labelHelperText, ...label } = useMuiInputLabelAdapter< FormValues, 'promoCode', HTMLLabelElement >({ control, name: 'promoCode', }); const { helperText, ref, ...input } = useMuiInputAdapterProps({ composeHelperText: true, control, helperText: 'Optional, e.g. SAVE-2024', messages: { pattern: 'Codes look like SAVE-2024' }, name: 'promoCode', pattern: /^[A-Z]{4}-\d{4}$/, transform: { output: (event: React.ChangeEvent<HTMLInputElement>) => event.target.value.toUpperCase(), }, }); return ( <FormControl variant="standard"> <InputLabel {...label} htmlFor={id}> Promo code </InputLabel> <Input {...input} id={id} inputRef={ref} /> <FormHelperText error={input.error}>{helperText}</FormHelperText> </FormControl> );}export default function Page() { const { control, handleSubmit } = useForm<FormValues>({ defaultValues: { promoCode: '' }, }); return ( <form onSubmit={handleSubmit((values) => alert(JSON.stringify(values, null, 2)))} noValidate> <Stack direction="column" spacing={2}> <PromoCodeField control={control} /> <div> <MuiButtonElement control={control} type="submit" variant="contained"> Apply </MuiButtonElement> </div> </Stack> </form> );}How it works
The hook reads the field's state by name and registers nothing, exactly like useMuiFormLabelAdapter. That page covers the returned props and the cases the label can't see, such as an input disabled through its own disabled prop.
What differs from the FormLabel hook. This hook doesn't read MUI's FormControl. It always returns disabled as a boolean, and InputLabel lets that value override the control's state. Inside <FormControl disabled>, the label keeps its normal color unless you also pass disabled to this hook. The same applies to error.
InputLabel still takes required, focused, variant and size from the FormControl, so the asterisk, the focus color and the floating position work as usual. Shrinking follows the input's value through the FormControl, not this hook.
Types. Remove helperText from the result before spreading, since InputLabel doesn't accept it. If TypeScript rejects the returned ref, pass the element type as the third generic: useMuiInputLabelAdapter<FormValues, "promoCode", HTMLLabelElement>(...).
Props
Prop
Type
Return value
Prop
Type
Notes
- Use this component when MUI InputLabel should react to RHF field state.
See also
MuiInputElement
Use MUI Input with react-hook-form without writing a Controller. MuiInputElement binds value and validation for the standard-variant input.
MuiOutlinedInputElement
Use MUI OutlinedInput with react-hook-form without writing a Controller. MuiOutlinedInputElement binds value and validation for the outlined-variant input.