Skip to content

Commit

Permalink
ap - call onChange when bound form data changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Panchal committed Jan 31, 2017
1 parent 9d9efe5 commit 7c2f28c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/components/BoundForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ class BoundForm extends React.Component {
children: React.PropTypes.node,
errors: React.PropTypes.object,
object: React.PropTypes.object.isRequired,
onSubmit: React.PropTypes.func
onSubmit: React.PropTypes.func,
onChange: React.PropTypes.func
};

static defaultProps = {
errors: {},
onSubmit: noop
onSubmit: noop,
onChange: noop
};

constructor(props) {
Expand All @@ -32,6 +34,7 @@ class BoundForm extends React.Component {
handleChange = name => data => {
const value = data.target instanceof Element ? data.target.value : data;
this.setState({ formData: set(this.state.formData, name, value) });
this.props.onChange(this.state.formData);
}

render() {
Expand Down
9 changes: 8 additions & 1 deletion test/components/BoundForm.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe('<BoundForm />', () => {
}

const submitFunc = sinon.stub();
const changeFunc = sinon.stub();

const Composite = props => (
<div>
Expand All @@ -33,7 +34,7 @@ describe('<BoundForm />', () => {
);

const component = shallow(
<BoundForm object={data} errors={errors} onSubmit={submitFunc}>
<BoundForm object={data} errors={errors} onSubmit={submitFunc} onChange={changeFunc}>
<FormRow label="First Name" name="firstName" />
<FormRow label="Last Name" name="lastName" />
<FormRow type="checkbox" label="Foobar" name="checkboxes">
Expand Down Expand Up @@ -61,6 +62,12 @@ describe('<BoundForm />', () => {
assert.equal(component.state('formData').firstName, 'Desmond');
});

it('should emit onChange when changed', () => {
const row = component.find('[name="firstName"]');
row.simulate('change', 'Desmond');
assert(changeFunc.calledWith(Object.assign({}, data, { firstName: 'Desmond' })));
});

it('should support nested data', () => {
const row = component.find('[name="address"]');
assert.equal(row.prop('value'), component.state('formData').address);
Expand Down

0 comments on commit 7c2f28c

Please sign in to comment.