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
{{ message }}
This repository was archived by the owner on Aug 23, 2022. It is now read-only.
The `<Field />` component is a helper component that wraps one or more controls and maps actions for the specified `model` to events on the controls. The supported controls are:
82
-
83
-
- `<input />` - any text type
84
-
- `<textarea />`
85
-
- `<input type="checkbox"/>`
86
-
- `<input type="radio"/>`
87
-
- `<select />`
88
-
- `<select multiple/>`
89
-
90
-
The only required prop is `model`, which is a string that represents the model value in the store. The allowed props (including optional props) are:
91
-
92
-
- `model` (String) - the string representing the store model value
93
-
- `updateOn` (String | Function) - a function that decorates the default `onChange` function, or a string:
94
-
- "change"
95
-
- "blur"
96
-
- "focus"
97
-
- `validators` (Object) - a validation object with key-validator pairs (see below)
98
-
- `asyncValidators` (Object) - an async validation object with key-asyncValidator pairs (see below)
99
-
- `parser` (Function) - a function that parses the value before updating the model.
100
-
101
-
## `<Field />` Component properties
102
-
103
-
### `model` property
104
-
105
-
The string representing the model value in the store.
106
-
107
-
```js
108
-
// in store.js
109
-
exportdefaultcreateStore(combineReducers({
110
-
'user':createModelReducer('user', { name:'' })
111
-
}));
112
-
113
-
// in component's render() method
114
-
<Field model="user.name">
115
-
<input type="text"/>
116
-
</Field>
117
-
```
118
-
119
-
### `updateOn` property
120
-
121
-
A string or function specifying when the component should dispatch a `change(...)` action. If a string, `updateOn` can be one of these values:
122
-
123
-
- `"change"` - will dispatch in `onChange`
124
-
- `"blur"` - will dispatch in `onBlur`
125
-
- `"focus"` - will dispatch in `onFocus`
126
-
127
-
So, `<Field model="foo.bar" updateOn="blur">` will only dispatch the `change(...)` action on blur.
128
-
129
-
If `updateOn` is a function, the function given will be called with the `change` action creator. The function given will be called in `onChange`. For example:
130
-
131
-
```js
132
-
importdebouncefrom'lodash/debounce';
133
-
134
-
<Field model="test.bounce"
135
-
updateOn={(change) =>debounce(change, 1000)}
136
-
<input type="text"/>
137
-
</Field>
138
-
```
139
-
140
-
### `validators` property
141
-
142
-
A map where the keys are validation keys, and the values are the corresponding functions that determine the validity of each key, given the model's value.
143
-
144
-
For example, this field validates that a username exists and is longer than 4 characters:
145
-
146
-
```js
147
-
<Field model="user.username"
148
-
validators={{
149
-
required: (val) =>val.length,
150
-
length: (val) =>val.length>4
151
-
}}>
152
-
<input type="text"/>
153
-
</Field>
154
-
```
155
-
156
-
## Action Creators
157
-
158
-
### `actions.change(model, value)`
159
-
Returns an action object that, when handled by a `modelReducer`, changes the value of the respective model to the new `value`.
160
-
161
-
When the change action is handled by a `formReducer`, the field model's `dirty` state is set to `true` and its corresponding `pristine` state is set to `false`.
162
-
163
-
**Arguments:**
164
-
- `model`: (String) the model whose value will be changed
165
-
- `value`: (*) the value the model will be changed to
Returns an action object that, when handled by a `formReducer`, changes the `focus` state of the field model in the form to `true`, as well as the corresponding `blur` state to `false`.
208
-
209
-
The "focus" state indicates that the field model is the currently focused field in the form.
210
-
211
-
**Arguments:**
212
-
- `model`: (String) the model indicated as focused
213
-
214
-
### `actions.blur(model)`
215
-
Returns an action object that, when handled by a `formReducer`, changes the `blur` state of the field model in the form to `true`, as well as the corresponding `focus` state to `false`. It also indicates that the field model has been `touched`, and will set that state to `true` and the `untouched` state to `false`.
216
-
217
-
The "blur" state indicates that the field model is not focused.
218
-
219
-
**Arguments:**
220
-
- `model`: (String) the model indicated as blurred
221
-
222
-
### `actions.setPristine(model)`
223
-
Returns an action object that, when handled by a `formReducer`, changes the `pristine` state of the field model in the form to `true`, as well as the corresponding `dirty` state to `false`.
224
-
225
-
The "pristine" state indicates that the user has not interacted with this field model yet.
226
-
227
-
**Arguments:**
228
-
- `model`: (String) the model indicated as pristine
229
-
230
-
### `actions.setDirty(model)`
231
-
Returns an action object that, when handled by a `formReducer`, changes the `dirty` state of the field model in the form to `true`, as well as the corresponding `pristine` state to `false`.
232
-
233
-
The "dirty" state indicates that the model value has been changed.
234
-
235
-
**Arguments:**
236
-
- `model`: (String) the model indicated as dirty
237
-
238
-
### `actions.setPending(model)`
239
-
Returns an action object that, when handled by a `formReducer`, changes the `pending` state of the field model in the form to `true`. It simultaneously sets the `submitted` state to `false`.
240
-
241
-
This action is useful when asynchronously validating or submitting a model, and is representative of the state between the initial and final state of a field model.
242
-
243
-
**Arguments:**
244
-
- `model`: (String) the model indicated as pending
245
-
246
-
### `actions.setValidity(model, validity)`
247
-
Returns an action object that, when handled by a `formReducer`, changes the `valid` state of the field model in the form to `true` or `false`, based on the `validity` (see below). It simultaneously sets the `errors` on the field model to the inverse of the `validity`.
248
-
249
-
The `validity` can be an object or a boolean value, indicating which aspects of the field model are valid. A validity object is a key/value pair where the values are functions that return a truthy (if valid) or falsey (if invalid) value.
250
-
251
-
**Example:**
252
-
```js
253
-
let val ='testing123';
254
-
255
-
dispatch(actions.setValidity('user.password', {
256
-
required: (val) => val &&val.length,
257
-
correct: (val) => val ==='hunter2'
258
-
}));
259
-
260
-
// Field model errors will now look like:
261
-
// errors: {
262
-
// required: false,
263
-
// correct: true
264
-
// }
265
-
```
266
-
267
-
**Arguments:**
268
-
- `model`: (String) the model indicated as pending
269
-
- `validity`: (Boolean | Object) the validity of the model
Returns a model reducer that only responds to `change()` and `reset()` actions on the model or model's child values.
277
-
278
-
**Note:** if using the `<Field />` component or any action thunk creators, the `model` _must be the same as_ the property given to the reducer in `combineReducers({...})`.
279
-
280
-
**Arguments:**
281
-
- `model`: (String) the model that the reducer will update
282
-
- `initialState`: (Any) the initial state of the model
Returns a form reducer that only responds to any actions on the model or model's child values. The reducer's state has the shape of `initialFormState`, with a `fields` property where each field has the shape of `initialFieldState`.
300
-
301
-
**Arguments:**
302
-
- `model`: (String) the model whose form state (and child field states) the reducer will update.
These action creators require [redux-thunk-middleware](https://github.com/gaearon/redux-thunk) to work, as they use thunks to determine the next state.
316
-
317
-
### `actions.merge(model, values)`
318
-
Dispatches a `change` action that merges the `values` into the value specified by the `model`.
319
-
320
-
**Arguments:**
321
-
- `model`: (String) the object model to be updated.
322
-
- `values`: (Object | Object[] | Objects...) the values that will be merged into the object represented by the `model`.
323
-
324
-
### `actions.xor(model, item)`
325
-
Dispatches a `change` action that applies an "xor" operation to the array represented by the `model`; that is, it "toggles" an item in an array.
326
-
327
-
If the model value contains `item`, it will be removed. If the model value doesn't contain `item`, it will be added.
328
-
329
-
**Arguments:**
330
-
- `model`: (String) the array model where the `xor` will be applied.
331
-
- `item`: (*) the item to be "toggled" in the model value.
332
-
333
-
### `actions.push(model, item)`
334
-
Dispatches a `change` action that "pushes" the `item` to the array represented by the `model`.
335
-
336
-
**Arguments:**
337
-
- `model`: (String) the array model where the `item` will be pushed.
338
-
- `item`: (*) the item to be "pushed" in the model value.
339
-
340
-
### `actions.toggle(model)`
341
-
Dispatches a `change` action that sets the `model` to true if it is falsey, and false if it is truthy.
342
-
343
-
**Arguments:**
344
-
- `model`: (String) the model whose value will be toggled.
345
-
346
-
### `actions.filter(model, iteratee)`
347
-
Dispatches a `change` action that filters the array represented by the `model` through the `iteratee` function. This action works similar to [lodash's `_.filter` method](https://lodash.com/docs#filter).
348
-
349
-
If no `iteratee` is specified, the identity function is used by default.
350
-
351
-
**Arguments:**
352
-
- `model`: (String) the array model to be filtered.
353
-
- `iteratee = identity`: (Function) the filter iteratee function that filters the array represented by the model.
354
-
355
-
### `actions.map(model, iteratee)`
356
-
Dispatches a `change` action that maps the array represented by the `model` through the `iteratee` function. This action works similar to [lodash's `_.map` method](https://lodash.com/docs#map).
357
-
358
-
If no `iteratee` is specified, the identity function is used by default.
359
-
360
-
**Arguments:**
361
-
- `model`: (String) the array model to be maped.
362
-
- `iteratee = identity`: (Function) the map iteratee function that maps the array represented by the model.
363
-
364
-
### `actions.remove(model, index)`
365
-
Dispatches a `change` action that removes the item at the specified `index` of the array represented by the `model`.
366
-
367
-
**Arguments:**
368
-
- `model`: (String) the array model to be updated.
369
-
- `index`: (Number) the index that should be removed from the array.
370
-
62
+
## Road map
63
+
- [x] - ~~Deep states for form and model reducers~~
64
+
- [ ] - React Native support
65
+
- [ ] - Support for `<input type="file"/>`
66
+
- [ ] - Automatic model resetting for `<input type="reset"/>`
0 commit comments