3.1.0 • Published 1 year ago

@cosva-lab/form-builder v3.1.0

Weekly downloads
279
License
MIT
Repository
github
Last release
1 year ago

@cosva-lab/form-builder

React form builder.

NPM JavaScript Style Guide

Install

npm install --save @cosva-lab/form-builder

Usage

import React from 'react';
import {
  FieldsRender,
  FieldsBuilder,
  FieldTranslateProvider,
} from '@cosva-lab/form-builder';
import { Grid, Button, ButtonGroup } from '@material-ui/core';

export default function App() {
  const [fieldsBuilder] = React.useState(
    new FieldsBuilder({
      validate: false,
      fields: [
        {
          name: 'name',
          label: 'Name',
          value: '',
          breakpoints: {
            sm: 6,
          },
          validChange: true,
          validations: [
            {
              rule: 'isEmpty',
              message: 'This field can not be empty',
            },
          ],
        },
        {
          name: 'birth_date',
          label: 'Birth date',
          type: 'date',
          value: '',
          breakpoints: {
            sm: 6,
          },
          validations: [
            {
              rule: 'isEmpty',
              message: 'This field can not be empty',
            },
          ],
        },
        {
          name: 'email',
          label: 'Email',
          value: '',
          breakpoints: {
            sm: 6,
          },
          validations: [
            {
              rule: 'isEmail',
              message: "This field don't is email",
            },
          ],
        },
      ],
    }),
  );
  const {
    fields,
    changeField,
    validate,
    restoreLast,
    saveData,
    restore,
  } = fieldsBuilder;

  return (
    <FieldTranslateProvider translator={({ message }) => message}>
      <Grid container>
        <ButtonGroup
          fullWidth
          aria-label="full width outlined button group"
        >
          <Button
            color="default"
            onClick={async () => {
              restore();
            }}
          >
            Reset
          </Button>
          <Button
            color="default"
            onClick={async () => {
              restoreLast();
            }}
          >
            Restore
          </Button>
          <Button
            color="default"
            onClick={async () => {
              saveData();
            }}
          >
            Save
          </Button>
        </ButtonGroup>
      </Grid>
      <Grid container spacing={4}>
        <FieldsRender
          {...{
            fields,
            changeField: changeField(),
            validate,
          }}
        />
      </Grid>
      <Grid container justify="flex-end">
        <Button
          variant="outlined"
          color="primary"
          onClick={() => {
            fieldsBuilder.hasErrors({
              setErrors: true,
            });
          }}
        >
          Validate
        </Button>
        <Button
          variant="outlined"
          color="secondary"
          onClick={async () => {
            if (!(await fieldsBuilder.hasErrors())) {
              console.log(fieldsBuilder.getFieldsObject());
            }
          }}
        >
          Get Values
        </Button>
      </Grid>
    </FieldTranslateProvider>
  );
}

License

MIT © andres112013

3.1.0

1 year ago

3.0.3

1 year ago

3.0.2

2 years ago

3.0.1

2 years ago

3.0.0

2 years ago

2.0.4

2 years ago

2.0.3

2 years ago

2.0.2

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

2.0.0-beta.7

2 years ago

1.5.5

2 years ago

2.0.2-beta.0

2 years ago

2.0.2-beta.1

2 years ago

2.0.0-beta.2

2 years ago

2.0.2-beta.2

2 years ago

2.0.0-beta.1

2 years ago

2.0.0-beta.5

2 years ago

2.0.0-beta.4

2 years ago

2.0.0-beta.3

2 years ago

1.5.4

3 years ago

1.5.3

3 years ago

1.5.2

4 years ago

1.5.1

4 years ago

1.5.0

4 years ago

1.4.9

4 years ago

1.4.8

4 years ago

1.4.6

4 years ago

1.4.7

4 years ago

1.4.5

4 years ago

1.4.4

4 years ago

1.4.3

5 years ago

1.4.2

5 years ago

1.4.1

5 years ago

1.4.0

5 years ago

1.3.9

5 years ago

1.3.8

5 years ago

1.3.7

5 years ago

1.3.6

5 years ago

1.3.5

5 years ago

1.3.4

5 years ago

1.3.3

5 years ago

1.3.2

5 years ago

1.3.1

5 years ago

1.2.5

5 years ago

1.3.0

5 years ago

1.2.4

5 years ago

1.2.3

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.21

5 years ago

1.1.19

5 years ago

1.1.20

5 years ago

1.1.16

5 years ago

1.1.15

5 years ago

1.1.18

5 years ago

1.1.17

5 years ago

1.1.14

5 years ago

1.1.13

5 years ago

1.1.12

5 years ago

1.1.11

5 years ago

1.1.9

5 years ago

1.1.10

5 years ago

1.1.8

5 years ago

1.1.6

5 years ago

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.31

5 years ago

1.0.30

5 years ago

1.0.29

5 years ago

1.0.28

5 years ago

1.0.27

5 years ago

1.0.26

5 years ago

1.0.25

5 years ago

1.0.24

5 years ago

1.0.23

5 years ago

1.0.22

5 years ago

1.0.21

5 years ago

1.0.20

5 years ago

1.0.19

5 years ago

1.0.18

5 years ago

1.0.17

5 years ago

1.0.16

5 years ago

1.0.15

5 years ago

1.0.14

5 years ago

1.0.1-3.1

5 years ago

1.0.13

5 years ago

1.0.12

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

3.1.1

5 years ago

0.1.1

6 years ago

0.1.0

6 years ago