0.0.1 • Published 4 years ago

dynamic-form-blundert v0.0.1

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

NPM Contributors Forks Stargazers Issues MIT License LinkedIn

Table of Contents

About The Project

This project implements a form generator with React. Using mainly React Hooks.

Built With

Getting Started

To start to use the library, follow these semple steps.

Prerequisites

This is an example of how to list things you need to use the software and how to install them.

npm i react react-dom react-jss

Installation

npm i dynamic-form-blundert

Usage

Create a configuration file:

// config.js
export default     
{
  name: "email",
  label: "Email",
  helperText: "Write your email",
  tag: "input",
  type: "email",
  validations: [
    {
      kind: "required",
      message: "Email is required"
    },
    {
      kind: "pattern",
      reg: /^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/,
      considerRegAs: "positive",
      message: "You must type an email i.e. test@test.com"
    }
  ]
};
import React, { useRef } from "react";
import PropTypes from "prop-types";

import {
  DynamicForm,
  applyCrypt2State,
  useDynamicForm,
  DynamicFormProvider
} from "dynamic-form";

import { form as formConfig } from "./config.js";

function Example(props) {

  const stateFromService = useDynamicForm("state", "model");
  const errorFromService = useDynamicForm("state", "error");
  const { _globalErrors } = errorFromService;

  const onSubmit = event => {

    if (_globalErrors === 0) {
      console.log(stateFromService);
    }

    event.preventDefault();
  };

  // Render
  return (
    <DynamicFormProvider>

      <section>

        {_globalErrors === 0 ? <p>Form contains error.</p>}

        <form onSubmit={onSubmit}>
          <DynamicForm
            config={formConfig}
            ref={childRef}
            updateModelAtBlur={true}
            debug={true}
          />

          <button
            type="submit"
            onClick={onSubmit}
          >
            Confirm
          </button>
        </form>

      </section>

    </DynamicFormProvider>
  );
}

Example.propTypes = {
  classes: PropTypes.object.isRequired
};

export default Example;

For more examples, please refer to the Documentation

Roadmap

See the open issues for a list of proposed features (and known issues).

Contributing

Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.

  1. Fork the Project;
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature);
  3. Commit your Changes (git commit -m 'Add some AmazingFeature');
  4. Push to the Branch (git push origin feature/AmazingFeature);
  5. Open a Pull Request.

License

Distributed under the MIT License. See LICENSE for more information.

Contact

Matteo Granzotto - @blundert - matteo.granzotto@wavelop.com

Wavelop - info@wavelop.com

Project Link: https://github.com/Blundert/dynamic-form