Skip to content
Branch: master
Go to file
Code

Latest commit

rkuykendall and dependabot-preview committed 1261bff Jun 23, 2020
Automatic library bumps (#511)
* chore(deps-dev): Bump @babel/core from 7.8.4 to 7.8.6 (#404)

* chore(deps-dev): Bump @types/react from 16.9.19 to 16.9.23 (#406)

* chore(deps-dev): Bump @types/jest from 25.1.2 to 25.1.3 (#407)

* chore(deps-dev): Bump rollup from 1.31.1 to 1.32.0 (#408)

* chore(deps-dev): Bump sinon from 8.1.1 to 9.0.0 (#409)

* chore(deps-dev): Bump react-dom from 16.12.0 to 16.13.0 (#410)

* chore(deps-dev): Bump @typescript-eslint/eslint-plugin (#411)

* chore(deps-dev): Bump @babel/preset-env from 7.8.4 to 7.8.6 (#405)

* chore(deps-dev): Bump babel-eslint from 10.0.3 to 10.1.0 (#414)

* chore(deps-dev): Bump react from 16.12.0 to 16.13.0 (#412)

* chore(deps-dev): Bump np from 5.2.1 to 6.2.0 (#413)

* Revert "chore(deps-dev): Bump np from 5.2.1 to 6.2.0 (#413)"

This reverts commit 7b53d66.

* chore(deps-dev): Bump @typescript-eslint/parser from 2.19.2 to 2.21.0 (#418)

* chore(deps-dev): Bump @types/react from 16.9.19 to 16.9.23 (#417)

* chore(deps): [Security] Bump acorn from 6.4.0 to 6.4.1 (#422)

Bumps [acorn](https://github.com/acornjs/acorn) from 6.4.0 to 6.4.1. **This update includes a security fix.**
- [Release notes](https://github.com/acornjs/acorn/releases)
- [Commits](acornjs/acorn@6.4.0...6.4.1)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>

* chore(deps-dev): Bump @babel/preset-typescript from 7.8.3 to 7.9.0 (#426)

* chore(deps-dev): Bump prettier from 1.19.1 to 2.0.2 (#428)

* chore(deps-dev): Bump @types/react-dom from 16.9.5 to 16.9.6 (#430)

* chore(deps-dev): Bump @typescript-eslint/parser from 2.21.0 to 2.26.0 (#429)

* chore(deps-dev): Bump babel-loader from 8.0.6 to 8.1.0 (#431)

* chore(deps-dev): Bump @typescript-eslint/eslint-plugin (#432)

* chore(deps-dev): Bump @babel/preset-env from 7.8.6 to 7.9.0 (#433)

* chore(deps-dev): Bump @types/jest from 25.1.3 to 25.1.4 (#434)

* chore(deps-dev): Bump sinon from 9.0.0 to 9.0.1 (#436)

* chore(deps-dev): Bump rollup from 1.32.0 to 1.32.1 (#437)

* chore(deps-dev): Bump @types/react from 16.9.23 to 16.9.31 (#435)

* chore(deps-dev): Bump @babel/preset-react from 7.8.3 to 7.9.4 (#438)

* chore(deps-dev): Bump @types/react from 16.9.31 to 16.9.34 (#439)

* chore(deps-dev): Bump @rollup/plugin-node-resolve from 7.1.1 to 7.1.3 (#441)

* chore(deps-dev): Bump sinon from 9.0.1 to 9.0.2 (#442)

* chore(deps-dev): Bump @types/jest from 25.1.4 to 25.2.1 (#443)

* chore(deps-dev): Bump @types/react-dom from 16.9.6 to 16.9.7 (#444)

* chore(deps-dev): Bump @typescript-eslint/eslint-plugin (#446)

* chore(deps-dev): Bump coveralls from 3.0.9 to 3.1.0 (#448)

* chore(deps-dev): Bump @babel/core from 7.8.6 to 7.9.6 (#451)

* chore(deps-dev): Bump @typescript-eslint/parser from 2.26.0 to 2.30.0 (#454)

* chore(deps-dev): Bump react from 16.13.0 to 16.13.1 (#440)

* chore(deps-dev): Bump @babel/preset-env from 7.9.0 to 7.9.6 (#453)

* chore(deps-dev): Bump babel-jest from 25.5.1 to 26.0.1 (#457)

* chore(deps-dev): Bump @babel/node from 7.8.4 to 7.8.7 (#461)

* chore(deps-dev): Bump react-dom from 16.13.0 to 16.13.1 (#465)

* chore(deps-dev): Bump @typescript-eslint/parser from 2.30.0 to 2.31.0 (#464)

* chore(deps-dev): Bump @typescript-eslint/eslint-plugin (#460)

* chore(deps-dev): Bump rollup from 1.32.1 to 2.7.6 (#463)

* chore(deps-dev): Bump prettier from 2.0.2 to 2.0.5 (#467)

* chore(deps-dev): Bump jest from 25.1.0 to 26.0.1 (#458)

* Reset yarn.lock

* Run format

* chore(deps-dev): Bump @typescript-eslint/parser from 2.31.0 to 2.34.0 (#474)

* chore(deps-dev): Bump @types/react-dom from 16.9.7 to 16.9.8 (#475)

* chore(deps-dev): Bump @rollup/plugin-commonjs from 11.1.0 to 12.0.0 (#476)

* chore(deps-dev): Bump @babel/cli from 7.8.4 to 7.10.1 (#477)

* chore(deps-dev): Bump @babel/preset-react from 7.9.4 to 7.10.1 (#485)

* chore(deps-dev): Bump @babel/preset-env from 7.9.6 to 7.10.2 (#481)

* chore(deps-dev): Bump typescript from 3.8.3 to 3.9.3 (#484)

* chore(deps-dev): Bump @babel/preset-typescript from 7.9.0 to 7.10.1 (#486)

* chore(deps-dev): Bump rollup from 2.7.6 to 2.17.1 (#492)

* chore(deps-dev): Bump @typescript-eslint/eslint-plugin (#480)

* chore(deps-dev): Bump eslint-plugin-react from 7.19.0 to 7.20.0 (#479)

* chore(deps-dev): Bump @rollup/plugin-node-resolve from 7.1.3 to 8.0.1 (#493)

* chore(deps-dev): Bump @babel/node from 7.8.7 to 7.10.3 (#494)

* chore(deps-dev): Bump auto-changelog from 2.0.0 to 2.1.0 (#495)

* chore(deps-dev): Bump @rollup/plugin-commonjs from 12.0.0 to 13.0.0 (#496)

* chore(deps-dev): Bump eslint-plugin-import from 2.20.2 to 2.21.2 (#498)

* chore(deps-dev): Bump @babel/core from 7.9.6 to 7.10.3 (#499)

* chore(deps-dev): Bump @babel/cli from 7.10.1 to 7.10.3 (#503)

* chore(deps-dev): Bump @types/react from 16.9.34 to 16.9.38 (#504)

* chore(deps-dev): Bump eslint-plugin-prettier from 3.1.3 to 3.1.4 (#505)

* chore(deps-dev): Bump @types/jest from 25.2.1 to 26.0.0 (#497)

* chore(deps-dev): Bump typescript from 3.9.3 to 3.9.5 (#500)

* chore(deps-dev): Bump @babel/preset-env from 7.10.2 to 7.10.3 (#501)

* chore(deps-dev): Bump eslint-plugin-jsx-a11y from 6.2.3 to 6.3.1 (#502)

* chore(deps-dev): Bump jest from 26.0.1 to 26.1.0 (#506)

* chore(deps-dev): Bump @rollup/plugin-node-resolve from 8.0.1 to 8.1.0 (#507)

* chore(deps-dev): Bump eslint-config-airbnb from 18.1.0 to 18.2.0 (#508)

* chore(deps-dev): Bump babel-jest from 26.0.1 to 26.1.0 (#510)

* chore(deps-dev): Bump rollup from 2.17.1 to 2.18.0 (#509)

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
src
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

formsy-react

GitHub release Build status Coverage Status Gzipped size GitHub contributors Typescript Types included

A form input builder and validator for React.

Quick Start API 1.x API

Background

christianalfoni wrote an article on forms and validation with React, Nailing that validation with React JS, the result of that was this library.

The main concept is that forms, inputs, and validation are done very differently across developers and projects. This React component aims to be that “sweet spot” between flexibility and reusability.

This project was originally located at christianalfoni/formsy-react if you're looking for old issues.

What You Can Do

  1. Build any kind of form element components. Not just traditional inputs, but anything you want, and get that validation for free
  2. Add validation rules and use them with simple syntax
  3. Use handlers for different states of your form. (onSubmit, onValid, etc.)
  4. Pass external errors to the form to invalidate elements (E.g. a response from a server)
  5. Dynamically add form elements to your form and they will register/unregister to the form

Install

yarn add formsy-react react react-dom and use with webpack, browserify, etc.

Formsy component packages

1.x to 2.x Upgrade Guide

The 2.0 release fixed a number of legacy decisions in the Formsy API, mostly a reliance on function props over value props passed down to wrapped components. However, the API changes are minor and listed below.

  • getErrorMessage() => errorMessage
  • getErrorMessages() => errorMessages
  • getValue() => value
  • hasValue() => hasValue
  • isFormDisabled(): => isFormDisabled
  • isFormSubmitted() => isFormSubmitted
  • isPristine() => isPristine
  • isRequired() => isRequired
  • isValid(): => isValid
  • showError() => showError
  • showRequired() => showRequired

Quick Start

1. Build a Formsy element

// MyInput.js
import { withFormsy } from 'formsy-react';
import React from 'react';

class MyInput extends React.Component {
  constructor(props) {
    super(props);
    this.changeValue = this.changeValue.bind(this);
  }

  changeValue(event) {
    // setValue() will set the value of the component, which in
    // turn will validate it and the rest of the form
    // Important: Don't skip this step. This pattern is required
    // for Formsy to work.
    this.props.setValue(event.currentTarget.value);
  }

  render() {
    // An error message is passed only if the component is invalid
    const errorMessage = this.props.errorMessage;

    return (
      <div>
        <input onChange={this.changeValue} type="text" value={this.props.value || ''} />
        <span>{errorMessage}</span>
      </div>
    );
  }
}

export default withFormsy(MyInput);

withFormsy is a Higher-Order Component that exposes additional props to MyInput. See the API documentation to view a complete list of the props.

2. Use your Formsy element

import Formsy from 'formsy-react';
import React from 'react';
import MyInput from './MyInput';

export default class App extends React.Component {
  constructor(props) {
    super(props);
    this.disableButton = this.disableButton.bind(this);
    this.enableButton = this.enableButton.bind(this);
    this.state = { canSubmit: false };
  }

  disableButton() {
    this.setState({ canSubmit: false });
  }

  enableButton() {
    this.setState({ canSubmit: true });
  }

  submit(model) {
    fetch('http://example.com/', {
      method: 'post',
      body: JSON.stringify(model),
    });
  }

  render() {
    return (
      <Formsy onValidSubmit={this.submit} onValid={this.enableButton} onInvalid={this.disableButton}>
        <MyInput name="email" validations="isEmail" validationError="This is not a valid email" required />
        <button type="submit" disabled={!this.state.canSubmit}>
          Submit
        </button>
      </Formsy>
    );
  }
}

This code results in a form with a submit button that will run the submit method when the form is submitted with a valid email. The submit button is disabled as long as the input is empty (required) and the value is not an email (isEmail). On validation error it will show the message: "This is not a valid email".

Contribute

  • Fork repo
  • yarn
  • yarn lint runs lint checks
  • yarn test runs the tests
  • npm run deploy build and release formsy

Changelog

Check out our Changelog and releases

License

The MIT License (MIT)

You can’t perform that action at this time.