1.0.14 • Published 4 years ago

react-form-dynamic v1.0.14

Weekly downloads
56
License
MIT
Repository
github
Last release
4 years ago

Generate form from JSON

Declare fields

const fields = [
  {
    name: 'firstName',
    label: 'First Name',
    type: 'text',
    validations: [
      {
        rule: 'required',
        params: ['Required'],
      }
    ],
  },
  {
    name: 'email',
    label: 'Email',
    type: 'text',
    validations: [
      {
        rule: 'required',
      },
      {
        rule: 'email',
        params: ['Incorrect email'],
      },
    ],
  },
  {
    name: 'password1',
    label: 'Password',
    type: 'password',
    validations: [
      {
        rule: 'required',
      },
      {
        rule: 'min',
        params: [2, 'Too short'],
      },
      {
        rule: 'max',
        params: [8, 'Too Long'],
      },
    ],
  },
  {
    name: 'password2',
    label: 'Password',
    type: 'password',
    validations: [
      {
        rule: 'fields-match',
        params: ['password1', 'Do not much'],
      },
    ],
  },
  {
    name: 'state',
    label: 'State',
    type: 'select',
    options: states.map(s => ({ value: s.abbr, label: s.name })),
    validations: [
      {
        rule: 'required',
      },
    ],
  },
  {
    name: 'submit',
    label: 'Submit',
    type: 'element',
    element: (<button>submit</button>),
  },
];

Use hook

import { useGeneratedForm } from 'react-form-dynamic';
const App = () => {
  const handleSubmit = () => {
    console.log('Form submitted!');
  };
  const [formElements, formik] = useGeneratedForm(fields, handleSubmit);
  return (
    <form onSubmit={formik.handleSubmit}>
      {formElements}
    </form>
  );
};

useForm hook

You can generate form manualy using useForm hook

import { useForm } from 'react-form-dynamic';

const fields = [
  {
    name: 'firstName',
    label: 'First Name',
    validations: [
      {
        rule: 'required',
        params: ['Required'], // Error message will be "First Name is a required field"
      }
    ],
  },
  {
    name: 'lastName',
    label: 'Last Name',
    validations: [
      {
        rule: 'required',
        params: ['Required', 'Last Name is strictly required'], // Custom error message
      }
    ],
  },
];

const App = () => {
  const onSubmit = () => {
    console.log('Form submitted!');
  };
  const formik = useForm({fields, onSubmit});
  return (
    <form onSubmit={formik.handleSubmit}>
      <Input name="firstName" formik={formik}>
      <Input name="lastName" formik={formik}>
    </form>
  );
};

fields structure

fields

FieldDescription
typeType of the form elementselect, text, number, email, element...
nameName of the element.Also used as a key
initialValueDefault value of the field
labelLabel of the form element
validationsAn array with the list of validations
elementReact.element. Used when type is elementEx. (<button>submit</button>)

validation

FieldDescription
ruleValidation rule's name. Mostly using yup rules.Possible values: required, min, max, fields-match...
paramsParam for rule. Can be custom error message.
1.0.14

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago