Skip to main content

React Hook Form

Here an example if you want to plug MuiOtpInput to your form using React Hook Form.

import React from 'react'
import Box from '@mui/material/Box'
import Button from '@mui/material/Button'
import FormHelperText from '@mui/material/FormHelperText'
import { MuiOtpInput } from 'mui-one-time-password-input'
import { Controller, useForm } from 'react-hook-form'

const App = () => {
const { control, handleSubmit } = useForm({
defaultValues: {
otp: ''
}
})

const onSubmit = (data) => {
alert(JSON.stringify(data))
}

return (
<form onSubmit={handleSubmit(onSubmit)}>
<Controller
name="otp"
control={control}
rules={{ validate: (value) => value.length === 6 }}
render={({ field, fieldState }) => (
<Box>
<MuiOtpInput sx={{ gap: 1 }} {...field} length={6} />
{fieldState.invalid ? (
<FormHelperText error>OTP invalid</FormHelperText>
) : null}
</Box>
)}
/>
<div>
<Button type="submit" variant="contained" sx={{ mt: 2 }}>
Submit
</Button>
</div>
</form>
)
}

Edit on CodeSandbox