2.0.0 • Published 7 months ago

mui-rhf-library v2.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

Installation

mui-rhf-library is available as an npm package.

// with npm
npm install mui-rhf-library

// with yarn
yarn add mui-rhf-library

Demo

Check the storybook of the library: https://6256bd53e0b94a003aad40bd-aljnzjawfs.chromatic.com/

Usage

Here is a quick example to get you started:

Controllers

import React from 'react';
import { createRoot } from 'react-dom/client';
import { TextFieldController, SelectController } from 'mui-rhf-library';
import { useForm } from 'react-hook-form';

function App() {
    const { control } = useForm();

    return (
        <>
            <TextFieldController control={control} name="name" defaultValue="" label="TextField Controller" />

            <SelectController
                name="select"
                label="Select Controller"
                control={control}
                options={[
                    { label: 'Option One', value: 'option-one', example: { name: 'example-one' } },
                    { label: 'Option Two', value: 'option-two', example: { name: 'example-two' } }
                ]}
                optionValue="example.name"
                optionLabel="example.name"
                variant="outlined"
            />
        </>
    );
}

const container = document.querySelector('#app');
const root = createRoot(container);
root.render(<App />);

Generate form fields

You can generate form fields using the FormFields component.

By default, the component renders form fields using Grid2, but if you decide to use Grid instead, set the shouldUseDeprecatedGrid prop to true.

import React from 'react';
import { createRoot } from 'react-dom/client';
import { FormFields } from 'mui-rhf-library';
import { useForm } from 'react-hook-form';
import { yupResolver } from '@hookform/resolvers/yup';
import { Grid2 } from '@mui/material';
import * as yup from 'yup';

function App() {
     const {
         handleSubmit
         control,
     } = useForm();

  	const fields = [
        {
            fieldType: 'textField', // 'textField' | 'select' | 'autocomplete' | 'checkbox' | 'radioGroup' | 'switch' | 'datePicker' |'custom'
            name: 'firstName',
            label: 'firstName',
            props: { fullWidth: true }, // Props of the field
            gridProps: { size: { xs: 6 } } // Props of the Grid (Container of the input field)
        }
    ];

	const handleFormSubmit = (data) => {
        console.log({ data });
    };

    return (
        <form onSubmit={handleSubmit(handleFormSubmit)}>
            <Grid2>
                <FormFields fields={fields} control={control} />
            </Grid2>
            <button type="submit">Submit</button>
        </form>
    );
}

const container = document.querySelector('#app');
const root = createRoot(container);
root.render(<App />);

Documentation

Form Generation

Form Fields

PropTypeDefaultDefinition
fieldsField[]The fields to be generated
controlControlThe React Hook Form object to register components into React Hook Form.
shouldUseDeprecatedGridbooleanfalseThe component should use Grid2 instead of Grid.

Field[]: Array of fields to be generated, where each field is an object with the following properties:

  • name: The name of the field.
  • label: The label of the field.
  • fieldType: The type of the field (textField, select, autocomplete, checkbox, radioGroup, switch, datePicker, or custom).
  • gridProps: Props of the Grid (Container of the input field), for the available props, please check Grid2 or Grid (if you want to use deprecated Grid).
  • props: Props of the field, for the available props, checkout related documentation depends on the fieldType.
  • hidden: If the field should be hidden.

Controllers

TextField Controller

Props of Material UI TextField are also available.

PropTypeDefaultDefinition
name*stringThe name of the input
control*ControlThe React Hook Form object to register components into React Hook Form.
defaultValueanyThe default value of the input that would be injected into React Hook Form Controller and the component
typeReact.HTMLInputTypeAttributetextThe type attribute in an HTML input.

Select Controller

Props of Material UI Select are also available.

PropTypeDefaultDefinition
name*stringThe name of the input
control*ControlThe React Hook Form object to register components into React Hook Form.
defaultValueanyThe default value of the input that would be injected into React Hook Form Controller and the component
options{disabled?: boolean, [key:string]: any}[]The option items that is available to the component.
optionValuestring'value'Set property of options's value
optionLabelstring'label'Set property of items’s text label
loadingbooleanfalseDisplays linear progress bar
customOptionLabel(option: any) => anyDisplay custom option label
helperTextReactNodeForm helper text

