react-conventions
Version:
An open source set of React components that implement Ambassador's Design and UX patterns.
178 lines (148 loc) • 6.66 kB
JavaScript
'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 _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _bind = require('classnames/bind');
var _bind2 = _interopRequireDefault(_bind);
var _ButtonToggle = require('./ButtonToggle');
var _ButtonToggle2 = _interopRequireDefault(_ButtonToggle);
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; }
/**
* The ButtonGroup component.
*/
var ButtonGroup = function (_React$Component) {
_inherits(ButtonGroup, _React$Component);
function ButtonGroup(props) {
_classCallCheck(this, ButtonGroup);
var _this = _possibleConstructorReturn(this, (ButtonGroup.__proto__ || Object.getPrototypeOf(ButtonGroup)).call(this, props));
_this.state = {
checkedOption: _this.props.defaultOption !== undefined ? _this.props.options[_this.props.defaultOption][_this.props.valueProp] : ''
};
_this.componentWillMount = function () {
if (typeof _this.props.defaultOption !== 'undefined') {
_this.props.options[_this.props.defaultOption].checked = true;
}
};
_this.componentWillReceiveProps = function (nextProps) {
if (typeof nextProps.defaultOption !== 'undefined' && nextProps.options[nextProps.defaultOption][nextProps.valueProp] !== _this.state.checkedOption) {
nextProps.options[nextProps.defaultOption].checked = true;
_this.setState({ checkedOption: nextProps.options[nextProps.defaultOption][nextProps.valueProp] });
}
};
_this.handleChange = function (event, value) {
event.persist();
_this.setState({ checkedOption: value }, function () {
if (typeof _this.props.changeCallback === 'function') {
_this.props.changeCallback(event, value);
}
});
};
_this.getOptions = function () {
var groupName = _this.props.name;
var buttonStyle = _this.props.buttonStyle;
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,
defaultOption = _this$props.defaultOption,
changeCallback = _this$props.changeCallback,
other = _objectWithoutProperties(_this$props, ['options', 'label', 'name', 'value', 'required', 'defaultOption', 'changeCallback']);
return _this.props.options.map(function (buttonToggle, index) {
return _react2.default.createElement(_ButtonToggle2.default, _extends({
key: buttonToggle[_this.props.valueProp],
value: buttonToggle[_this.props.valueProp],
label: buttonToggle[_this.props.displayProp],
name: groupName,
checked: _this.state.checkedOption === buttonToggle[_this.props.valueProp],
optClass: _this.state.checkedOption === buttonToggle[_this.props.valueProp] ? buttonStyle : 'secondary',
changeCallback: _this.handleChange
}, other));
});
};
_this.render = function () {
var cx = _bind2.default.bind(_style2.default);
var buttonGroupClass = cx(_style2.default['button-group'], _this.props.optClass);
return _react2.default.createElement(
'div',
{ className: buttonGroupClass },
_this.props.required ? _react2.default.createElement(
'span',
{ className: _style2.default.asterisk },
'*'
) : null,
_this.props.label ? _react2.default.createElement(
'label',
{ className: _style2.default['button-group-label'] },
_this.props.label
) : null,
_react2.default.createElement(
'div',
{ className: _style2.default.options },
_this.getOptions()
)
);
};
return _this;
}
return ButtonGroup;
}(_react2.default.Component);
ButtonGroup.defaultProps = {
disabled: false,
required: false,
displayProp: 'label',
valueProp: 'value'
};
ButtonGroup.propTypes = {
/**
* Text shown above the button 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 button group is required.
*/
required: _react2.default.PropTypes.bool,
/**
* Whether the button group is disabled.
*/
disabled: _react2.default.PropTypes.bool,
/**
* A list of options for the button group.
*/
options: _react2.default.PropTypes.array.isRequired,
/**
* The property to be used as the display property
*/
displayProp: _react2.default.PropTypes.string,
/**
* The property to be used as the value property
*/
valueProp: _react2.default.PropTypes.string,
/**
* Which option is checked by default.
*/
defaultOption: _react2.default.PropTypes.number,
/**
* A callback function to be called when an option is changed.
*/
changeCallback: _react2.default.PropTypes.func,
/**
* The style for the buttons in the group.
*/
buttonStyle: _react2.default.PropTypes.string
};
exports.default = ButtonGroup;