UNPKG

react-conventions

Version:

An open source set of React components that implement Ambassador's Design and UX patterns.

227 lines (189 loc) 8.59 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; 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 _react = require('react'); var _react2 = _interopRequireDefault(_react); var _bind = require('classnames/bind'); var _bind2 = _interopRequireDefault(_bind); var _Radio = require('./Radio'); var _Radio2 = _interopRequireDefault(_Radio); var _style = require('./style.scss'); var _style2 = _interopRequireDefault(_style); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } 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 (_React$Component) { _inherits(RadioGroup, _React$Component); function RadioGroup(props) { _classCallCheck(this, RadioGroup); var _this = _possibleConstructorReturn(this, (RadioGroup.__proto__ || Object.getPrototypeOf(RadioGroup)).call(this, props)); _this.state = { value: _this.props.value, options: [] }; _this.componentWillMount = function () { //form an array of options based on the children that were passed in //this can be done in the case of a RadioGroup with explicit children (see docs example) if (_this.props.children) { var childOptions = _this.props.children.reduce(function (options, child) { if (child.type === _Radio2.default) { options.push({ name: child.props.name, label: child.props.label }); } return options; }, []); _this.setState({ options: childOptions }); } if (typeof _this.state.value !== 'undefined' && (_this.state.options || _this.props.options)) { _this.checkItem(_this.state.value, _this.state.options || _this.props.options); } }; _this.componentWillReceiveProps = function (nextProps) { if (nextProps.value !== _this.state.value) { _this.setState({ value: nextProps.value }, function () { _this.checkItem(nextProps.value, _this.state.options || nextProps.options); }); } }; _this.handleChange = function (event, value) { event.persist(); if (value !== _this.state.value) { _this.setState({ value: value }, function () { _this.checkItem(value, _this.state.options || _this.props.options); }); if (typeof _this.props.changeCallback === 'function') { _this.props.changeCallback(event, value); } } }; _this.checkItem = function (value, options) { var index = _this.getIndex(value, options); if (index >= 0) { options[index].checked = true; } }; _this.getIndex = function (value, options) { var optionIndex = -1; options.map(function (radio, index) { if (radio.value === value) { optionIndex = index; return; } }); return optionIndex; }; _this.getOptions = function () { var groupName = _this.props.name; var groupLabelPosition = _this.props.labelPosition; var _this$props = _this.props, options = _this$props.options, label = _this$props.label, name = _this$props.name, value = _this$props.value, required = _this$props.required, labelPosition = _this$props.labelPosition, changeCallback = _this$props.changeCallback, other = _objectWithoutProperties(_this$props, ['options', 'label', 'name', 'value', 'required', 'labelPosition', 'changeCallback']); //this means explicit radio buttons were defined (usually paired with other form fields) //we create an options array in the state (because there is no options in props) for checkItem to use if (_this.props.children) { return _this.props.children.map(function (child, index) { if (child.type === _Radio2.default) { return _react2.default.cloneElement(child, { key: index, name: groupName, checked: _this.state.value === child.props.value, changeCallback: _this.handleChange, labelPosition: groupLabelPosition }); } else { return child; } }); } //this means a normal RadioGroup with an options array was defined else { return _this.props.options.map(function (option) { return _react2.default.createElement(_Radio2.default, _extends({ key: option.value, value: option.value, label: option.label, name: groupName, checked: _this.state.value === option.value, labelPosition: groupLabelPosition, optClass: option.optClass, changeCallback: _this.handleChange }, other)); }); } }; return _this; } _createClass(RadioGroup, [{ key: 'render', value: function render() { var cx = _bind2.default.bind(_style2.default); var radioGroupClass = cx(_style2.default['radio-group'], this.props.optClass); return _react2.default.createElement( 'div', { className: radioGroupClass }, this.props.required ? _react2.default.createElement( 'span', { className: _style2.default.asterisk }, '*' ) : null, this.props.label ? _react2.default.createElement( 'label', { className: _style2.default['radio-group-label'] }, this.props.label ) : null, this.getOptions() ); } }]); return RadioGroup; }(_react2.default.Component); RadioGroup.defaultProps = { disabled: false, required: false, labelPosition: 'right' }; RadioGroup.propTypes = { /** * Text shown above the radio group. */ label: _react2.default.PropTypes.string, /** * The name that will be applied to all radio buttons inside it. */ name: _react2.default.PropTypes.string.isRequired, /** * Whether the radio group is required. */ required: _react2.default.PropTypes.bool, /** * Whether the radio group is disabled. */ disabled: _react2.default.PropTypes.bool, /** * A list of options for the radio group. */ options: _react2.default.PropTypes.array, /** * Which option is checked. */ value: _react2.default.PropTypes.string, /** * Where the label will be placed for all radio buttons. This will override any labelPosition properties defined for an individual radio button. */ labelPosition: _react2.default.PropTypes.oneOf(['left', 'right']), /** * A callback function to be called when an option is changed. */ changeCallback: _react2.default.PropTypes.func }; exports.default = RadioGroup;