Intuitive react forms for building powerful applications
npm install react-form-controlledIntuitive react forms for building powerful applications.
All components are controlled
That means form is always showing the current state and data are immutable.
Each form has own internal state that means you can skip onChange event.
If you will change the value prop of the form component it will change the state of the form immediately.
[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][coveralls-image]][coveralls-url]
[npm-image]: https://img.shields.io/npm/v/react-form-controlled.svg?style=flat-square
[npm-url]: https://www.npmjs.com/react-form-controlled
[travis-image]: https://img.shields.io/travis/seeden/react-form-controlled/master.svg?style=flat-square
[travis-url]: https://travis-ci.org/seeden/react-form-controlled
[coveralls-image]: https://img.shields.io/coveralls/seeden/react-form-controlled/master.svg?style=flat-square
[coveralls-url]: https://coveralls.io/r/seeden/react-form-controlled?branch=master
[github-url]: https://github.com/seeden/react-form-controlled
- Immutable data
- Controlled behavior (support for "uncontrolled" behaviour)
- Build on latest standards ES6 and promises
- Support for isomorphic application
- You are able to use forms without special components
- Support for arrays/lists and indexes
- Standard html elements like an input, select, textarea and fieldset (arrays)
- Custom components and support for 3rd party libraries
- Validation
- Tests and coverage
Star this project on [GitHub][github-url].
``js
import React, { Component } from 'react';
import Form from 'react-form-controlled';
export default class Example extends Component {
constructor(props, context) {
super(props, context);
this.formData = {
firstName: null,
lastName: null
};
}
onSubmit = (data) => {
alert(Hi ${data.firstName} ${data.lastName});
}
render() {
return (
);
}
}
`
Value is automatically added as prop to the inputs. When you will change it it will reload whole form (controlled form, but this is the work for React).
`js
import React, { Component } from 'react';
import Form from 'react-form-controlled';
export default class Example extends Component {
constructor(props, context) {
super(props, context);
this.state = {
users: [{
firstName: 'Zlatko'
}, {
firstName: 'Livia'
}]
};
}
onChange = (data) => {
this.setState(data);
}
onSubmit = (data) => {
alert(Hi ${data.users[0].firstName});
}
render() {
return (
);
}
}
`
`js
import React, { Component } from 'react';
import Form from 'react-form-controlled';
export default class Example extends Component {
constructor(props, context) {
super(props, context);
this.state = {
users: [{
firstName: 'Zlatko'
}, {
firstName: 'Livia'
}]
};
}
onChange = (data) => {
this.setState(data);
}
onSubmit = (data) => {
alert(Hi ${data.users[0].firstName});
}
render() {
return (
);
}
}
`
If you are using fieldset with simple array do not enter the name attribute.
`js
import React, { Component } from 'react';
import Form from 'react-form-controlled';
export default class Example extends Component {
constructor(props, context) {
super(props, context);
this.state = {
items: [123, 222]
};
}
onSubmit = (data) => {
alert(Hi ${data.users[0].firstName});
}
render() {
return (
);
}
}
`
If you want to use complex names you can use dot or array notation.
`js
import React, { Component } from 'react';
import Form from 'react-form-controlled';
export default class Example extends Component {
constructor(props, context) {
super(props, context);
this.state = {
users: [{
firstName: 'Zlatko',
stats: {
followers: 10,
},
}, {
firstName: 'Livia',
stats: {
followers: 22,
},
}]
};
}
onSubmit = (data) => {
alert(Hi ${data.users[0].firstName});
}
render() {
return (
);
}
}
`
or you can use one more fieldset
`js
import React, { Component } from 'react';
import Form from 'react-form-controlled';
export default class Example extends Component {
constructor(props, context) {
super(props, context);
this.state = {
users: [{
firstName: 'Zlatko',
stats: {
followers: 10,
},
}, {
firstName: 'Livia',
stats: {
followers: 22,
},
}]
};
}
onSubmit = (data) => {
alert(Hi ${data.users[0].firstName});
}
render() {
return (
);
}
}
`
If you are using arrays with fieldset you want to use indexes.
#### render: function
Instead of having a component rendered for you, you can pass in a function. Your render function will be called with the same props that are passed to the component.
`js
import React, { Component } from 'react';
import Form, { Index } from 'react-form-controlled';
export default class Component extends Component {
constructor(props, context) {
super(props, context);
this.state = {
users: [{
firstName: 'Zlatko',
}, {
firstName: 'Livia',
}]
};
}
onSubmit = (data) => {
alert(Hi ${data.users[0].firstName});
}
render() {
return (
);
}
}
`
You can use value from parent with dot notation. Example ".selected"
`js
import React, { Component } from 'react';
import Form, { Index } from 'react-form-controlled';
export default class Component extends Component {
constructor(props, context) {
super(props, context);
this.state = {
options: ['dog', 'mouse', 'cat'],
selected: 1,
};
}
onSubmit = (data) => {
alert(Selected option is ${data.options[data.selected]});
}
render() {
return (
);
}
}
`
Integration is very easy you can use Integrate component. Here is example with react-select library.
`js
import React, { Component } from 'react';
import Form, { Integrate } from 'react-form-controlled';
import Select from 'react-select';
export default class Component extends Component {
onSubmit = (data) => {
alert(Selected option is ${data.selected});
}
render() {
const options = [
{ value: 'one', label: 'One' },
{ value: 'two', label: 'Two' }
];
return (
);
}
}
`
`js
import React, { Component } from 'react';
import Form, { Remove } from 'react-form-controlled';
export default class Component extends Component {
constructor(props, context) {
super(props, context);
this.state = {
users: [{
firstName: 'Zlatko',
}, {
firstName: 'Livia',
}]
};
}
onSubmit = (data) => {
alert(Hi ${data.users[0].firstName});
}
render() {
return (
);
}
}
`
Remove, Up and Down components has same properties like index. You can use render and component property as well.
`js
import React, { Component } from 'react';
import Form, { Up, Down } from 'react-form-controlled';
export default class Component extends Component {
constructor(props, context) {
super(props, context);
this.state = {
users: [{
firstName: 'Zlatko',
}, {
firstName: 'Livia',
}]
};
}
onSubmit = (data) => {
alert(Hi ${data.users[0].firstName});
}
render() {
return (
);
}
}
`
You can simply handle working state and show loading indicator. Form property onSubmit is based on promises. During you processing of this callback is form in the "isWorking" state.
If the form is in the isWorking state you are not able to submit form again.
`js
import React, { Component } from 'react';
import Form, { Working } from 'react-form-controlled';
export default class Component extends Component {
constructor(props, context) {
super(props, context);
this.state = {
users: [{
firstName: 'Zlatko',
}, {
firstName: 'Livia',
}]
};
}
onSubmit = async (data) => {
return new Promise((resolve) => {
alert(Hi ${data.users[0].firstName});
setTimeout(resolve, 3000);
});
}
render() {
return (
/>
);
}
}
`
Try to image simple quiz with questions and answers. Y
If you want to disable autoreplace of the standard components like an input, select, textarea etc...
You can disable this behavior with the form parameter skipReplace.
This feature is great if you want to use this library with other 3rd libraries.
You will be able to use Input, Select, Textarea and Fieldset.
`js
import Form, { Input, Select, Textarea, Fieldset } from from 'react-form-controlled';
export default class Component extends Component {
constructor(props, context) {
super(props, context);
this.state = {
users: [{
firstName: 'Zlatko',
}, {
firstName: 'Livia',
}]
};
}
onSubmit(data) {
alert(Hi ${data.users[0].firstName});
}
render() {
return (
);
}
}
`
This part is moved to another library named react-form-controlled-validate
Yes, you can use JSON schema as property to the form. Why JSON schema? Because it is a standard.
`js
const schema = {
type: "object",
properties: {
firstName: {
type: "string"
},
lastName: {
type: "string"
}
}
};