Javi Velasco 2016-09-03 16:08:52 +02:00
parent cd72ffb440
commit ccc1699cc4
4 changed files with 28 additions and 11 deletions

View File

@ -2,6 +2,7 @@ import React, { Component, PropTypes } from 'react';
import { themr } from 'react-css-themr'; import { themr } from 'react-css-themr';
import { RADIO } from '../identifiers.js'; import { RADIO } from '../identifiers.js';
import InjectRadioButton from './RadioButton.js'; import InjectRadioButton from './RadioButton.js';
import { isComponentOfType } from '../utils/react.js';
const factory = (RadioButton) => { const factory = (RadioButton) => {
class RadioGroup extends Component { class RadioGroup extends Component {
@ -24,11 +25,13 @@ const factory = (RadioButton) => {
}; };
renderRadioButtons () { renderRadioButtons () {
return React.Children.map(this.props.children, radio => ( return React.Children.map(this.props.children, child => (
React.cloneElement(radio, { !isComponentOfType(RadioButton, child)
checked: radio.props.value === this.props.value, ? child
disabled: this.props.disabled || radio.props.disabled, : React.cloneElement(child, {
onChange: this.handleChange.bind(this, radio.props.value) checked: child.props.value === this.props.value,
disabled: this.props.disabled || child.props.disabled,
onChange: this.handleChange.bind(this, child.props.value)
}) })
)); ));
} }

3
components/utils/react.js vendored Normal file
View File

@ -0,0 +1,3 @@
export function isComponentOfType (classType, reactElement) {
return reactElement && reactElement.type === classType;
}

View File

@ -19,6 +19,8 @@ var _RadioButton = require('./RadioButton.js');
var _RadioButton2 = _interopRequireDefault(_RadioButton); var _RadioButton2 = _interopRequireDefault(_RadioButton);
var _react3 = require('../utils/react.js');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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"); } } 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() { value: function renderRadioButtons() {
var _this2 = this; var _this2 = this;
return _react2.default.Children.map(this.props.children, function (radio) { return _react2.default.Children.map(this.props.children, function (child) {
return _react2.default.cloneElement(radio, { return !(0, _react3.isComponentOfType)(RadioButton, child) ? child : _react2.default.cloneElement(child, {
checked: radio.props.value === _this2.props.value, checked: child.props.value === _this2.props.value,
disabled: _this2.props.disabled || radio.props.disabled, disabled: _this2.props.disabled || child.props.disabled,
onChange: _this2.handleChange.bind(_this2, radio.props.value) onChange: _this2.handleChange.bind(_this2, child.props.value)
}); });
}); });
} }

9
lib/utils/react.js vendored Normal file
View File

@ -0,0 +1,9 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isComponentOfType = isComponentOfType;
function isComponentOfType(classType, reactElement) {
return reactElement && reactElement.type === classType;
}