Gathering detailed insights and metrics for mui-react-hook-form-plus
Gathering detailed insights and metrics for mui-react-hook-form-plus
Gathering detailed insights and metrics for mui-react-hook-form-plus
Gathering detailed insights and metrics for mui-react-hook-form-plus
The complete type-safe material-ui and react-hook-form combo and beyond with simple api.
npm install mui-react-hook-form-plus
Typescript
Module System
TypeScript (89.37%)
HTML (8.77%)
JavaScript (1.8%)
CSS (0.07%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
69 Stars
153 Commits
8 Forks
3 Watchers
1 Branches
3 Contributors
Updated on Feb 02, 2025
Latest Version
1.5.1
Package Id
mui-react-hook-form-plus@1.5.1
Unpacked Size
488.55 kB
Size
67.75 kB
File Count
58
Published on
May 28, 2023
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
4
35
material-ui
💙TS
💙react-hook-form
& more...The complete type-safe
material-ui
and react-hook-form
combo and beyond with simple api.
Highly Customizable
and supports 99% use-cases
Click here to see a live example!
Before Installing we need to install material-ui & react-hook-form
For date pickers
1npm install @mui/x-date-pickers
2---- or ----
3yarn add @mui/x-date-pickers
1npm install mui-react-hook-form-plus 2---- or ---- 3yarn add mui-react-hook-form-plus
If you are familiar with react-hook-form
you will love it! Otherwise, you will also love it 😻
We use propGetter
pattern just like react-hook-form
is doing by registering
the state
of each field.
Components
and Hooks
form mui-react-hook-form-plus
.useHookForm
get the registerState
method.registerState
method with name
as argument
that you want to register
the field
to with spread operator
.For more clear-cut answer follow
the example below:
1import { HookTextField, HookRating, useHookForm } from 'mui-react-hook-form-plus '; 2 3const Component = () => { 4 const defaultValues = { name: 'Adiat Hasan', rating: 4 }; 5 6 const { registerState, handleSubmit } = useHookForm({ 7 defaultValues, 8 }); 9 10 const onSubmit = (data: typeof defaultValues) => { 11 // will run if it is valid 12 }; 13 14 return ( 15 <form onSubmit={handleSubmit(onSubmit)}> 16 <HookTextField {...registerState('name')} /> 17 <HookRating {...registerState('rating')} /> 18 <button type='submit'>Submit</button> 19 </form> 20 ); 21};
We have awesome typescript
support so that you can take the most of it. Also, validation
is a piece of 🧁(cake)
Validation
Add rules
prop to your [InputComponents]
1import { HookTextField, useHookForm } from 'mui-react-hook-form-plus '; 2 3const Component = () => { 4 const defaultValues = { name: '', isAdmin: true }; 5 6 const { registerState, handleSubmit } = useHookForm({ 7 defaultValues, 8 }); 9 10 const onSubmit = (data: typeof defaultValues) => { 11 // will run if it is validated | if !valid will thrown error in the UI 12 }; 13 14 return ( 15 <form onSubmit={handleSubmit(onSubmit)}> 16 <HookTextField 17 {...registerState('name')} 18 rules={{ 19 required: { 20 value: true, 21 message: 'A required field', 22 }, 23 // maxLength 24 // minLength 25 // pattern 26 // validate -> Fn -> reutrn -> srting | undefined 27 }} 28 /> 29 <button type='submit'>Submit</button> 30 </form> 31 ); 32};
It will validate
based on validation rules
we specify.
The onSubmit
Fn
will be triggered if all input === valid
For more options for rules look into this
Now what if we want our vanilla
<input />
?
Just use the register
method not the registerState
1import { HookTextField, useHookForm } from 'mui-react-hook-form-plus '; 2 3const Component = () => { 4 const defaultValues = { name: 'Adiat Hasan', rating: 4 }; 5 6 const { registerState, handleSubmit, register } = useHookForm({ 7 defaultValues, 8 }); 9 10 const onSubmit = (data: typeof defaultValues) => { 11 // -> do something with the data 12 }; 13 14 return ( 15 <form onSubmit={handleSubmit(onSubmit)}> 16 <input {...register('rating')} /> 17 <HookTextField {...registerState('name')} /> 18 <button type='submit'>Submit</button> 19 </form> 20 ); 21};
You might be wondering what about deep nested
complex Component
?
Use the FormContext
to make it simple.
HookFormProvider
useHookForm
to HookFormProvider
registerState
method anywhere in the tree
from useHookFormContext
1import { HookTextField, useHookForm, HookFormProvider } from 'mui-react-hook-form-plus '; 2 3const Component = () => { 4 const defaultValues = { firstName: '', lastName: '', sex: '', rating: 3.5 }; 5 6 const methods = useHookForm<Person>({ 7 defaultValues, 8 }); 9 10 const { registerState, handleSubmit } = methods; 11 12 const onSubmit = (data: Person) => { 13 // do something 14 }; 15 16 return ( 17 <HookFormProvider {...methods}> 18 <form onSubmit={handleSubmit(onSubmit)}> 19 <HookTextField {...registerState('firstName')} textFieldProps={{ label: 'First Name' }} /> 20 <HookTextField {...registerState('lastName')} textFieldProps={{ label: 'Last Name' }} /> 21 <NestedComponent /> 22 <button type='submit'>Submit</button> 23 </form> 24 </HookFormProvider> 25 ); 26};
Now we can get the registerState
without prop drilling
1import { HookRating, useHookForm } from 'mui-react-hook-form-plus '; 2 3const NestedComponent = () => { 4 const { registerState } = useHookFormContext<Person>(); 5 6 return <HookRating {...registerState('rating')} ratingProps={{ precision: 0.5 }} />; 7};
Note that using FormContext
can lack in performance as it is built on top of React.Context
.
To optimize it further and for learning more check out this
Layouts [ Form + Grid ]
We baked in <Grid/>
directly into the [InputComponents]
so that it enhances the DX
.
A gridProps
is what you need to lay out the [InputComponents]
.
But don't forget to Wrap
it inside a <Grid Container/>
1import { Button, Grid } from '@mui/material'; 2import { HookTextField, HookRating, useHookForm } from 'mui-react-hook-form-plus '; 3 4const Component = () => { 5 const defaultValues = { name: '', rating: 4 }; 6 7 const { registerState, handleSubmit } = useHookForm({ 8 defaultValues, 9 }); 10 11 const onSubmit = (data: typeof defaultValues) => { 12 // will run if it is valid 13 }; 14 15 return ( 16 <form onSubmit={handleSubmit(onSubmit)}> 17 <Grid container spacing={3}> 18 <HookTextField 19 {...registerState('name')} 20 gridProps={{ 21 xs: 12, 22 md: 5, 23 }} 24 /> 25 <HookRating 26 {...registerState('rating')} 27 gridProps={{ 28 xs: 12, 29 md: 5, 30 }} 31 /> 32 <Grid> 33 <Button type='submit' variant='contained'> 34 Submit 35 </Button> 36 </Grid> 37 </Grid> 38 </form> 39 ); 40};
DatePicker
You need to install 3 different types of package to make the pickers work:
1// Install component (community version) 2yarn add @mui/x-date-pickers 3 4// Install date library (if not already installed) 5yarn add date-fns
1import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns'; 2import { HookDatePicker } from 'mui-react-hook-form-plus '; 3 4const Component = () => { 5 return ( 6 <LocalizationProvider dateAdapter={AdapterDateFns}> 7 <form onSubmit={handleSubmit(onSubmit)}> 8 <HookDatePicker {...registerState('trialEndsAt')} /> 9 </form> 10 </LocalizationProvider> 11 ); 12};
Available Input Components
<HookToggleButtonGroup />
<HookAutoComplete />
<HookRadioButton />
<HookTextField />
<HookCheckBox />
<HookSelect />
<HookSwitch />
<HookRating />
<HookSlider />
Check out Inputs Demo
DatePicker
<HookDatePicker />
<HookStaticDatePicker />
<HookDesktopDatePicker />
<HookMobileDatePicker />
Check out DatePicker Demo
DateTimePicker
<HookDateTimePicker />
<HookStaticDateTimePicker />
<HookDesktopDateTimePicker />
<HookMobileDateTimePicker />
Check out DateTimePicker Demo
TimePicker
<HookTimePicker />
<HookStaticTimePicker />
<HookDesktopTimePicker />
<HookMobileTimePicker />
Check out TimePicker Demo
Form Hooks
useHookForm
useHookFormContext
Context Providers
HookFormProvider
Effortless Hooks
As we have promised
with the project name
with adding a -plus
to mui-react-hook-form-plus
.
We delivered it. A few effortless hooks to make your mui
journey special
.
We provided the same pattern
as register
and propGetters
as the form
components
Those Hooks are:
useMenu
usePagination
useAccordion
useTabs
useDialog
useBackdrop
useBottomNavigation
And more hooks
are in lab 🧪 preparing to be released. So, stay tuned.
Check out Hooks Demo
Just follow the CONTRIBUTING.md
& you are good to go.
No vulnerabilities found.
No security vulnerabilities found.