From ccc1699cc466b04c0d01920416d8a9d5443cb6e1 Mon Sep 17 00:00:00 2001 From: Javi Velasco Date: Sat, 3 Sep 2016 16:08:52 +0200 Subject: [PATCH] Fix #630 --- components/radio/RadioGroup.js | 15 +++++++++------ components/utils/react.js | 3 +++ lib/radio/RadioGroup.js | 12 +++++++----- lib/utils/react.js | 9 +++++++++ 4 files changed, 28 insertions(+), 11 deletions(-) create mode 100644 components/utils/react.js create mode 100644 lib/utils/react.js diff --git a/components/radio/RadioGroup.js b/components/radio/RadioGroup.js index 7c612639..d15d5037 100644 --- a/components/radio/RadioGroup.js +++ b/components/radio/RadioGroup.js @@ -2,6 +2,7 @@ import React, { Component, PropTypes } from 'react'; import { themr } from 'react-css-themr'; import { RADIO } from '../identifiers.js'; import InjectRadioButton from './RadioButton.js'; +import { isComponentOfType } from '../utils/react.js'; const factory = (RadioButton) => { class RadioGroup extends Component { @@ -24,12 +25,14 @@ const factory = (RadioButton) => { }; renderRadioButtons () { - return React.Children.map(this.props.children, radio => ( - React.cloneElement(radio, { - checked: radio.props.value === this.props.value, - disabled: this.props.disabled || radio.props.disabled, - onChange: this.handleChange.bind(this, radio.props.value) - }) + return React.Children.map(this.props.children, child => ( + !isComponentOfType(RadioButton, child) + ? child + : React.cloneElement(child, { + checked: child.props.value === this.props.value, + disabled: this.props.disabled || child.props.disabled, + onChange: this.handleChange.bind(this, child.props.value) + }) )); } diff --git a/components/utils/react.js b/components/utils/react.js new file mode 100644 index 00000000..01860283 --- /dev/null +++ b/components/utils/react.js @@ -0,0 +1,3 @@ +export function isComponentOfType (classType, reactElement) { + return reactElement && reactElement.type === classType; +} diff --git a/lib/radio/RadioGroup.js b/lib/radio/RadioGroup.js index 0610b94d..aa7ff0bc 100644 --- a/lib/radio/RadioGroup.js +++ b/lib/radio/RadioGroup.js @@ -19,6 +19,8 @@ var _RadioButton = require('./RadioButton.js'); var _RadioButton2 = _interopRequireDefault(_RadioButton); +var _react3 = require('../utils/react.js'); + function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } @@ -52,11 +54,11 @@ var factory = function factory(RadioButton) { value: function renderRadioButtons() { var _this2 = this; - return _react2.default.Children.map(this.props.children, function (radio) { - return _react2.default.cloneElement(radio, { - checked: radio.props.value === _this2.props.value, - disabled: _this2.props.disabled || radio.props.disabled, - onChange: _this2.handleChange.bind(_this2, radio.props.value) + return _react2.default.Children.map(this.props.children, function (child) { + return !(0, _react3.isComponentOfType)(RadioButton, child) ? child : _react2.default.cloneElement(child, { + checked: child.props.value === _this2.props.value, + disabled: _this2.props.disabled || child.props.disabled, + onChange: _this2.handleChange.bind(_this2, child.props.value) }); }); } diff --git a/lib/utils/react.js b/lib/utils/react.js new file mode 100644 index 00000000..c6267df1 --- /dev/null +++ b/lib/utils/react.js @@ -0,0 +1,9 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.isComponentOfType = isComponentOfType; +function isComponentOfType(classType, reactElement) { + return reactElement && reactElement.type === classType; +} \ No newline at end of file