0.7.2 • Published 3 years ago

@aeco-cloud/react-easy-form v0.7.2

Weekly downloads
103
License
ISC
Repository
-
Last release
3 years ago

React Easy Form

A react component that allows to easily create forms. It is basically a wrapper around for react hook form that takes out the pain of registering and configuring components and validations. All components in the form are based on the material ui theme. In order to use a custom mui theme, just wrap your form in the mui theme provider.

The package is flexible: you can still register custom elements in the react hook form if you have more customized needs.

Getting started

Install the package

npm install @aeco-cloud/react-easy-form

If you want to integrate nicely with material-ui, do not forget to include the Roboto font and use the CssBaseline component.

Working example:

import {ReactEasyForm, Checkbox, DatePicker, Dropdown, Slider, TextField} from "@aeco-cloud/react-easy-form"

import React from "react";
import { useForm, SubmitHandler, SubmitErrorHandler } from "react-hook-form";
import { Container, Button } from "@material-ui/core";
import { isValid } from "date-fns";

const App = () => {
  const onSubmit: SubmitHandler<any> = (data) => {
    console.log("submitted");
    console.log(data);
  };
  const onError: SubmitErrorHandler<any> = (e) => {
    console.log("could not submit due to errors");
    console.log(e);
  };

  const form = useForm();

  return (
    <div className="App">
      <Container maxWidth="md">
        <ReactEasyForm form={form}>
          <Checkbox
            name="checkbox1"
            type="radio"
            values={[
              { value: "true", text: "Ja" },
              { value: "false", text: "Nee" },
            ]}
            validation={{
              required: "Deze vraag is verplicht",
            }}
          />
          <DatePicker
            name="myDatePicker"
            muiProps={{color:"secondary"}}
            validation={{
              validate: {
                couldNotBeParsed: (value: any) => isValid(value) || "not a valid date",
                isAfterToday: (value: Date) => {
                  return value > new Date() || "Date should be in the future";
                },
              },
            }}
          />
          <Dropdown
            name="myDropdown"
            values={[
              { value: "afrika", text: "🇿🇦" },
              { value: "usa", text: "🇺🇸" },
            ]}
            muiProps={{ variant: "outlined" }}
          />
          <Slider
            name="mySlider"
            muiProps={{
              color:"secondary",
              max: 100,
              step: 10,
              marks: [
                { value: 20, label: "a rather low value" },
                { value: 80, label: "a rather high value" },
              ],
            }}
            validation={{
              validate: {
                lessThanTwenty: (value: any) => parseInt(value) < 20 || "should be lower than 20",
              },
            }}
          />
          <TextField
            name="myTextField"
            validation={{ required: "Please fill in this question" }}
            muiProps={{ color: "secondary", fullWidth: false }}
          />
          <Button onClick={form.handleSubmit(onSubmit, onError)}>Submit</Button>
        </ReactEasyForm>
      </Container>
    </div>
  );
};

export default App;

Documentation

ReactEasyForm

The main component is the ReactEasyForm component. The component accepts the following props:

propsrequireddescription
formyesthe form which is a result of the useForm() hook from react-hook-form
stylenoa styles object
errorStylenoa styles object that will be injected into the div that wraps all error texts

All components that are wrapped inside the ReactEasyForm component will have acces to the form, it's settings and validations

import ReactEasyForm from "@aeco-cloud/react-easy-form";
import { useForm} from "react-hook-form";
const form = useForm()

<ReactEasyForm form={form}>
  // the components that fill up the form
  // check out the documentation for all available components
  <Components>
  // in order to be able to submit the form you need to call the handleSubmit function somewhere
  <Button onClick={form.handleSubmit((data) => {console.log(data)})}>Submit</Button>;
</ReactEasyForm>

Components

The form is constructed from a combination of components.

Currently the following components are available:

Refer to the documentation of each element type to see which props are available.

TextField

A basic input textfield. Can also be used as a numberField

ProprequiredvalueDescription
nameyesstringThe uniqe name of this input
validationnoreact hook register optionsvalidation rules
muiPropsnoMUI text-field propsAll props (except name) from the MUI text-field props are available.

Dropdown

A dropdown element

ProprequiredvalueDescription
nameyesstringThe uniqe name of this input
defaultValuenostringThe default value. Should be one of the name attributes that is provided in the values prop
valuesyes{name:string, label:string}[]The options for the dropdown. name refers to the name that is available when the form is submitted, label is the label that is shown to the user
validationnoreact hook register optionsvalidation rules
muiPropsnoMUI select propsAll props (except name) from the MUI select props are available

CheckboxButton

A fancy and customizable checkbox selection component to show a number of options from which a user can make a selection. This component is a combination of a button and a checkbox. By choosing different combinations for the buttonActiveMuiProps, buttonInactiveMuiProps, checkboxActiveMuiProps and checkboxInactiveMuiProps you can create many different UI components.

