1.1.93 • Published 4 years ago

react-basic-form v1.1.93

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

react-basic-form


Form builder for React

NPM version npm download

Install

npm install --save react-basic-form

react-basic-form

Usage

Example 1

import React from 'react';
import { render } from 'react-dom';
import Form from 'react-basic-form';
// import 'react-basic-form/dist/style.css'; //optional

render(
  <Form
    onSubmit={data => console.log(data)}
    validations={{
      email: value => emailRegex.test(value),
    }}
    errorMessages={{
      email: 'Please check your email address.',
    }}
  >
    <Form.Element label="Full Name" name="fullname" required />
    <Form.Element label="E-mail" name="email" type="email" required />
    <Form.Submit />
  </Form>,
  container,
);

Example 2

import React from 'react';
import { render } from 'react-dom';
import Form from 'react-basic-form';

render(
  <Form onSubmit={data => console.log(data)}>
    <Form.Element>
      {({ showErrorMessage, onChange }) => (
        <div>
          <input name="fullname" type="text" onChange={onChange} placeholder="Full Name" required />
          {showErrorMessage('fullname')}
        </div>
      )}
    </Form.Element>
    <Form.Submit>
      {({ isLoading }) => (
        <button type="submit" disabled={isLoading}>
          {isLoading ? 'Sending' : 'Send'}
        </button>
      )}
    </Form.Submit>
  </Form>,
  container,
);

Example 3

import React from 'react';
import { render } from 'react-dom';
import Form from 'react-basic-form';

render(
  <Form onSubmit={data => console.log(data)}>
    {({ showErrorMessage, onChange, isLoading }) => (
      <div>
        <div>
          <input name="fullname" type="text" onChange={onChange} placeholder="Full Name" required />
          {showErrorMessage('fullname')}
        </div>
        <button type="submit" disabled={isLoading}>
          Send
        </button>
      </div>
    )}
  </Form>,
  container,
);

Example 4

import React from 'react';
import { render } from 'react-dom';
import Form from 'react-basic-form';

render(
  <Form
    onSubmit={(data, submitState) => {
      submitState.start(); // isLoading true
      setTimeout(() => {
        console.log(data);
        submitState.end(); // form reset && isLoading = false
      }, 1000);
    }}
  >
    <Form.Element label="Full Name" name="fullname" required />
    <Form.Submit />
  </Form>,
  container,
);

API

Form props

NameTypeDefaultDescription
onSubmit(values, submitState) => {}form submission handler
validationsObject{}validation rules for each form field
errorMessagesObject{}error messages for each form field
defaultErrorMessagestring'This field is required.'default error message for form fields without defined error message
childrenReact.Node or functionif children is a function, this function should return the JSX which contains the form and all inputs

Form.Element props

NameTypeDefaultDescription
optionsArray(string) or Array(shape({label, value}))if type prop includes select,radio or checkbox, this prop is required
labelstringif this prop is filled, Form.Element will create a label tag with this string
childrenReact.Node or functionif children is a function, this function should return the JSX which contains the form and all inputs

Form.Submit props

NameTypeDefaultDescription
textstring'Send'button text of the form
loadingTextstring'Sending'button text of the form when submit state is loading
childrenReact.Node or functionif children is a function, this function should return the JSX which contains the form and all inputs

children function arguments

NameTypeDescription
showErrorMessagefunctionit takes form name as an argument and shows error message for this field
onChangefunctionfunction that controls validation errors on field value change
isLoadingbooleanshow submit state condition whether it's loading or not

Development

npm install
npm start

Example

npm start and then go to http://localhost:3001/

License

react-basic-form is released under the MIT license.

1.1.93

4 years ago

1.1.92

4 years ago

1.1.91

5 years ago

1.1.9

5 years ago

1.1.8

5 years ago

1.1.7

5 years ago

1.1.6

5 years ago

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.4

5 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.1

8 years ago