Skip to Content
Piplup
@piplup/rhf-adaptersmui-materialComponents

MuiOutlinedInputElement

Use MUI OutlinedInput with react-hook-form without writing a Controller. MuiOutlinedInputElement binds value and validation for the outlined-variant input.

Import

import { MuiOutlinedInputElement } from '@piplup/rhf-adapters/mui-material';

Usage

'use client';import Stack from '@mui/material/Stack';import { MuiOutlinedInputElement, MuiButtonElement } from '@piplup/rhf-adapters/mui-material';import { useForm } from 'react-hook-form';export default function Page() {  const { control, handleSubmit } = useForm({    defaultValues: { text: '' },  });  return (    <form onSubmit={handleSubmit((values) => alert(JSON.stringify(values, null, 2)))}>      <Stack spacing={2}>        <MuiOutlinedInputElement control={control} name="text" />        <div>          <MuiButtonElement control={control} type="submit" variant="contained">            Submit          </MuiButtonElement>        </div>      </Stack>    </form>  );}

Props

Prop

Type

MuiOutlinedInputElement supports props of the MUI OutlinedInput component, along with additional adapter-specific props. For a complete list of available InputLabel properties, refer to the MUI API documentation.

The ref is forwarded to the underlying <OutlinedInput /> component.

useMuiOutlinedInputAdapter

useMuiOutlinedInputAdapter connects a field to React Hook Form and returns props you spread onto MUI's OutlinedInput. It's what MuiOutlinedInputElement uses internally.

Use it when you build an outlined field from parts, typically because you need adornments with their own state, like a show/hide button on a password, that you want to keep inside one reusable component.

Import

import { useMuiOutlinedInputAdapter } from '@piplup/rhf-adapters/mui-material';

This PasswordField requires 8 to 64 characters and has a button that switches between hidden and visible text. The value and validation state are kept when you toggle.

At least 8 characters

'use client';import * as React from 'react';import Button from '@mui/material/Button';import FormControl from '@mui/material/FormControl';import FormHelperText from '@mui/material/FormHelperText';import InputAdornment from '@mui/material/InputAdornment';import InputLabel from '@mui/material/InputLabel';import OutlinedInput from '@mui/material/OutlinedInput';import Stack from '@mui/material/Stack';import { MuiButtonElement, useMuiOutlinedInputAdapter } from '@piplup/rhf-adapters/mui-material';import { useForm } from 'react-hook-form';import type { Control } from 'react-hook-form';type FormValues = { password: string };// A password field with a show/hide toggle. The input type is passed to the// hook, not to OutlinedInput, because the adapter returns its own `type`.function PasswordField({ control }: { control: Control<FormValues> }) {  const id = React.useId();  const [visible, setVisible] = React.useState(false);  const { helperText, ref, ...adapter } = useMuiOutlinedInputAdapter({    composeHelperText: true,    control,    helperText: 'At least 8 characters',    maxLength: 64,    messages: {      minLength: (length) => `Use at least ${length} characters`,      required: 'Choose a password',    },    minLength: 8,    name: 'password',    required: true,    type: visible ? 'text' : 'password',  });  return (    <FormControl error={adapter.error} variant="outlined" required>      <InputLabel htmlFor={id}>Password</InputLabel>      <OutlinedInput        {...adapter}        endAdornment={          <InputAdornment position="end">            <Button onClick={() => setVisible((v) => !v)} size="small">              {visible ? 'Hide' : 'Show'}            </Button>          </InputAdornment>        }        autoComplete="new-password"        id={id}        inputRef={ref}        label="Password"      />      <FormHelperText>{helperText}</FormHelperText>    </FormControl>  );}export default function Page() {  const { control, handleSubmit } = useForm<FormValues>({    defaultValues: { password: '' },  });  return (    <form onSubmit={handleSubmit((values) => alert(JSON.stringify(values, null, 2)))} noValidate>      <Stack direction="column" spacing={2}>        <PasswordField control={control} />        <div>          <MuiButtonElement control={control} type="submit" variant="contained">            Set password          </MuiButtonElement>        </div>      </Stack>    </form>  );}

How it works

The hook has the same logic as useMuiInputAdapterProps: it falls back to a surrounding FormControl for required and disabled, and you should pass the returned ref to inputRef. Value handling, transforms and the validation rules are described on useMuiInputBaseAdapter.

Changing the input type. The result always contains a type key, even when you don't pass one. In <OutlinedInput type="password" {...adapter} />, the spread replaces your type with undefined and the password shows as plain text. Pass the type to the hook instead, as the demo does with type: visible ? "text" : "password". Changing it re-renders the same field, so the value is kept.

Label notch. OutlinedInput only leaves a gap in its border for the floating label when you give it the same text through its label prop. This is MUI behavior, but it's easy to miss when the label comes from an InputLabel next to it.

Length messages. The demo's minLength message is a function, (length) => `Use at least ${length} characters`, so the number in the text follows the rule. maxLength keeps its default, "Please shorten this text to 64 characters or less."

Props

Prop

Type

Return value

Prop

Type

See also

On this page