You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
7
+
6
8
# Features
7
-
- UI independent.
8
-
- Zero dependencies.
9
-
- Nested forms.
10
-
- Subscribers for value & status changes of controls.
11
-
- Provides a set of validators & also supports custom sync & async validators.
12
-
- Better form management with `FormGroup` & `FormArray` apis.
13
-
- Customizable update strategy for better performace with large forms.
14
-
# Installation
9
+
10
+
* UI independent.
11
+
* Zero dependencies.
12
+
* Nested forms.
13
+
* Subscribers for value & status changes of controls.
14
+
* Provides a set of validators & also supports custom sync & async validators.
15
+
* Better form management with `FormGroup` & `FormArray` apis.
16
+
* Customizable update strategy for better performace with large forms.
17
+
# Installation
18
+
15
19
```sh
16
20
npm install react-reactive-form --save
17
21
```
22
+
18
23
# Basic Example
19
24
20
25
```js
21
26
importReact, { Component } from'react';
22
-
import { FormBuilder, Validators, Field } from"react-reactive-form";
You can also create dynamic controls without even initializing the group control object with the help of new react form components ( [FieldGroup](docs/api/FieldGroup.md), [FieldControl](docs/api/FieldControl.md), [FieldArray](docs/api/FieldArray.md)).
{touched &&hasError('required') &&'Username is required'}
134
+
</span>
135
+
</div>
136
+
)}
137
+
/>
138
+
<FieldControl
139
+
name="password"
140
+
options={{ validators:Validators.required }}
141
+
render={({ handler, touched, hasError }) => (
142
+
<div>
143
+
<input {...handler()} />
144
+
<span>
145
+
{touched &&hasError('required') &&'Password is required'}
146
+
</span>
147
+
</div>
148
+
)}
149
+
/>
150
+
<FieldControl
151
+
name="rememberMe"
152
+
render={({ handler }) => (
153
+
<div>
154
+
<input {...handler('checkbox')} />
155
+
</div>
156
+
)}
157
+
/>
158
+
<button type="button" onClick={() =>reset()}>
159
+
Reset
160
+
</button>
161
+
<button type="submit" disabled={invalid}>
162
+
Submit
163
+
</button>
164
+
</form>
165
+
)}
166
+
/>
167
+
)
168
+
}
169
+
}
170
+
```
171
+
172
+
<b>So, it's not mandatory that you need to define your control separately but if you want better control then you should do that, if your controls are dynamic then you can also initalize the empty group control and add the controls later.
173
+
See the example:</b>
174
+
175
+
```js
176
+
importReact, { Component } from'react'
177
+
import {
178
+
FormBuilder,
179
+
FieldGroup,
180
+
FieldControl,
181
+
Validators
182
+
} from'react-reactive-form'
183
+
184
+
exportdefaultclassLoginextendsComponent {
185
+
// Initialize the empty group control
186
+
loginForm =FormBuilder.group({})
187
+
188
+
handleReset=e=> {
189
+
this.loginForm.reset()
190
+
}
191
+
handleSubmit=e=> {
192
+
console.log('Form values', this.loginForm.value)
193
+
e.preventDefault()
194
+
}
195
+
render() {
196
+
return (
197
+
<FieldGroup
198
+
control={this.loginForm}
199
+
render={({ get, invalid, reset, value }) => (
200
+
<form onSubmit={this.handleSubmit}>
201
+
<FieldControl
202
+
name="username"
203
+
options={{ validators:Validators.required }}
204
+
render={({ handler, touched, hasError }) => (
205
+
<div>
206
+
<input {...handler()} />
207
+
<span>
208
+
{touched &&hasError('required') &&'Username is required'}
209
+
</span>
210
+
</div>
211
+
)}
212
+
/>
213
+
<FieldControl
214
+
name="password"
215
+
options={{ validators:Validators.required }}
216
+
render={({ handler, touched, hasError }) => (
217
+
<div>
218
+
<input {...handler()} />
219
+
<span>
220
+
{touched &&hasError('required') &&'Password is required'}
221
+
</span>
222
+
</div>
223
+
)}
224
+
/>
225
+
<FieldControl
226
+
name="rememberMe"
227
+
render={({ handler }) => (
228
+
<div>
229
+
<input {...handler('checkbox')} />
230
+
</div>
231
+
)}
232
+
/>
233
+
<button type="button" onClick={this.handleReset}>
234
+
Reset
235
+
</button>
236
+
<button type="submit" disabled={invalid}>
237
+
Submit
238
+
</button>
239
+
</form>
240
+
)}
241
+
/>
242
+
)
243
+
}
244
+
}
245
+
```
246
+
100
247
# Documentation
101
-
*[Getting Started](docs/GettingStarted.md)
248
+
249
+
*[Getting Started](docs)
102
250
*[API](docs/api/)
103
-
# Code Sandboxes
104
-
Try out `react-reactive-forms` in these sandbox versions of the Examples.
251
+
# Code Sandboxes
252
+
Try out `react-reactive-forms` in these sandbox versions of the Examples.
0 commit comments