From e9310002d16ff04e9bf8ba21e4353443a819824d Mon Sep 17 00:00:00 2001 From: Stephen Demjanenko Date: Sat, 21 Feb 2015 11:50:49 -0800 Subject: [PATCH] registerInputs: Support null/undefined children; add test A lot of react examples have rendering functions which early return null (such as a closed dropdown). This method was erroring in that case. --- specs/Formsy-spec.js | 36 ++++++++++++++++++++++++++++++++++++ src/main.js | 4 ++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/specs/Formsy-spec.js b/specs/Formsy-spec.js index c129b59..ed0d106 100755 --- a/specs/Formsy-spec.js +++ b/specs/Formsy-spec.js @@ -14,6 +14,42 @@ describe('Formsy', function () { expect(form.getDOMNode().className).toEqual('foo'); }); + it('should allow for null/undefined children', function (done) { + var TestInput = React.createClass({ + mixins: [Formsy.Mixin], + changeValue: function (event) { + this.setValue(event.target.value); + }, + render: function () { + return + } + }); + + var model = null; + var TestForm = React.createClass({ + onSubmit: function (formModel) { + model = formModel; + }, + render: function () { + return ( + +

Test

+ { null } + { undefined } + +
+ ); + } + }); + + var form = TestUtils.renderIntoDocument(); + setTimeout(function () { + TestUtils.Simulate.submit(form.getDOMNode()); + expect(model).toEqual({name: 'foo'}); + done(); + }, 10); + }); + it('should allow for inputs being added dynamically', function (done) { var inputs = []; diff --git a/src/main.js b/src/main.js index 5052552..78e6cd9 100644 --- a/src/main.js +++ b/src/main.js @@ -351,13 +351,13 @@ Formsy.Form = React.createClass({ registerInputs: function (children) { React.Children.forEach(children, function (child) { - if (child.props && child.props.name) { + if (child && child.props && child.props.name) { child.props._attachToForm = this.attachToForm; child.props._detachFromForm = this.detachFromForm; child.props._validate = this.validate; } - if (child.props && child.props.children) { + if (child && child.props && child.props.children) { this.registerInputs(child.props.children); }