Autocomplete Controller

Props of Material UI Autocomplete are also available.

PropTypeDefaultDefinition
name*stringThe name of the input
control*ControlThe React Hook Form object to register components into React Hook Form.
defaultValue*anyThe default value of the input that would be injected into React Hook Form Controller and the component
options*{}[]The option items that is available to the component.
optionValuestring'value'Set property of options's value
optionLabelstring'label'Set property of items’s text label
textFieldPropsTextFieldPropsThe props that will be passed to TextField component in the renderInput of AutoComplete.
customOptionLabel(option: any) => anyDisplay custom option label

RadioGroup Controller

PropTypeDefaultDefinition
name*stringThe name of the input
labelstringThe label content
control*ControlThe React Hook Form object to register components into React Hook Form.
defaultValue*string | numberThe default value of the input that would be injected into React Hook Form Controller and the component
options*OptionsThe option items that is available to the component.
onChange(event: React.ChangeEvent) => voidA custom method that gets triggered when the value of the input is changed
helperTextReactNodeForm helper text
onBlur(event: React.FocusEvent) => void;A custom method that gets triggered on blur of the input

Checkbox Controller

PropTypeDefaultDefinition
name*stringThe name of the input
label*stringThe label content
control*ControlThe React Hook Form object to register components into React Hook Form.
defaultValuebooleanThe default value of the input that would be injected into React Hook Form Controller and the component
helperTextReactNodeForm helper text
onBlur(event: React.FocusEvent) => void;A custom method that gets triggered on blur of the input

Switch Controller

PropTypeDefaultDefinition
name*stringThe name of the input
label*stringThe label content
control*ControlThe React Hook Form object to register components into React Hook Form.
onChange(event: React.ChangeEvent, value: string) => void;A custom method that gets triggered when the value of the switch is changed
helperTextReactNodeForm helper text
onBlur(event: React.FocusEvent) => void;A custom method that gets triggered on blur of the input

DatePicker Controller

PropTypeDefaultDefinition
name*stringThe name of the input
label*stringThe label content
control*ControlThe React Hook Form object to register components into React Hook Form.
defaultValueThe default value of the input that would be injected into React Hook Form Controller and the component
onChange((value: any, context: PickerChangeHandlerContext<DateValidationError>) => void)A custom method that gets triggered when the value changes
helperTextReactNodeForm helper text

Custom Controller

PropTypeDefaultDefinition
name*stringThe name of the input
control*ControlThe React Hook Form object to register components into React Hook Form.
CustomComponentReact.FCA custom component that will be rendered.

FormFields

PropTypeDefaultDefinition
fields*array of FieldPropsThe name of the input
control*ControlThe React Hook Form object to register components into React Hook Form.

Changelog

Please read the changelog for details of what has changed.

2.0.0

7 months ago

1.5.9

9 months ago

1.5.10

9 months ago

1.5.12

9 months ago

1.5.11

9 months ago

1.5.8

11 months ago

1.5.7

1 year ago

1.5.5-beta.1

1 year ago

1.5.6

1 year ago

1.5.5

1 year ago

1.5.5-beta.0

1 year ago

1.5.4

1 year ago

1.5.3

2 years ago

1.5.2

2 years ago

1.5.1

2 years ago

1.5.0

2 years ago

1.4.4

2 years ago

1.4.3

2 years ago

1.4.2

2 years ago

1.4.1

2 years ago

1.4.0

2 years ago

1.3.3

2 years ago

1.3.2

2 years ago

1.3.1

2 years ago

1.3.0

2 years ago

1.2.0

3 years ago

1.2.1

3 years ago

1.1.9

3 years ago

1.1.8

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

1.0.0-beta-3

3 years ago

1.0.0-beta-2

3 years ago

1.0.0-beta-1

3 years ago

1.0.0-beta-0

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago

0.3.0

3 years ago

0.2.0

3 years ago