npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mui-rhf-integration

v4.0.3

Published

React Hook Form integration for MUI with strict TypeScript handling based on form context.

Downloads

134

Readme

MUI RHF Integration

npm version Release codecov

This is a lightweight integration library for MUI and React Hook Form with the two goals of being strictly typed with proper generic definitions as well as being as unopinionated about the usage as possible.

Quick start

Installation

npm i mui-rhf-integration

You'll need to have the following peer dependencies installed:

  • react@^18
  • @mui/material@^5.5.0
  • react-hook-form@^7.29.0

Note that in order to use the RhfDatePicker, RhfDateTimePicker and RhfTimePicker components you need to also install the @mui/x-date-pickers package.

Note: As having the date and time pickers exported together with all other input types breaks some bundlers like Vite when the optional dependency is not installed, these pickers have to be imported from mui-rhf-integration/date-picker specifically.

Form setup

import {useForm} from 'react-hook-form';
import {RhfTextField} from 'mui-rhf-integration';
import {Button} from '@mui/material';

type FieldValues = {
    title : string;
};

const MyForm = () : JSX.Element => {
    const form = useForm<FieldValues>();

    return (
        <form
            onSubmit={form.handleSubmit(() => {
                // No-op
            })}
        >
            <RhfTextField control={form.control} name="title"/>
            <Button type="submit">Submit</Button>
        </form>
    );
};

In contrast to some other integration libraries, you'll notice that you have to pass in the form control to the input element. This is preferred over utilizing a form context, as it allows Typescript to immediately verify the name passed in to match up with the properties in the FieldValues type.

Input elements

All input elements exported by this library take a control and name property to link up with the form. They additionally accept various properties which are passed through to the wrapped components.

Where applicable, the wrapper components take care of error handling and displaying error messages as helper text components below the input elements.

RhfTextField

Wrapper around the TextField component. Passes all known TextFieldProps properties down. Allowed value types are string, null and undefined.

<RhfTextField 
    control={form.control} 
    name="title"
    label="Title"
    helperText="Title of the thing"
/>

The RhfTextField additional implements the Material Design recommendation for character counters. To enable them, pass maxCharacters as property. You should additionally enable maxLength on the inputProps.

RhfCheckbox

Wrapper around the Checkbox component. Passes all known CheckboxProps properties down. Allowed value types are bool, null and undefined.

<RhfCheckbox 
    control={form.control} 
    name="enabled"
/>

In order to display the checkbox with a label, you can utilize the MUI FormControlLabel component:

<FormControlLabel
    control={<RhfCheckbox control={form.control} name="enabled"/>}
    label="Enabled"
/>

RhfSwitch

Wrapper around the Switch component. Passes all known SwitchProps properties down. Allowed value types are bool, null and undefined.

<RhfSwitch 
    control={form.control} 
    name="enabled"
/>

In order to display the switch with a label, you can utilize the MUI FormControlLabel component:

<FormControlLabel
    control={<RhfSwitch control={form.control} name="enabled"/>}
    label="Enabled"
/>

RhfCheckboxGroup

Element which handles multiple checkboxes according to the MUI documentation. The resulting value in the form values is represented as an array of strings.

Options are defined as an array of objects with a value and label property.

<RhfCheckboxGroup
    control={form.control} 
    name="colors"
    label="Colors"
    options={[
        {value: 'red', label: 'Red'},
        {value: 'green', label: 'Green'},
        {value: 'blue', label: 'Blue'},
    ]}
/>

You can influence the generated sub components via the root properties as well as the formLabelProps and formGroupProps properties.

RhfRadioGroup

Element which handles multiple radios according to the MUI documentation. The resulting value in the form values is represented as a string.

Options are defined as an array of objects with a value and label property.

<RhfRadioGroup
    control={form.control} 
    name="color"
    label="Color"
    options={[
        {value: 'red', label: 'Red'},
        {value: 'green', label: 'Green'},
        {value: 'blue', label: 'Blue'},
    ]}
/>

You can influence the generated sub components via the root properties as well as the formLabelProps and formGroupProps properties.

RhfAutocomplete

Wrapper around the Autocomplete component. Passes all known AutocompleteProps properties down. Allowed value types are null, undefined and the value type defined by the autocomplete options.

<RhfAutocomplete
    control={form.control} 
    name="city"
    textFieldProps={{
        label: 'City',
    }}
    options={[
        {value: 'berlin', label: 'Berlin'},
        {value: 'new_york', label: 'New York'},
        {value: 'syndey', label: 'Sydney'},
    ]}
/>

Additionally, it accepts the two properties valueToOption and optionToValue, which are used when you need to map e.g. objects to IDs and vice versa for your form values.

RhfDatePicker

Wrapper around the DatePicker component. Passes all known DatePickerProps properties down.

<RhfDatePicker
    control={form.control} 
    name="date"
    label="Date"
/>

RhfDateTimePicker

Wrapper around the DateTimePicker component. Passes all known DateTimePickerProps properties down.

<RhfDateTimePicker
    control={form.control} 
    name="dateTime"
    label="Date time"
/>

RhfTimePicker

Wrapper around the TimePicker component. Passes all known TimePickerProps properties down.

<RhfDateTimePicker
    control={form.control} 
    name="time"
    label="Time"
/>