1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | 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; };
|
8 |
|
9 | var _react = require('react');
|
10 |
|
11 | var _react2 = _interopRequireDefault(_react);
|
12 |
|
13 | var _propTypes = require('prop-types');
|
14 |
|
15 | var _propTypes2 = _interopRequireDefault(_propTypes);
|
16 |
|
17 | var _bind = require('classnames/bind');
|
18 |
|
19 | var _bind2 = _interopRequireDefault(_bind);
|
20 |
|
21 | var _ButtonToggle = require('./ButtonToggle');
|
22 |
|
23 | var _ButtonToggle2 = _interopRequireDefault(_ButtonToggle);
|
24 |
|
25 | var _style = require('./style.scss');
|
26 |
|
27 | var _style2 = _interopRequireDefault(_style);
|
28 |
|
29 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
30 |
|
31 | 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; }
|
32 |
|
33 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
34 |
|
35 | 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; }
|
36 |
|
37 | 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; }
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | var ButtonGroup = function (_React$Component) {
|
43 | _inherits(ButtonGroup, _React$Component);
|
44 |
|
45 | function ButtonGroup(props) {
|
46 | _classCallCheck(this, ButtonGroup);
|
47 |
|
48 | var _this = _possibleConstructorReturn(this, (ButtonGroup.__proto__ || Object.getPrototypeOf(ButtonGroup)).call(this, props));
|
49 |
|
50 | _this.state = {
|
51 | checkedOption: _this.props.defaultOption !== undefined ? _this.props.options[_this.props.defaultOption][_this.props.valueProp] : ''
|
52 | };
|
53 |
|
54 | _this.UNSAFE_componentWillMount = function () {
|
55 | if (typeof _this.props.defaultOption !== 'undefined') {
|
56 | _this.props.options[_this.props.defaultOption].checked = true;
|
57 | }
|
58 | };
|
59 |
|
60 | _this.UNSAFE_componentWillReceiveProps = function (nextProps) {
|
61 | if (typeof nextProps.defaultOption !== 'undefined' && nextProps.options[nextProps.defaultOption][nextProps.valueProp] !== _this.state.checkedOption) {
|
62 | nextProps.options[nextProps.defaultOption].checked = true;
|
63 | _this.setState({ checkedOption: nextProps.options[nextProps.defaultOption][nextProps.valueProp] });
|
64 | }
|
65 | };
|
66 |
|
67 | _this.handleChange = function (event, value) {
|
68 | event.persist();
|
69 | _this.setState({ checkedOption: value }, function () {
|
70 | if (typeof _this.props.changeCallback === 'function') {
|
71 | _this.props.changeCallback(event, value);
|
72 | }
|
73 | });
|
74 | };
|
75 |
|
76 | _this.getOptions = function () {
|
77 | var groupName = _this.props.name;
|
78 | var buttonStyle = _this.props.buttonStyle;
|
79 |
|
80 | var _this$props = _this.props,
|
81 | options = _this$props.options,
|
82 | label = _this$props.label,
|
83 | name = _this$props.name,
|
84 | value = _this$props.value,
|
85 | required = _this$props.required,
|
86 | defaultOption = _this$props.defaultOption,
|
87 | changeCallback = _this$props.changeCallback,
|
88 | other = _objectWithoutProperties(_this$props, ['options', 'label', 'name', 'value', 'required', 'defaultOption', 'changeCallback']);
|
89 |
|
90 | return _this.props.options.map(function (buttonToggle, index) {
|
91 | return _react2.default.createElement(_ButtonToggle2.default, _extends({
|
92 | key: buttonToggle[_this.props.valueProp],
|
93 | value: buttonToggle[_this.props.valueProp],
|
94 | label: buttonToggle[_this.props.displayProp],
|
95 | name: groupName,
|
96 | checked: _this.state.checkedOption === buttonToggle[_this.props.valueProp],
|
97 | optClass: _this.state.checkedOption === buttonToggle[_this.props.valueProp] ? buttonStyle : 'secondary',
|
98 | changeCallback: _this.handleChange
|
99 | }, other));
|
100 | });
|
101 | };
|
102 |
|
103 | _this.render = function () {
|
104 | var cx = _bind2.default.bind(_style2.default);
|
105 | var buttonGroupClass = cx(_style2.default['button-group'], _this.props.optClass);
|
106 |
|
107 | return _react2.default.createElement(
|
108 | 'div',
|
109 | { className: buttonGroupClass },
|
110 | _this.props.required ? _react2.default.createElement(
|
111 | 'span',
|
112 | { className: _style2.default.asterisk },
|
113 | '*'
|
114 | ) : null,
|
115 | _this.props.label ? _react2.default.createElement(
|
116 | 'label',
|
117 | { className: _style2.default['button-group-label'] },
|
118 | _this.props.label
|
119 | ) : null,
|
120 | _react2.default.createElement(
|
121 | 'div',
|
122 | { className: _style2.default.options },
|
123 | _this.getOptions()
|
124 | )
|
125 | );
|
126 | };
|
127 |
|
128 | return _this;
|
129 | }
|
130 |
|
131 | return ButtonGroup;
|
132 | }(_react2.default.Component);
|
133 |
|
134 | ButtonGroup.defaultProps = {
|
135 | disabled: false,
|
136 | required: false,
|
137 | displayProp: 'label',
|
138 | valueProp: 'value'
|
139 | };
|
140 | ButtonGroup.propTypes = {
|
141 | |
142 |
|
143 |
|
144 | label: _propTypes2.default.string,
|
145 | |
146 |
|
147 |
|
148 | name: _propTypes2.default.string.isRequired,
|
149 | |
150 |
|
151 |
|
152 | required: _propTypes2.default.bool,
|
153 | |
154 |
|
155 |
|
156 | disabled: _propTypes2.default.bool,
|
157 | |
158 |
|
159 |
|
160 | options: _propTypes2.default.array.isRequired,
|
161 | |
162 |
|
163 |
|
164 | displayProp: _propTypes2.default.string,
|
165 | |
166 |
|
167 |
|
168 | valueProp: _propTypes2.default.string,
|
169 | |
170 |
|
171 |
|
172 | defaultOption: _propTypes2.default.number,
|
173 | |
174 |
|
175 |
|
176 | changeCallback: _propTypes2.default.func,
|
177 | |
178 |
|
179 |
|
180 | buttonStyle: _propTypes2.default.string
|
181 | };
|
182 | exports.default = ButtonGroup; |
\ | No newline at end of file |