Skip to content

Commit 4bf3970

Browse files
author
bietkul
committed
2 parents a2eea90 + c8ce7b7 commit 4bf3970

File tree

6 files changed

+158
-32
lines changed

6 files changed

+158
-32
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
1+
<p align="center">
2+
<img src="logo.png" alt="React Native Game Engine" height="120" />
3+
</p>
4+
15
# React Reactive Forms
26

37
[![Build Status](https://travis-ci.org/bietkul/react-reactive-form.svg?branch=master)](https://travis-ci.org/bietkul/react-reactive-form)
48
[![NPM Version](https://img.shields.io/npm/v/react-reactive-form.svg?style=flat)](https://www.npmjs.com/package/react-reactive-form)
59
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
10+
<img
11+
alt="gzip size"
12+
src="https://img.shields.io/badge/gzip%20size-7.1%20kB-brightgreen.svg"
13+
/>
14+
<a href="https://github.com/bietkul/react-reactive-form/pulls">
15+
<img
16+
alt="PRs welcome"
17+
src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg"
18+
/>
19+
</a>
620

721
It's a library inspired by the [Angular's Reactive Forms](https://angular.io/guide/reactive-forms), which allows to create a tree of form control objects in the component class and bind them with native form control elements.
822

@@ -317,7 +331,10 @@ Its an another performance booster in RRF, it just holds the computation needed
317331

318332
Yes, this library works with react-native also, currently it supports react-native `TextInput` and `Switch` component.
319333

334+
### Note:
335+
If you're using react-native then please add the following line of code in `index.js` of your project to avoid error in android devices.
320336

337+
`import "babel-polyfill"`
321338

322339

323340
Let's make React Reactive Forms better! If you're interested in helping, all contributions are welcome and appreciated.

docs/GettingStarted.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ The above example will create an instance of [FormGroup](FormGroup.md) class whi
6363

6464

6565
### step2: Connect form with component
66-
This steps is not needed if you're using dynamic controls but if you want a better control over your form state then you should do that, if your controls are dynamic then you can also initalize the empty group control and add the controls later.
66+
This step is not needed if you're using dynamic controls but if you want a better control over your form state then you should do that, if your controls are dynamic then you can also initalize the empty group control and add the controls later.
6767
Example:
6868

6969
```js
@@ -154,7 +154,7 @@ export default class Login extends Component {
154154
### Add Listeners
155155

156156
You can add subscriptions for listening the state changes in form.
157-
There are a total of five observables available currently:
157+
There are a total of five observables available now:
158158

159159
#### valueChanges
160160
Emits an event every time when the control's value changes.
@@ -164,7 +164,7 @@ Emits an event every time when the control's state(value, touched, ...) changes.
164164
Emits an event every time when the control's status(PENDING, INVALID, VALID, DISABLED) changes.
165165
#### onValueChanges
166166
Emits an event every time when the control's value is changed by onChange event i.e by user.
167-
#### onBlurChnages
167+
#### onBlurChanges
168168
Emits an event every time when a blur event triggers on a control.
169169

170170
You can use these listeners to modify the form state dynamically based on the value of other controls.

docs/README.md

Lines changed: 128 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
# Getting Started With `react-reactive-form`
32
The basic implementation of reactive forms is super easy but it may be helpful to read a brief description of the core form classes.
43
* [Abstract Control](api/AbstractControl.md)
@@ -11,9 +10,9 @@ The basic implementation of reactive forms is super easy but it may be helpful t
1110
### step 1: Create FormGroup or FormArray
1211
A form group is a collection object of form controls & form array is the collection array of form controls.
1312

14-
There are two ways to create these:
13+
There are three ways to create these:
1514

16-
#### With FormBuilder
15+
#### With FormBuilder ( Static Controls )
1716
The FormBuilder class helps reduce repetition and clutter by handling details of control creation for you.
1817
`FormBuilder.group` is a static method that creates a FormGroup. `FormBuilder.group` takes an object whose keys and values are
1918
`FormControl` names and their definitions. In this example, the username control is defined by its initial data value,
@@ -31,7 +30,7 @@ const loginForm = FormBuilder.group({
3130
});
3231
```
3332

34-
#### Without FormBuilder
33+
#### Without FormBuilder ( Static Controls )
3534

3635
```js
3736
import { FormGroup, FormControl } from "react-reactive-form";
@@ -42,79 +41,103 @@ const loginForm = new FormGroup({
4241
})
4342
```
4443

44+
#### Without initializing the controls ( Dynamic Controls )
45+
46+
You can also create controls without even initializing the group control object with the help of new react form components ( [FieldGroup](api/FieldGroup.md), [FieldControl](api/FieldControl.md), [FieldArray](api/FieldArray.md)).
47+
48+
For eg.
49+
50+
```ts
51+
<FieldGroup
52+
render={({ value }) => (
53+
<form>
54+
<FieldControl
55+
name="test"
56+
render={({ handler }) => <input {...handler()}/>}
57+
/>
58+
<pre>{value.toString()}</pre>
59+
</form>)}
60+
/>
61+
```
62+
The above example will create an instance of [FormGroup](FormGroup.md) class which has a control named `test`.
63+
64+
4565
### step2: Connect form with component
46-
[Field](api/Field.md) component subscribes a particular control & only update it when it’s or it’s parent’s state changes, which improves the performance by restricting the unnecessary re-rendering of other fields.
66+
This steps is not needed if you're using dynamic controls but if you want a better control over your form state then you should do that, if your controls are dynamic then you can also initalize the empty group control and add the controls later.
67+
Example:
4768

4869
```js
4970
import React, { Component } from 'react';
50-
import { FormBuilder, Validators, Field } from "react-reactive-form";
71+
import {
72+
FormBuilder,
73+
FieldGroup,
74+
FieldControl,
75+
Validators,
76+
} from "react-reactive-form";
5177

5278
export default class Login extends Component {
53-
constructor(props) {
54-
super(props);
55-
// Create the controls
56-
this.loginForm = FormBuilder.group({
79+
loginForm = FormBuilder.group({
5780
username: ["", Validators.required],
5881
password: ["", Validators.required],
5982
rememberMe: false
6083
});
6184
}
62-
handleReset=(e) => {
85+
handleReset=() => {
6386
this.loginForm.reset();
64-
e.preventDefault();
6587
}
6688
handleSubmit=(e) => {
67-
console.log("Form values", this.loginForm.value);
6889
e.preventDefault();
90+
console.log("Form values", this.loginForm.value);
6991
}
7092
render() {
7193
return (
72-
<Field
94+
<FieldGroup
7395
control={this.loginForm}
7496
render={({ get, invalid }) => (
7597
<form onSubmit={this.handleSubmit}>
76-
<Field
77-
control={get("username")}
98+
<FieldControl
99+
name="username"
78100
render={({ handler, touched, hasError }) => (
79101
<div>
80102
<input {...handler()}/>
81103
<span>
82-
{touched
104+
{touched
83105
&& hasError("required")
84106
&& "Username is required"}
85107
</span>
86108
</div>
87109
)}
88110
/>
89-
<Field
90-
control={get("password")}
111+
<FieldControl
112+
name="password"
91113
render={({ handler, touched, hasError }) => (
92114
<div>
93115
<input {...handler()}/>
94116
<span>
95-
{touched
117+
{touched
96118
&& hasError("required")
97119
&& "Password is required"}
98120
</span>
99121
</div>
100122
)}
101123
/>
102-
<Field
103-
control={get("rememberMe")}
124+
<FieldControl
125+
name="rememberMe"
104126
render={({handler}) => (
105127
<div>
106128
<input {...handler("checkbox")}/>
107129
</div>
108130
)}
109131
/>
110-
<button
132+
<button
133+
type="button"
111134
onClick={this.handleReset}
112135
>
113136
Reset
114137
</button>
115138
<button
116139
type="submit"
117-
disabled={invalid}
140+
disabled={invalid}
118141
>
119142
Submit
120143
</button>
@@ -125,3 +148,84 @@ export default class Login extends Component {
125148
}
126149
}
127150
```
151+
152+
## Common Operations
153+
154+
### Add Listeners
155+
156+
You can add subscriptions for listening the state changes in form.
157+
There are a total of five observables available currently:
158+
159+
#### valueChanges
160+
Emits an event every time when the control's value changes.
161+
#### stateChanges
162+
Emits an event every time when the control's state(value, touched, ...) changes.
163+
#### statusChanges
164+
Emits an event every time when the control's status(PENDING, INVALID, VALID, DISABLED) changes.
165+
#### onValueChanges
166+
Emits an event every time when the control's value is changed by onChange event i.e by user.
167+
#### onBlurChnages
168+
Emits an event every time when a blur event triggers on a control.
169+
170+
You can use these listeners to modify the form state dynamically based on the value of other controls.
171+
172+
Example:
173+
174+
#### Disable/Enable a control based on another control's value
175+
176+
```js
177+
class Form extends Component {
178+
myForm = {
179+
public: true,
180+
images: [[]]
181+
}
182+
componentDidMount() {
183+
this.myForm.get('public').valueChanges.subscribe((value) => {
184+
const imagesControl = this.myForm.get('images')
185+
if(value) {
186+
imagesControl.disable()
187+
} else {
188+
imagesControl.enable()
189+
}
190+
})
191+
}
192+
componentWillUnmount() {
193+
this.myForm.get('public').valueChanges.unsubscribe()
194+
}
195+
}
196+
```
197+
198+
#### Set the value of a control based on another control's value
199+
200+
The following example ensures that one of the control's value must be `true`.
201+
202+
```js
203+
class Form extends Component {
204+
myForm = {
205+
showMeMen: true,
206+
showMeWomen: true
207+
}
208+
componentDidMount() {
209+
const showMeMenControl = this.myForm.get('showMeMen').unsubscribe()
210+
const showMeWomenControl = this.myForm.get('showMeWomen')
211+
212+
showMeMenControl.valueChanges.subscribe((value) => {
213+
if(!value && !showMeWomenControl.value) {
214+
showMeWomenControl.setValue(true)
215+
}
216+
})
217+
218+
showMeWomenControl.valueChanges.subscribe((value) => {
219+
if(!value && !showMeMenControl.value) {
220+
showMeMenControl.setValue(true)
221+
}
222+
})
223+
224+
}
225+
226+
componentWillUnmount() {
227+
this.myForm.get('showMeMen').valueChanges.unsubscribe()
228+
this.myForm.get('showMeWomen').valueChanges.unsubscribe()
229+
}
230+
}
231+
```

logo.png

1.77 KB
Loading

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-reactive-form",
3-
"version": "1.0.20",
3+
"version": "1.0.21",
44
"description": "Angular like Reactive Forms in React",
55
"keywords": [
66
"forms",
@@ -29,7 +29,9 @@
2929
"url": "https://github.com/bietkul/react-reactive-form/issues"
3030
},
3131
"babel": {
32-
"presets": ["es2015"]
32+
"presets": [
33+
"es2015"
34+
]
3335
},
3436
"devDependencies": {
3537
"babel-cli": "^6.26.0",
@@ -44,10 +46,13 @@
4446
"eslint-plugin-react": "^7.5.1",
4547
"prettier": "^1.8.2",
4648
"prettier-eslint-cli": "^4.4.2",
47-
"react": "^16.1.1"
49+
"react": "^16.1.1",
50+
"prop-types": "^15.6.0"
4851
},
4952
"peerDependencies": {
50-
"react": "^15.0.0-0 || ^16.0.0-0"
53+
"react": "^15.0.0-0 || ^16.0.0-0",
54+
"prop-types": "^15.6.0",
55+
"babel-polyfill": "^6.26.0"
5156
},
5257
"typings": "./index.d.ts",
5358
"homepage": "https://github.com/bietkul/react-reactive-form#readme"

0 commit comments

Comments
 (0)