0.0.23 • Published 4 years ago

rj-form v0.0.23

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

rj-form

A React JS library for creating forms using a JSON schema

NPM JavaScript Style Guide

Install

npm install --save rj-form

Usage

import React, { Component } from "react";
import RJForm from "rj-form";
import { isRequired } from "./util/validations";
import Api from "./util/api";

export default class App extends Component {
  state = { isLoading: false, options: [] };

  componentDidMount() {
    Api.get("posts").subscribe(
      data => {
        console.log(data.data);
        this.setState({ options: data.data });
      },
      error => {
        if (error.response) {
          console.log(error.response);
        }
      }
    );
  }

  handleUpload = val => {
    console.log(val);
  };

  setFormApi = api => {
    this.formApi = api;
  };

  handleSubmit = value => {
    console.log(value);
    this.formApi.reset();
  };

  isRequired(value) {
    return value === null ||
      value === undefined ||
      ("" + value).trim().length <= 0
      ? "This field is required"
      : undefined;
  }

  validateFields = values => {
    return {
      name: isRequired(values.name),
      date: isRequired(values.date),
      city: isRequired(values.city)
    };
  };

  handleClick = () => {
    console.log("Click");
  };

  handleChange = val => {};

  render() {
    let isLoading = this.state.isLoading;

    return (
      <RJForm
        handleUpload={this.handleUpload}
        options={this.state.options}
        handleChange={this.handleChange}
        isLoading={isLoading}
        getFormApi={this.setFormApi}
        validateFields={this.validateFields}
        handleSubmit={this.handleSubmit}
        formData={DATA}
      ></RJForm>
    );
  }
}

const DATA = {
  styles: {
    formClassName: "column m-2",
    fieldClassName: "form-control",
    containerClassName: "w-100 my-4"
  },
  fields: [
    {
      key: "name",
      placeholder: "Name",
      type: "text",
      maxLength: 4
    },
    {
      key: "date",
      placeholder: "Date",
      type: "date",
      className: "form-control"
    },
    {
      key: "country",
      type: "select",
      labelKey: "userId",
      valueKey: "id"
    },
    {
      key: "city",
      type: "select",
      placeholder: "City",
      valueKey: "name",
      labelKey: "name",
      options: [{ name: "Delhi" }, { name: "Kolkata" }]
    },

    {
      key: "desc",
      type: "text-area"
    },

    {
      key: "button",
      text: "Submit",
      type: "submit",
      className: "col-md-8"
    }
  ]
};

License

MIT © drudrapaul

0.0.23

4 years ago

0.0.22

4 years ago

0.0.21

4 years ago

0.0.20

5 years ago

0.0.19

5 years ago

0.0.18

5 years ago

0.0.17

5 years ago

0.0.16

5 years ago

0.0.15

5 years ago

0.0.14

5 years ago

0.0.13

5 years ago

0.0.12

5 years ago

0.0.11

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago

1.0.0

5 years ago