Giter Site home page Giter Site logo

cmolenda / mobx-react-form Goto Github PK

View Code? Open in Web Editor NEW

This project forked from foxhound87/mobx-react-form

0.0 0.0 0.0 4.15 MB

Reactive MobX Form State Management

Home Page: https://foxhound87.github.io/mobx-react-form

License: MIT License

JavaScript 0.31% TypeScript 98.72% HTML 0.96%

mobx-react-form's Introduction

MobX React Form

Reactive MobX Form State Management

NPM

GitHub Workflow Status (with branch) GitHub release (latest by date) npm bundle size Codecov Coverage node GitHub license GitHub closed pull requests GitHub closed issues Downloads Downloads Backers on Open Collective Sponsors on Open Collective


Features


Quick Start

Edit form-quickstart

npm install --save mobx-react-form

Choose and Setup a Validation Plugin

Below we are creating a plugins object using the validatorjs package to enable DVR functionalities (Declarative Validation Rules).

import dvr from 'mobx-react-form/lib/validators/DVR';
import validatorjs from 'validatorjs';

const plugins = {
  dvr: dvr(validatorjs)
};

See Validation Plugins for more info on supported packages.

Define the Form Fields

Define the fields as a collection with a rules property for the validation.

const fields = [{
  name: 'email',
  label: 'Email',
  placeholder: 'Insert Email',
  rules: 'required|email|string|between:5,25',
}, {
  name: 'password',
  label: 'Password',
  placeholder: 'Insert Password',
  rules: 'required|string|between:5,25',
  type: 'password',
}, {
  name: 'passwordConfirm',
  label: 'Password Confirmation',
  placeholder: 'Confirm Password',
  rules: 'required|string|same:password',
  type: 'password',
}];

See Fields Definitions and all available Field Props on the docs.

Define the Validation Hooks

const hooks = {
  onSuccess(form) {
    alert('Form is valid! Send the request here.');
    // get field values
    console.log('Form Values!', form.values());
  },
  onError(form) {
    alert('Form has errors!');
    // get all form errors
    console.log('All form errors', form.errors());
  }
}

See more on the docs about the Validation Hooks and the Event Hooks

Initialize the Form

Simply pass the fields, plugins and hooks objects to the constructor

import MobxReactForm from 'mobx-react-form';

const myForm = new MobxReactForm({ fields }, { plugins, hooks });

Learn more on the docs about the Form Instance

Pass the myForm to a react component

The package provide some built-in and ready to use Event Handlers:

onSubmit(e), onClear(e), onReset(e) & more...

import React from 'react';
import { observer } from 'mobx-react';

export default observer(({ myForm }) => (
  <form onSubmit={myForm.onSubmit}>
    <label htmlFor={myForm.$('email').id}>
      {myForm.$('email').label}
    </label>
    <input {...myForm.$('email').bind()} />
    <p>{myForm.$('email').error}</p>

    {/* ... other inputs ... */}

    <button type="submit" onClick={myForm.onSubmit}>Submit</button>
    <button type="button" onClick={myForm.onClear}>Clear</button>
    <button type="button" onClick={myForm.onReset}>Reset</button>

    <p>{myForm.error}</p>
  </form>
));

See more on the docs about the Field Props Bindings

Extending the Form class

See how to implement the same configuration of this quickstart extending the Form class


Contributing

  1. Fork the repository
  2. Make applicable changes (with tests!)
  3. To run tests: npm run test
  4. Ensure builds succeed: npm run build
  5. Commit and run pre-commit checks: npm run commit

New Issues

When opening new issues, provide the setup of your form in a CodeSandbox.

These issues, and the ones which provides also PR with failing tests will get higher priority.

Contributors

This project exists thanks to all the people who contribute.

Backers

Thank you to all our backers! 🙏 [Become a backer]

Sponsors

Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]

mobx-react-form's People

Contributors

foxhound87 avatar greenkeeperio-bot avatar riceyeh avatar vkrol avatar dakkers avatar rdgco avatar yuan-raken avatar sdsleon avatar danconnell avatar patrykkopycinski avatar royriojas avatar anotherpit avatar mprobber avatar ajorkowski avatar wwahammy avatar zetoke avatar pocheptsov avatar liveresume avatar levic92 avatar monkeywithacupcake avatar dependabot[bot] avatar dalcib avatar tonyjiang88215 avatar tchock avatar necrifede avatar mmoczulski avatar mattia46 avatar leesiongchan avatar krisrp avatar nodios avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.