Skip to content
This repository was archived by the owner on Aug 23, 2022. It is now read-only.

Commit 7911e15

Browse files
committed
Merge branch 'master' of github.com:davidkpiano/react-redux-form
2 parents 17243ed + 5db0fce commit 7911e15

File tree

5 files changed

+133
-9
lines changed

5 files changed

+133
-9
lines changed

docs/api/Control.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ For example, this control validates that a username exists and is longer than 4
216216
/>
217217
```
218218

219-
### `parser={() => ...}` {#prop-parser}
219+
## `parser={() => ...}` {#prop-parser}
220220
_(Function)_: A function that _parses_ the view value of the control before it is changed. It takes in two arguments:
221221
- `value` - the view value that represents the _next_ model value
222222
- `previous` (optional) - the current model value _before_ it is changed

examples/immutable-track/app.js

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import React, { Component } from 'react';
2+
import ReactDOM from 'react-dom';
3+
4+
import { Provider } from 'react-redux';
5+
import { createStore, applyMiddleware, compose } from 'redux';
6+
7+
import { fromJS } from 'immutable';
8+
9+
import {
10+
Field,
11+
Control,
12+
Form,
13+
Fieldset,
14+
track,
15+
combineForms,
16+
} from 'react-redux-form/immutable';
17+
18+
const initialUserState = fromJS({
19+
firstName: '',
20+
lastName: '',
21+
groups: [
22+
{
23+
id: 5,
24+
name: 'Group A',
25+
},
26+
{
27+
id: 6,
28+
name: 'Group B',
29+
},
30+
],
31+
});
32+
33+
const store = createStore(
34+
combineForms({
35+
user: initialUserState,
36+
}),
37+
);
38+
39+
class UserForm extends Component {
40+
constructor(props) {
41+
super(props);
42+
this.state = {
43+
groupIndex: 0,
44+
groupId: 5,
45+
};
46+
}
47+
48+
render() {
49+
return (
50+
<Form model="user" onSubmit={v => console.log(v.toJS())}>
51+
<div className="field">
52+
<label>First name:</label>
53+
<Control.text model=".firstName" />
54+
</div>
55+
56+
<div className="field">
57+
<label>Last name:</label>
58+
<Control.text model=".lastName" />
59+
</div>
60+
61+
<div className="field">
62+
<label>Group index</label>
63+
<input
64+
type="number"
65+
value={this.state.groupIndex}
66+
onChange={event =>
67+
this.setState({ groupIndex: event.target.value })}
68+
/>
69+
</div>
70+
71+
<div className="field">
72+
<label>Select Group by ID: </label>
73+
<input
74+
type="number"
75+
value={this.state.groupId}
76+
onChange={event => this.setState({ groupId: parseInt(event.target.value, 10) })}
77+
/>
78+
</div>
79+
80+
<Fieldset model={track(`.groups[]`, { id: this.state.groupId })}>
81+
<div className="field">
82+
<label>Group: </label>
83+
<div>
84+
ID: <Control.text model=".id" />
85+
<br />
86+
Name: <Control.text model=".name" />
87+
</div>
88+
</div>
89+
</Fieldset>
90+
91+
<button type="submit">
92+
Submit (check console)
93+
</button>
94+
</Form>
95+
);
96+
}
97+
}
98+
99+
class App extends React.Component {
100+
render() {
101+
return (
102+
<Provider store={store}>
103+
<UserForm />
104+
</Provider>
105+
);
106+
}
107+
}
108+
109+
ReactDOM.render(<App />, document.getElementById('app'));

examples/immutable-track/index.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!doctype html>
2+
<link href="/global.css" rel="stylesheet"/>
3+
<title>Immutable track() - react redux form examples</title>
4+
<body>
5+
<h1 class="breadcrumbs"><a href="/">react redux form examples</a> / Immutable track()</h1>
6+
<div id="app" />
7+
<script src="/__build__/shared.js"></script>
8+
<script src="/__build__/immutable-track.js"></script>
9+
</body>

examples/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ <h1>react redux form examples</h1>
77
<li><a href="quick-start/">Quick Start</a></li>
88
<li><a href="validation/">Validation</a></li>
99
<li><a href="immutable/">Immutable</a></li>
10+
<li><a href="immutable-track/">Immutable track()</a></li>
1011
<li><a href="stress-test/">stress-test</a></li>
1112
<li><a href="scheduling/">Scheduling</a></li>
1213
</ul>

src/utils/find-key.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
1+
import { List } from 'immutable';
12

23
export default function findKey(object, predicate) {
34
let resultKey;
45

5-
Object.keys(object).some((key) => {
6-
const isKey = predicate(object[key], key, object);
6+
if (List.isList(object)) {
7+
resultKey = object.findKey(predicate);
8+
} else {
9+
Object.keys(object).some((key) => {
10+
const isKey = predicate(object[key], key, object);
711

8-
if (isKey) {
9-
resultKey = key;
10-
return true;
11-
}
12+
if (isKey) {
13+
resultKey = key;
14+
return true;
15+
}
1216

13-
return false;
14-
});
17+
return false;
18+
});
19+
}
1520

1621
return resultKey;
1722
}

0 commit comments

Comments
 (0)