Skip to content

Commit

Permalink
Coerce checked property of checkboxes within Forms to be booleans [#1…
Browse files Browse the repository at this point in the history
…54579887]

Signed-off-by: Reid Mitchell <rmitchell@pivotal.io>
  • Loading branch information
Jonathan Berney authored and reidmit committed Jan 26, 2018
1 parent d4b78d1 commit f51a206
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
20 changes: 18 additions & 2 deletions spec/pivotal-ui-react/form/form-col_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('FormCol', () => {

beforeEach(() => {
checkbox = <input type="checkbox"/>;
state = {current: {checkboxName: true}};
state = {current: {}};
innerOnChange = () => 'changed';
onChangeCheckbox = jasmine.createSpy('onChangeCheckbox').and.returnValue(innerOnChange);

Expand All @@ -74,11 +74,27 @@ describe('FormCol', () => {
expect(React.cloneElement).toHaveBeenCalledWith(checkbox, {
name: 'checkboxName',
id: 'some-unique-string',
checked: true,
checked: false,
onChange: innerOnChange
});
});

describe('when the value is true in the state', () => {
beforeEach(() => {
state = {current: {checkboxName: true}};
subject::setProps({state});
});

it('passes in the correct props to the cloned element', () => {
expect(React.cloneElement).toHaveBeenCalledWith(checkbox, {
name: 'checkboxName',
id: 'some-unique-string',
checked: true,
onChange: innerOnChange
});
});
});

describe('when a checked state is passed', () => {
beforeEach(() => {
checkbox = <input type="checkbox" checked={false}/>;
Expand Down
2 changes: 1 addition & 1 deletion src/react/forms/form-col.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class FormCol extends React.Component {
if (name) {
const isCheckbox = element.props.type === 'checkbox';
if (isCheckbox) {
props.checked = element.props.hasOwnProperty('checked') ? element.props.checked : (state.current && state.current[name]);
props.checked = !!(element.props.hasOwnProperty('checked') ? element.props.checked : (state.current && state.current[name]));
props.onChange = element.props.onChange || onChangeCheckbox(name);
} else {
props.value = element.props.hasOwnProperty('value') ? element.props.value : (state.current && state.current[name]);
Expand Down

0 comments on commit f51a206

Please sign in to comment.