Skip to content

Commit

Permalink
Merge pull request #120 from appfolio/boundFormUpdates
Browse files Browse the repository at this point in the history
Bound form updates
  • Loading branch information
gthomas-appfolio authored Jan 31, 2017
2 parents 616491b + 7c2f28c commit f00dd7b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 17 deletions.
8 changes: 4 additions & 4 deletions src/components/Address.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ class Address extends Component {
<FormGroup>
<Select
className="w-100"
name="country"
name="countryCode"
options={COUNTRIES}
placeholder="Country"
{...this.propsFor('country')}
onChange={selection => this.onChange({ country: selection && selection.value })}
{...this.propsFor('countryCode')}
onChange={selection => this.onChange({ countryCode: selection && selection.value })}
/>
</FormGroup>
</div>
Expand All @@ -101,7 +101,7 @@ const fieldTypes = {
city: React.PropTypes.string,
state: React.PropTypes.string,
postal: React.PropTypes.string,
country: React.PropTypes.string
countryCode: React.PropTypes.string
};

Address.propTypes = {
Expand Down
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
4 changes: 2 additions & 2 deletions stories/Address.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ storiesOf('Address', module)
city: 'Smallsville',
state: 'AL',
postal: '12345-1234',
country: 'US'
countryCode: 'US'
}}
onChange={action('address onChange')}
/>
Expand All @@ -30,7 +30,7 @@ storiesOf('Address', module)
city: text('city', 'Smallsville'),
state: select('state', states.map(s => s.value), 'AL'),
postal: text('postal', '12345-1234'),
country: 'US'
countryCode: 'US'
}}
onChange={action('address onChange')}
/>
Expand Down
16 changes: 8 additions & 8 deletions test/components/Address.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('<Address />', () => {
city: 'Gotham',
state: 'NJ',
postal: '07001',
country: 'US'
countryCode: 'US'
}}
onChange={callback}
/>
Expand Down Expand Up @@ -82,17 +82,17 @@ describe('<Address />', () => {
});

it('should have country', () => {
const input = component.find('[name="country"]');
const input = component.find('[name="countryCode"]');
assert.equal(input.type(), Select);
assert.equal(input.prop('placeholder'), 'Country');
assert.equal(input.prop('defaultValue'), 'US');
assert.equal(input.prop('value'), undefined);

input.simulate('change', { label: 'USA', value: 'US' });
assert(callback.calledWith({ country: 'US' }));
assert(callback.calledWith({ countryCode: 'US' }));

input.simulate('change', null);
assert(callback.calledWith({ country: null }));
assert(callback.calledWith({ countryCode: null }));
});
});

Expand All @@ -104,7 +104,7 @@ describe('<Address />', () => {
city: 'Gotham',
state: 'NJ',
postal: '07001',
country: 'US'
countryCode: 'US'
}
const component = shallow(
<Address
Expand Down Expand Up @@ -162,15 +162,15 @@ describe('<Address />', () => {
});

it('should update country', () => {
const input = component.find('[name="country"]');
const input = component.find('[name="countryCode"]');
assert.equal(input.prop('value'), 'US');
assert.equal(input.prop('defaultValue'), '');

input.simulate('change', { label: 'USA', value: 'US' });
assert(callback.calledWith(Object.assign({}, addressData, { country: 'US' })));
assert(callback.calledWith(Object.assign({}, addressData, { countryCode: 'US' })));

input.simulate('change', null);
assert(callback.calledWith(Object.assign({}, addressData, { country: null })));
assert(callback.calledWith(Object.assign({}, addressData, { countryCode: null })));
});
});
});
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 f00dd7b

Please sign in to comment.