From bade05b9cd2e3845ecc7814fe5d1516b05c82133 Mon Sep 17 00:00:00 2001 From: Stephen Demjanenko Date: Sat, 14 Mar 2015 09:45:33 -0700 Subject: [PATCH] Fix bug: Allow value === false to be passed This is necessary to make a checkbox input work. Without this change, value would be set to '' in this case. Add a test. --- specs/Formsy-spec.js | 45 ++++++++++++++++++++++++++++++++++++++++++++ src/Mixin.js | 4 ++-- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/specs/Formsy-spec.js b/specs/Formsy-spec.js index 1e3561d..d3b5c1d 100755 --- a/specs/Formsy-spec.js +++ b/specs/Formsy-spec.js @@ -403,4 +403,49 @@ describe('Formsy', function () { }); + describe("value === false", function() { + var onSubmit; + var TestInput = React.createClass({ + mixins: [Formsy.Mixin], + getDefaultProps: function() { + return { + type: "text", + }; + }, + changeValue: function() { + this.setValue(e.target[this.props.type === "checkbox" ? "checked" : "value"]); + }, + render: function () { + return + } + }); + + var TestForm = React.createClass({ + render: function () { + return ( + + + + + ); + } + }); + + beforeEach(function() { + onSubmit = jasmine.createSpy("onSubmit"); + }); + + it("should call onSubmit correctly", function() { + var form = TestUtils.renderIntoDocument(); + TestUtils.Simulate.submit(form.getDOMNode()); + expect(onSubmit).toHaveBeenCalledWith({foo: false}); + }); + + it("should allow dynamic changes to false", function() { + var form = TestUtils.renderIntoDocument(); + form.setProps({value: false}); + TestUtils.Simulate.submit(form.getDOMNode()); + expect(onSubmit).toHaveBeenCalledWith({foo: false}); + }); + }); }); diff --git a/src/Mixin.js b/src/Mixin.js index b436c72..48c8cd2 100644 --- a/src/Mixin.js +++ b/src/Mixin.js @@ -1,7 +1,7 @@ module.exports = { getInitialState: function () { return { - _value: this.props.value ? this.props.value : '', + _value: this.props.value !== undefined ? this.props.value : '', _isValid: true, _isPristine: true }; @@ -59,7 +59,7 @@ module.exports = { // the value, set the value again running a validation if (prevProps.validations !== this.props.validations || isValueChanged()) { - this.setValue(this.props.value || ''); + this.setValue(this.props.value === undefined ? '' : this.props.value); } },