1.5.6 • Published 5 months ago

mui-rhf-library v1.5.6

Weekly downloads
-
License
MIT
Repository
github
Last release
5 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-cnmtmxtjgl.chromatic.com/

Usage

Here is a quick example to get you started:

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:

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 * 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',
            control: control,
            props: { fullWidth: true }, // Props of the field
            gridProps: {xs: 12} // Props of the Grid: "xs" | "sm" | "md" | "ld" | "xl"
        }
    ];

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

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

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

Documentation

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

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
onChange(event: SelectChangeEvent) => voidCallback fired when a menu item is selected.
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
multiplebooleanIf true, menu will support multiple selections.
onChange(event: SelectChangeEvent) => voidCallback fired when a menu item is selected.
disableClearableboolean
textFieldPropsTextFieldPropsThe props that will be passed to TextField component in the renderInput of AutoComplete.
loadingbooleanfalseDisplays linear progress bar
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.
defaultValuestring | numberThe default value of the input that would be injected into React Hook Form Controller and the component
optionsOptionsThe 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

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.
onChange(event: React.ChangeEvent) => voidA custom method that gets triggered when the value of the checkbox is changed
defaultValuebooleanThe default value of the input that would be injected into React Hook Form Controller and the component
helperTextReactNodeForm helper text

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.
defaultValuebooleanThe default value of the input that would be injected into React Hook Form Controller and the component
onChange(event: React.ChangeEvent) => voidA custom method that gets triggered when the value of the switch is changed
helperTextReactNodeForm helper text

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
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.

1.5.5-beta.1

5 months ago

1.5.6

5 months ago

1.5.5

5 months ago

1.5.5-beta.0

5 months ago

1.5.4

5 months ago

1.5.3

7 months ago

1.5.2

7 months ago

1.5.1

8 months ago

1.5.0

8 months ago

1.4.4

8 months ago

1.4.3

11 months ago

1.4.2

11 months ago

1.4.1

11 months ago

1.4.0

11 months ago

1.3.3

1 year ago

1.3.2

1 year ago

1.3.1

1 year ago

1.3.0

1 year ago

1.2.0

1 year ago

1.2.1

1 year ago

1.1.9

2 years ago

1.1.8

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

1.0.0-beta-3

2 years ago

1.0.0-beta-2

2 years ago

1.0.0-beta-1

2 years ago

1.0.0-beta-0

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago

0.3.0

2 years ago

0.2.0

2 years ago