MCPcopy Index your code
hub / github.com/dohomi/react-hook-form-mui

github.com/dohomi/react-hook-form-mui @10.0.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release 10.0.0 ↗ · + Follow
9,485 symbols 32,808 edges 124 files 8 documented · 0%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Material-UI and react-hook-form combined

Material-UI and react-hook-form combined

GitHub Average time to resolve an issue Percentage of issues still open

About this project

This project simplifies the use of react-hook-form and Material-UI. It provides opinionated use cases with following components:

  • FormContainer
  • AutocompleteElement
  • TextFieldElement
  • SelectElement
  • MultiSelectElement
  • RadioButtonGroup
  • CheckboxButtonGroup
  • CheckboxElement
  • SwitchElement
  • PasswordElement
  • DatePickerElement
  • DateTimePickerElement
  • SliderElement
  • ToggleButtonGroupElement

Please check out the demo for the element overview.

How to use it

Installation

# npm install react-hook-form react-hook-form-mui
# yarn add react-hook-form react-hook-form-mui

This package utilizes pickers and icons of the MUI ecosystem. If you make use of it add them to your app.

#  npm install @mui/x-date-pickers @mui/icons-material
#  yarn add @mui/x-date-pickers @mui/icons-material

Important

From versions >= 3.x of this package MUI v5 is in use. Versions of 1 & 2 using Material-UI v4

From version >= 6 x-date-pickers version 6 is in use. Make sure you upgrade your dependencies.

Simple form setup

import {FormContainer, TextFieldElement} from 'react-hook-form-mui'

function Form() {
    return (
        <FormContainer
            defaultValues={{name: ''}}
            onSuccess={data => console.log(data)}
        >
            <TextFieldElement name="name" label="Name" required/>
        </FormContainer>
    )
}

Typesafe form setup

  function Form() {
    const {control, handleSubmit} = useForm({
      defaultValues: {
        name: '',
        auto: '',
        check: false
      },
    })
    const options = [
      {id: 'one', label: 'One'},
      {id: 'two', label: 'Two'},
      {id: 'three', label: 'Three'},
    ]
    return (
        <form onSubmit={handleSubmit((data) => console.log(data))} noValidate>
          <Stack spacing={2}>
            <TextFieldElement
              name={'name'}
              label={'Name'}
              control={control}
              required
              fullWidth
            />
            <AutocompleteElement
              name={'auto'}
              label={'Autocomplete'}
              control={control}
              options={options}
            />
            <CheckboxElement name={'check'} label={'Check'} control={control} />
            <Button type={'submit'} color={'primary'}>
              Submit
            </Button>
          </Stack>
        </form>
    )
  }

You can have a look at all different possibilities to use forms at following code examples

FormContainer creates formContext

The <FormContainer /> wires up a form and you can create sub-components which either make use of useFormContext() | useWatch() to react to form values.

Demo

Check out Storybook: Demo

You will find examples and use cases.

With Datepicker

If you are using the DatepickerElement keep in mind that you have to wrap your form with a provider:

Examples for Dayjs or DateFns provider (used in the demo):

Troubleshooting

Issues if context is undefined (useWatch)

For convenient reasons this package is re-exporting react-hook-form which is especially required if you have context issues of React.

import {useWatch} from 'react-hook-form-mui' // instead of react-hook-form

const MySubmit = () => {
    const value = useWatch('fieldName')
    return (
        <Button disabled={!value}>Submit</Button>
    )
}

Bundle

This project uses tsup to wrap the package for npm.

Support Maintenance

If you find this package useful consider a small contribution: Buy Me A Coffee


Contributing

Make sure you're running Node.js 20

  1. Fork this repository

Click the “Fork” button on the top-right of the repo:
👉 https://github.com/dohomi/react-hook-form-mui

  1. Clone your forked repository

Replace <your-username> with your GitHub username:

git clone https://github.com/<your-username>/react-hook-form-mui.git
cd react-hook-form-mui

Optional (but recommended): Add the original repo as a remote named upstream:

git remote add upstream https://github.com/dohomi/react-hook-form-mui.git
  1. Install dependencies
yarn
  1. Build the project
yarn build
  1. Run the Storybook
yarn sb-start
  1. Make your changes
  2. Changes to storybook stories can be made in apps/storybook/stories.
  3. Changes to the library can be made in packages/rhf-mui.

  4. Push to a branch on your fork and open a Pull Request

After pushing, GitHub will show a “Compare & pull request” button.

Extension points exported contracts — how you extend this code

MyAppProps (Interface)
(no doc)
apps/nextjs/src/pages/_app.tsx

Core symbols most depended-on inside this repo

createElement
called by 3494
apps/storybook/storybook-static/assets/components-D_wsgbzG.js
n
called by 903
apps/storybook/storybook-static/assets/iframe-BBlJSKdw.js
t
called by 593
apps/storybook/storybook-static/assets/esm-D4A6xbwj.js
keys
called by 547
apps/storybook/storybook-static/vite-inject-mocker-entry.js
filter
called by 510
apps/storybook/storybook-static/assets/components-D_wsgbzG.js
W
called by 371
apps/storybook/storybook-static/assets/esm-D4A6xbwj.js
t
called by 352
apps/storybook/storybook-static/assets/date-fns-ojAvi97E.js
set
called by 335
apps/storybook/storybook-static/assets/iframe-BBlJSKdw.js

Shape

Function 8,610
Method 807
Class 67
Interface 1

Languages

TypeScript100%

Modules by API surface

apps/storybook/storybook-static/assets/iframe-BBlJSKdw.js2,160 symbols
apps/storybook/storybook-static/sb-manager/runtime.js1,093 symbols
apps/storybook/storybook-static/assets/components-D_wsgbzG.js1,064 symbols
apps/storybook/storybook-static/sb-addons/storybook-core-server-presets-0/common-manager-bundle.js763 symbols
storybook-static/assets/formatter-9dc562d4.7fe334a9.js762 symbols
storybook-static/assets/index.3861d36b.js623 symbols
apps/storybook/storybook-static/assets/react-18-CGb5c2Cq.js489 symbols
apps/storybook/storybook-static/assets/esm-D4A6xbwj.js472 symbols
apps/storybook/storybook-static/assets/DocsRenderer-LL677BLK-B1Uf3Bxm.js276 symbols
apps/storybook/storybook-static/assets/date-fns-ojAvi97E.js251 symbols
storybook-static/8.48bb74f3515c3fd5527e.manager.bundle.js156 symbols
storybook-static/assets/OverlayScrollbars-26c4a78d.a724cc42.js136 symbols

For agents

$ claude mcp add react-hook-form-mui \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact