1.8.0 • Published 4 months ago

react-baby-form v1.8.0

Weekly downloads
46
License
MIT
Repository
github
Last release
4 months ago

react-baby-form

Easy form for react to use.

Demo

react-antd-form Demo, Base on Antd-Design.

Installation

npm install --save react-baby-form

Usage

import React, { Component, createRef } from 'react';
import BabyForm, { submit } from 'react-baby-form';

class Base extends Components {
  formRef = createRef();

  state = {
    value: {},
  }

  onChangeForm = (value) => {
    this.setState({ value });
  }

  onClickSubmit = () => {
    submit(this.formRef)
      .then(() => console.log('good to go'));
      .catch(errors => console.log('errors', errors));
  }

  renderForm() {
    const { value = {} } = this.state;

    return (
      <Babyform value={value} onChange={this.onChangeForm}>
        <input
          type="text"
          _name="name"
          _title="name"
          _required
          _minLength={6}
          _maxLength={6}
          _pattern={/^[0-9a-zA-Z]*$/g}
          _fn={name => name !== 'Tom'}
          />
        <input
          type="number"
          _name="age"
          _title="age"
          _required
          _min={18}
          _max={100}
          />
      </Babyform>
    );
  }

  renderButton() {
    return (
      <button onClick={this.onClickSubmit}>submit</button>
    );
  }

  render() {
    return (
      <div>
        { this.renderForm() }
        { this.renderButton() }
      </div>
    );
  }
}

API

BabyForm

{
  value: {}, // PropTypes.Object, value from BabyForm
  warning: {}, // PropTypes.Object, warning message from BabyForm
  Container: 'div', // PropTypes.element, The container for render, support React.Fragment.
  onChange: () => value. // PropTypes.func, Trigger when value change.
  onError: () => errors. // PropTypes.func, Trigger when render, return all errors or [].
}

submit

Validate BabyForm and return Promise. Just like this:

  ref => new Promise();

Default warning

{
  maxLength(value, condition, opts) {
    const { _title } = opts;

    return `${_title} too long`;
  },
  minLength(value, condition, opts) {
    const { _title } = opts;

    return `${_title} too short`;
  },
  max(value, condition, opts) {
    const { _title } = opts;

    return `${_title} too big`;
  },
  min(value, condition, opts) {
    const { _title } = opts;

    return `${_title} too small`;
  },
  required(value, condition, opts) {
    const { _title } = opts;

    return `${_title} is required`;
  },
  pattern(value, condition, opts) {
    const { _title } = opts;

    return `${_title} is not in right format`;
  },
  fn(value, condition, opts) {
    const { _title } = opts;

    return `${_title} doesn't work.`;
  },
}

Error structure

{
  key: '', // from '_name',
  value: undefined,
  errors: [
    message: '', // from warning
  ],
}

Children Props

{
  _ignore: false, // PropTypes.bool, ignore this node
  _stop: false, // PropTypes.bool, stop recursive this node children

  _error: false, // PropTypes.bool, save errors to props
  errors: [], // PropTypes.Array, need set _error be true.

  _name: '', // PropTypes.string, attribute in value
  _title: '', // PropTypes.string, to show in error message

  _triggerAttr: 'onChange', // PropTypes.string
  _valueAttr: 'value', // PropTypes.string

  _maxLength: undefined, // PropTypes.number
  _minLength: undefined, // PropTypes.number
  _max: undefined, // PropTypes.number
  _min: undefined, // PropTypes.number
  _required: undefined, // PropTypes.bool
  _pattern: undefined, // RegExp
  _fn: undefined, // PropTypes.function, value => PropTypes.bool, validate it anyway you like
  _warning: undefined, // PropTypes.object, just like warning, custom error message
}
1.8.0

4 months ago

1.7.2

3 years ago

1.7.1

3 years ago

1.6.3

3 years ago

1.7.0

3 years ago

1.6.2

3 years ago

1.6.1

3 years ago

1.6.0

3 years ago

1.6.0-alpha.5

3 years ago

1.6.0-alpha.4

3 years ago

1.6.0-alpha.1

3 years ago

1.6.0-alpha.3

3 years ago

1.6.0-alpha.2

3 years ago

1.5.1

3 years ago

1.5.0

3 years ago

1.4.4

3 years ago

1.4.3

3 years ago

1.3.7

3 years ago

1.3.6

3 years ago

1.4.1

3 years ago

1.4.0

3 years ago

1.3.5

3 years ago

1.3.4

3 years ago

1.3.3

3 years ago

1.3.2

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

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