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

Commit e0dca37

Browse files
committed
Parsing initial value if parsed value differs from initial value. Fixes #379
1 parent 4b8cc47 commit e0dca37

File tree

2 files changed

+85
-44
lines changed

2 files changed

+85
-44
lines changed

src/components/control-component.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,25 @@ class Control extends Component {
4545
}
4646

4747
componentWillMount() {
48-
const { modelValue } = this.props;
48+
const {
49+
model,
50+
modelValue,
51+
parser,
52+
dispatch,
53+
} = this.props;
4954
const { onLoad } = this.state.mappedProps;
5055

5156
if (onLoad) {
5257
onLoad(modelValue);
5358
}
59+
60+
if (parser) {
61+
const parsedValue = parser(modelValue);
62+
63+
if (parsedValue !== modelValue) {
64+
dispatch(actions.change(model, parsedValue));
65+
}
66+
}
5467
}
5568

5669
componentWillReceiveProps(nextProps) {

test/field-parser-spec.js

Lines changed: 71 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,55 +7,83 @@ import { Provider } from 'react-redux';
77
import { modelReducer, formReducer, Field } from '../src';
88

99
describe('<Field parser={...} />', () => {
10-
const store = createStore(combineReducers({
11-
test: modelReducer('test', { foo: '' }),
12-
testForm: formReducer('test', { foo: '' }),
13-
}));
10+
context('standard usage', () => {
11+
const store = createStore(combineReducers({
12+
test: modelReducer('test', { foo: '' }),
13+
testForm: formReducer('test', { foo: '' }),
14+
}));
1415

15-
const parseValue = val => ({
16-
data: val,
17-
});
16+
const parseValue = val => ({
17+
data: val,
18+
});
19+
20+
const field = TestUtils.renderIntoDocument(
21+
<Provider store={store}>
22+
<Field
23+
model="test.foo"
24+
parser={parseValue}
25+
validators={{
26+
isParsed: (val) => val
27+
&& val.data
28+
&& val.data === 'parse test',
29+
}}
30+
>
31+
<input type="text" />
32+
</Field>
33+
</Provider>
34+
);
35+
36+
const input = TestUtils.findRenderedDOMComponentWithTag(field, 'input');
37+
38+
it('should parse the changed values given a parser function', () => {
39+
const expected = { data: 'foo' };
40+
41+
input.value = 'foo';
42+
43+
TestUtils.Simulate.change(input);
1844

19-
const field = TestUtils.renderIntoDocument(
20-
<Provider store={store}>
21-
<Field
22-
model="test.foo"
23-
parser={parseValue}
24-
validators={{
25-
isParsed: (val) => val
26-
&& val.data
27-
&& val.data === 'parse test',
28-
}}
29-
>
30-
<input type="text" />
31-
</Field>
32-
</Provider>
33-
);
34-
35-
const input = TestUtils.findRenderedDOMComponentWithTag(field, 'input');
36-
37-
it('should parse the changed values given a parser function', () => {
38-
const expected = { data: 'foo' };
39-
40-
input.value = 'foo';
41-
42-
TestUtils.Simulate.change(input);
43-
44-
assert.deepEqual(
45-
store.getState().test.foo,
46-
expected);
45+
assert.deepEqual(
46+
store.getState().test.foo,
47+
expected);
48+
});
49+
50+
it('should parse before validation', () => {
51+
input.value = 'parse test';
52+
53+
assert.isFalse(
54+
store.getState().testForm.fields.foo.validity.isParsed,
55+
'should not be valid yet');
56+
57+
TestUtils.Simulate.change(input);
58+
59+
assert.isTrue(
60+
store.getState().testForm.fields.foo.validity.isParsed);
61+
});
4762
});
4863

49-
it('should parse before validation', () => {
50-
input.value = 'parse test';
64+
it('should parse the initial value immediately', () => {
65+
const store = createStore(combineReducers({
66+
test: modelReducer('test', { foo: 'initial' }),
67+
testForm: formReducer('test', { foo: 'initial' }),
68+
}));
69+
70+
const parseValue = val => val.toUpperCase();
71+
72+
const field = TestUtils.renderIntoDocument(
73+
<Provider store={store}>
74+
<Field
75+
model="test.foo"
76+
parser={parseValue}
77+
>
78+
<input type="text" />
79+
</Field>
80+
</Provider>
81+
);
5182

52-
assert.isFalse(
53-
store.getState().testForm.fields.foo.validity.isParsed,
54-
'should not be valid yet');
83+
const input = TestUtils.findRenderedDOMComponentWithTag(field, 'input');
5584

56-
TestUtils.Simulate.change(input);
85+
assert.equal(input.value, 'INITIAL');
5786

58-
assert.isTrue(
59-
store.getState().testForm.fields.foo.validity.isParsed);
87+
assert.equal(store.getState().test.foo, 'INITIAL');
6088
});
6189
});

0 commit comments

Comments
 (0)