From 34f3bdcc4ef248d86655c07e689da83b255db95a Mon Sep 17 00:00:00 2001 From: Brett Sun Date: Fri, 12 Feb 2016 11:15:56 +0100 Subject: [PATCH] Small cleanup for renderChildren() --- js/components/ascribe_forms/form.js | 14 +++++++++----- js/components/ascribe_forms/property.js | 22 ++++++++++++---------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/js/components/ascribe_forms/form.js b/js/components/ascribe_forms/form.js index c95b206e..f7c0c2de 100644 --- a/js/components/ascribe_forms/form.js +++ b/js/components/ascribe_forms/form.js @@ -250,12 +250,15 @@ let Form = React.createClass({ }, renderChildren() { - return ReactAddons.Children.map(this.props.children, (child, i) => { + const { children, disabled } = this.props; + + return ReactAddons.Children.map(children, (child, i) => { if (child) { return React.cloneElement(child, { - handleChange: this.handleChangeChild, ref: (ref) => { - this._refs[child.props.name] = ref; + if (child.props.name) { + this._refs[child.props.name] = ref; + } // Since refs will be overwritten by this functions return statement, // we still want to be able to define refs for nested `Form` or `Property` @@ -265,10 +268,11 @@ let Form = React.createClass({ child.ref(ref); } }, - key: i, // We need this in order to make editable be overridable when setting it directly // on Property - editable: child.props.overrideForm ? child.props.editable : !this.props.disabled + editable: child.props.overrideForm ? child.props.editable : !disabled, + handleChange: this.handleChangeChild, + key: i }); } }); diff --git a/js/components/ascribe_forms/property.js b/js/components/ascribe_forms/property.js index 7450459a..b90b994f 100644 --- a/js/components/ascribe_forms/property.js +++ b/js/components/ascribe_forms/property.js @@ -263,17 +263,13 @@ const Property = React.createClass({ }, renderChildren(style) { + const { checkboxLabel, children, editable, name } = this.props; + // Input's props should only be cloned and propagated down the tree, // if the component is actually being shown (!== 'expanded === false') - if((this.state.expanded && this.props.checkboxLabel) || !this.props.checkboxLabel) { - return ReactAddons.Children.map(this.props.children, (child) => { + if ((this.state.expanded && checkboxLabel) || !checkboxLabel) { + return ReactAddons.Children.map(children, (child) => { const childWithProps = React.cloneElement(child, { - style, - onChange: this.handleChange, - onFocus: this.handleFocus, - onBlur: this.handleBlur, - setWarning: this.setWarning, - disabled: !this.props.editable, ref: (ref) => { this._refs.input = ref; @@ -285,8 +281,14 @@ const Property = React.createClass({ child.ref(ref); } }, - name: this.props.name, - setExpanded: this.setExpanded + name, + style, + disabled: !editable, + onBlur: this.handleBlur, + onChange: this.handleChange, + onFocus: this.handleFocus, + setExpanded: this.setExpanded, + setWarning: this.setWarning }); return childWithProps;