PropsrequiredvalueDescription
indicator nobooleanSet to true to show a checkbox indicator in the button. Default is true.
valuesyes{name:string, label:string}[]The list of options to pick from. name refers to the value that is available when the form is submitted, label is the label that is shown to the user
nameyesstringThe uniqe name of this input
defaultValuesnostring[]The default values. Should be one or more of the name attributes that is provided in the values prop
buttonActiveMuiPropsnoMUI button propsThe button props for the button in the active state
buttonInactiveMuiPropsnoMUI button propsThe button props for the button in the inactive state
checkboxActiveMuiPropsnoMUI checkbox propsThe checkbox props for the checkbox in the active state
checkboxInactiveMuiPropsnoMUI checkbox propsThe checkbox props for the checkbox in the inactive state
validationnoreact hook register optionsvalidation rules
numberOfColumnsnonumberThe number of columns to display. Default is 1.
columnGapnonumberThe gap between the columns. Unit is px, default is 0.
rowGapnonumberThe gap between the rows. Unit is px, default is 0.

RadioButton

Equivalent to CheckboxButton. Only difference is that here a round box is shown to indicate that it is a radio button and the answers are mutually exclusive. All props are the same as on CheckboxButton except the following props:

PropsrequiredvalueDescription
defaultValuenostringThe default value. Should be one of the name attributes that is provided in the values prop
radioActiveMuiPropsnoMUI radio propsThe radio props for the radio in the active state
radioInactiveMuiPropsnoMUI radio propsThe radio props for the radio in the inactive state

Slider

A slider element.

PropsrequiredvalueDescription
nameyesstringThe uniqe name of this input
defaultValuenonumber | [number,number]the default value. if a list of two numbers is provided, a slider with a start and endpoint is created.
validationnoreact hook register optionsvalidation rules
muiPropsnoMUI slider propsAll props (except name and defaultValue) from the MUI slider props are available

DatePicker

A date picker element

ParameterrequiredvalueDescription
nameyesstringThe uniqe name of this input
defaultValuenoDatethe default value. If this is not provided, the current date will be used.
validationnoreact hook register optionsvalidation rules
muiPropsnoKeyboardDatePicker propsAll props (except name, defaultValue, invalidDateMessage, minDateMessage and maxDateMessage) from the KeyboardDatePicker props. The invalidDateMessage, minDateMessage and maxDateMessage should be implemented as validations in the validation prop.

Roadmap

  • Bugfix: on first render of checkboxButton/radioButton, the active style of default components is not applied => Done
  • add search option to dropdown
  • add example of validations in the documentation
  • add data structure of submitted fields in the documentation
  • add typed muiProps in the datePicker component
  • document expected values on no input (undefined?) and after input

New Roadmap

  • go over all components to see if they are implemented in the same way with RHF 7
  • Clean the helpers (Question, section, label)
  • Add the helpers to the library
  • Add helpers to include icons and tooltips in the select buttons
  • Use the Higher-order component api to have seperate stylesheets for each components.
  • By using the higher-order component api it might be possible to use selectButtons by styling checkbox/radio without having to use the muiButton

Documentation Roadmap

  • Question
  • Section
  • Label
  • CheckboxButton
  • RadioButton

changelog

  • 0.2.0 => breaking changes: settings from the formConfig are now accessed as props
  • 0.3.0 => breaking changes: formConfig is deprecated, components are now submitted as children of ReactEasyForm
  • 0.4.0 => breaking changes: muiProps are now supported in standard mui elements
  • 0.4.1 => required props fixes
  • 0.4.2 => style is now available as prop on the ReactEasyForm component
  • 0.5.0 => breaking changes: CheckboxButton and RadioButton are now available and constructed with native mui components
  • 0.5.2 => added styling helpers such as Section, Question and Label
  • 0.5.3 => if question is not shown, it is removed from the DOM (otherwise you can have hidden fields that need validations)
  • 0.6.0 => breaking changes: components have more predictable behaviour (no value = undefined) and renamings
  • 0.6.1 => no value = "" (empty string) => this avoids errors about switching from controlled to uncontrolled
  • 0.6.12 => added GROUP options in dropdown
  • 0.7.0 => migrated to react hook from 7, needs more testing! Only use this version if you know what you are doing
  • 0.7.1 => BUGFIX: on first render of checkboxButton/radioButton, the active style of default components is now applied
0.7.2

3 years ago

0.7.1

3 years ago

0.7.0

3 years ago

0.6.17

3 years ago

0.6.16

3 years ago

0.6.15

3 years ago

0.6.14

3 years ago

0.6.13

3 years ago

0.6.12

3 years ago

0.6.11

3 years ago

0.6.9

3 years ago

0.6.10

3 years ago

0.6.7

3 years ago

0.6.8

3 years ago

0.6.6

3 years ago

0.6.3

3 years ago

0.6.2

3 years ago

0.6.5

3 years ago

0.6.4

3 years ago

0.6.1

3 years ago

0.6.0

3 years ago

0.5.3

3 years ago

0.5.2

3 years ago

0.5.1

3 years ago

0.5.0

3 years ago

0.4.4

3 years ago

0.4.2

3 years ago

0.4.1

3 years ago

0.4.0

3 years ago

0.3.0

3 years ago

0.3.1

3 years ago

0.2.0

3 years ago

0.1.11

3 years ago

0.1.10

3 years ago

0.1.9

3 years ago

0.1.8

3 years ago

0.1.7

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago