0.4.0 • Published 7 years ago

react-form-manager v0.4.0

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

react-form-manager

react-form-manager is a dynamic form for react with validation and data posting support with customizable input components.

Installation

$ npm i --save react-form-manager

Example

The simplest use case

import React, { Component } from 'react';
import Form from "react-form-manager";
import 'semantic-ui-css/semantic.css';

const Separator = (self) => (<h4 style={{margin: "3rem 0 -1rem 0"}}>{self.props.title}</h4>);

const modelAttributes = [
    [
        { name: "name", type: "Text", label:"Name", placeholder: "Enter your name", required: true },
        { name: "title", type: "Deferred", label: "Title", required: true, minLength: 3 },
    ],
    [
        { name: "sex", type: "Radio", label: "Sex", required: true, options: [
            {value: 'male', text: "Male"},
            {value:'female', text: "Female"}
        ]},
        { name: "age", type: "Number", label: "Age", required: true},
        { name: "dob", type: "Date", label: "Date of Birth", required: true, min:'1991-03-28', max: '1991-03-31'},
        { name: "human", type: "Boolean", label: "Is Human", required: true, verticalAlign: 'middle' },
    ],
    { name: "sn", type: "Number", label: "SN", placeholder: "Your Serial Number", max: 999, min: 100 },
    [
        { name: "hobbies", type: "Select", label: "Hobbies", options: [
            {value:'foot', text:'Football'},
            {value:'base', text:'Baseball'},
            {value:'rug', text:'Rugby'},
            {value:'dev', text:'Code'},
        ]},
        { name: "select", type: "Select", label: "Select One", options : [
            {value:"one", text: "Option 1"},
            {value:"two", text: "Option 2"},
            {value:"three", text: "Option 3"},
        ], required: true, search:true, multiple:true},
    ],
    <Separator title="Environment" />,
     /*{ name: "environment", type: "Json", label: "Environment", options: [
            {name: "ROOT_URL", required: true, placeholder: 'http://yourApp.techexmachina.com'},
            {name: "MONGO_URL", required: true, placeholder: 'mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]'},
            {name: "SOME_OTHER_VAR"}
        ]},*/
    { name: "description", type: "TextArea", label: "Description" },
    { name: "submit", type: "Submit", label: "Submit", textAlign:"center" }
];

const modelValues = {
    sn: 150,
    dob: '1991-03-30',
    hobbies: 'dev',
    select: ['one', 'two'],
    description: "hola!",
    human: true
};

class App extends Component {
    handleSubmit = (e, values) => {
        e.preventDefault();

        console.log(values);
    }

    render() {
        return (
            <div className="App">
                <Form onSubmit={this.handleSubmit} name="semantic" attributes={modelAttributes} values={modelValues} material={false} />
            </div>
        );
    }
}

export default App;

Form Props

FormatRequiredWhat it does ?
namestringYESThe name of your form
onSubmitfunctionNOA callback used when the form was submitted. Receives SyntheticEvent and an object with values { name: value }
attributesarrayYESArray of Object to construct the form. You can group inputs on the same line with array in this array.
valuesobjectNOAn object to initialize the form { name: value }
errorHeaderstringNOCustom header of error box
langstringNOThe lang used for default error messages.
materialbooleanYESIf true, the form will be display with material-ui instead semantic.

You can submit the form with ref: call ref.submit();

Attributes Props

FormatRequiredWhat it does ?
namestringYESThe name of this input.
labelstringYESThe label of this input.
typestringYESOne of Text, TextArea, Number, Radio, Boolean, Date, Select, Json, File (see below).
requiredbooleanNOThis input can be required or not.
textAlignstringNOText Alignment in Grid. One of left, center or right.
verticalAlignstringNOVertical Alignment in Grid. One of left, center or right.

Attributes can be a React Element directly, to use separator for example.

Text element

FormatRequiredWhat it does ?
placeholderstringNOOnly if type is Text. The placeholder for this input.
minLengthstringNOThe mix length of strings for this input.
maxLengthstringNOThe max length of strings for this input.
disabledOnEditbooleanNOPut in readonly if there is a init value
regexpRegExpNOA Regular Expression to validate this input
regexpMsgstringNOA message which display when this input was not validate by regexp

Number element

FormatRequiredWhat it does ?
minnumberNOThe minimum number (number).
maxnumberNOThe maximum number (number).

Date element

FormatRequiredWhat it does ?
minstringNOThe minimum date (string Format: YYYY-MM-DD).
maxstringNOThe maximum date (string Format: YYYY-MM-DD).

TextArea element

FormatRequiredWhat it does ?
minLengthstringNOThe mix length of strings for this input.
maxLengthstringNOThe max length of strings for this input.

Select element

FormatRequiredWhat it does ?
multiplestringNOMultiple selection or not.
searchstringNOThe select can be searchable or not. This can't be used with material.
options[object]YESAn Array of object to construct the select { value: 'some', text: 'example' } or a function which return the same structure to live update the select input

Json element

A Json element is useful to display a gui to construct a json string which will be return on form submit.

FormatRequiredWhat it does ?
options[object]YESAn Array of object to construct the json { name: 'some', placeholder: 'example', required: true, regexp: myRegexp, regexpMsg: 'Some Error Msg', readOnly: false }
requiredbooleanNOIf true, the Json must have at least one value

File element

A File element can upload a file on AWS s3 bucket.

FormatRequiredWhat it does ?
pathstringYESpath in bucket where store the file
publicReadbooleanYESIf true, the file have a public URL. If false, the file will be private
acceptstringYESMime type to filter sended file
s3GetSignedUrlfunctionYESThe function you provide should take file and callback arguments. Callback should be called with an object containing signedUrl key.
s3DeleteObjectfunctionYESA function to delete the object on aws. The function you provide should take the public link argument. Must return a promise

Customizing the Form

Alternatively you can use Field to define a input. @TODO Needs some extra examples

  import React from 'react';
  import { render } from 'react-dom';
  import Form, { Field } from 'react-form-manager';
  import 'semantic-ui-css/semantic.css';

  render(
      <Form>
        <Field type="Text" name="username" label="Username" required />,
      </Form>
  ,document.body);
0.4.0

7 years ago

0.3.19

7 years ago

0.3.18

7 years ago

0.3.17

7 years ago

0.3.16

7 years ago

0.3.15

7 years ago

0.3.14

7 years ago

0.3.13

7 years ago

0.3.12

7 years ago

0.3.11

7 years ago

0.3.10

7 years ago

0.3.9

7 years ago

0.3.8

7 years ago

0.3.7

7 years ago

0.3.6

7 years ago

0.3.5

7 years ago

0.3.4

7 years ago

0.3.3

7 years ago

0.3.2

7 years ago

0.3.1

7 years ago

0.3.0

7 years ago

0.2.0

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago