UNPKG

dragon-mobile-ui

Version:
132 lines (103 loc) 5.11 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _classnames2 = require('classnames'); var _classnames3 = _interopRequireDefault(_classnames2); var _Radio = require('./Radio'); var _Radio2 = _interopRequireDefault(_Radio); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var RadioGroup = function (_Component) { _inherits(RadioGroup, _Component); function RadioGroup(props) { _classCallCheck(this, RadioGroup); var _this = _possibleConstructorReturn(this, (RadioGroup.__proto__ || Object.getPrototypeOf(RadioGroup)).call(this, props)); _this.state = { value: props.value || props.defaultValue || _this.getCheckedValue(props.children) }; return _this; } _createClass(RadioGroup, [{ key: 'componentWillReceiveProps', value: function componentWillReceiveProps(nextProps) { if ('value' in nextProps || this.getCheckedValue(nextProps.children)) { this.setState({ value: nextProps.value || this.getCheckedValue(nextProps.children) }); } } }, { key: 'render', value: function render() { var _this2 = this, _classnames; var props = this.props; var type = props.type, disabled = props.disabled, isBlock = props.isBlock, isRadius = props.isRadius, isDisabled = props.isDisabled, theme = props.theme, size = props.size, className = props.className, children = props.children; var items = _react2.default.Children.map(children, function (element, index) { return (0, _react.cloneElement)(element, { key: index, type: type, onChange: function onChange() { return _this2.onRadioChange(element.props.value); }, checked: _this2.state.value === element.props.value }); }); var cls = (0, _classnames3.default)((_classnames = { 'ui-radio-group': true, 'block': 'block' in props || isBlock, 'radius': 'radius' in props || isRadius, 'disabled': 'disabled' in props || isDisabled }, _defineProperty(_classnames, 'theme-' + theme, !!theme), _defineProperty(_classnames, 'size-' + size, !!size), _defineProperty(_classnames, className, !!className), _classnames)); return _react2.default.createElement( 'div', { className: cls }, items ); } }, { key: 'getCheckedValue', value: function getCheckedValue(children) { var checkedValue = null; _react2.default.Children.forEach(children, function (radio) { if (radio.props && radio.props.checked) { checkedValue = radio.props.value; } }); return checkedValue; } }, { key: 'onRadioChange', value: function onRadioChange(value) { this.setState({ value: value }); this.props.onChange(value); } }]); return RadioGroup; }(_react.Component); RadioGroup.propTypes = { type: _propTypes2.default.oneOf(['default', 'button']), onChange: _propTypes2.default.func }; RadioGroup.defaultProps = { type: 'default', onChange: function onChange() {} }; exports.default = RadioGroup;