Skip to content

Commit 37f0329

Browse files
committed
Fix state being mutated when using onValues
1 parent c5cfe92 commit 37f0329

File tree

4 files changed

+48
-5
lines changed

4 files changed

+48
-5
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-validify",
3-
"version": "2.0.4",
3+
"version": "2.0.5",
44
"description": "Form validation made easy",
55
"main": "dist/form.js",
66
"directories": {

src/form.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ export default class Form extends React.Component {
5151
}
5252

5353
onChange({ target }) {
54-
let { values } = this.state;
5554
let { onValues } = this.props;
55+
let values = { ...this.props.values, ...this.state.values };
56+
5657
values[target.name] = target.value;
5758

5859
if (onValues) return onValues({ ...values });

tests/on-values.js

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
//Test things dealing with onValues prop
22
import React from 'react';
33
import Form from '../src/form';
4-
import { shallow } from 'enzyme';
4+
import { shallow, mount } from 'enzyme';
55

66
const Input = ({ error, ...props }) =>
7-
error ? <p className="error">{error}</p> : <input {...props} />;
7+
error
8+
? <p className="error">
9+
{error}
10+
</p>
11+
: <input {...props} />;
812

913
test('Form passes values to onValues', () => {
1014
const onValues = jest.fn();
@@ -40,3 +44,37 @@ test('Form sets values on prop change', () => {
4044
wrapper.setProps({ values: { test: 'changed!' } });
4145
expect(wrapper.find(Input).props().value).toEqual('changed!');
4246
});
47+
48+
test('onValues does not mutate state', () => {
49+
let onClick = jest.fn();
50+
51+
class Test extends React.Component {
52+
constructor() {
53+
super();
54+
this.state = { values: { test: 'Set' } };
55+
}
56+
57+
render() {
58+
return (
59+
<Form
60+
values={this.state.values}
61+
onValues={values => this.setState({ values })}
62+
>
63+
<Input name="test" />
64+
<div
65+
submit
66+
className="submit"
67+
onClick={() => this.setState({ values: { test: '' } })}
68+
/>
69+
</Form>
70+
);
71+
}
72+
}
73+
const wrapper = mount(<Test />);
74+
wrapper
75+
.find(Input)
76+
.simulate('change', { target: { name: 'test', value: 'Setting' } });
77+
78+
wrapper.find('.submit').simulate('click');
79+
expect(wrapper.find(Input).props().value).toEqual('');
80+
});

tests/submit.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import Form from '../src/form';
44
import { shallow } from 'enzyme';
55

66
const Input = ({ error, ...props }) =>
7-
error ? <p className="error">{error}</p> : <input {...props} />;
7+
error
8+
? <p className="error">
9+
{error}
10+
</p>
11+
: <input {...props} />;
812

913
test('Submit triggers validation', () => {
1014
const wrapper = shallow(

0 commit comments

Comments
 (0)