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 _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; }; }();
|
10 |
|
11 | var _react = require('react');
|
12 |
|
13 | var _react2 = _interopRequireDefault(_react);
|
14 |
|
15 | var _propTypes = require('prop-types');
|
16 |
|
17 | var _propTypes2 = _interopRequireDefault(_propTypes);
|
18 |
|
19 | var _bind = require('classnames/bind');
|
20 |
|
21 | var _bind2 = _interopRequireDefault(_bind);
|
22 |
|
23 | var _Radio = require('./Radio');
|
24 |
|
25 | var _Radio2 = _interopRequireDefault(_Radio);
|
26 |
|
27 | var _style = require('./style.scss');
|
28 |
|
29 | var _style2 = _interopRequireDefault(_style);
|
30 |
|
31 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
32 |
|
33 | 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; }
|
34 |
|
35 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
36 |
|
37 | 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; }
|
38 |
|
39 | 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; }
|
40 |
|
41 | var RadioGroup = function (_React$Component) {
|
42 | _inherits(RadioGroup, _React$Component);
|
43 |
|
44 | function RadioGroup(props) {
|
45 | _classCallCheck(this, RadioGroup);
|
46 |
|
47 | var _this = _possibleConstructorReturn(this, (RadioGroup.__proto__ || Object.getPrototypeOf(RadioGroup)).call(this, props));
|
48 |
|
49 | _this.state = {
|
50 | value: _this.props.value,
|
51 | options: []
|
52 | };
|
53 |
|
54 | _this.UNSAFE_componentWillMount = function () {
|
55 |
|
56 |
|
57 | if (_this.props.children) {
|
58 | var childOptions = _this.props.children.reduce(function (options, child) {
|
59 | if (child.type === _Radio2.default) {
|
60 | options.push({ name: child.props.name, label: child.props.label });
|
61 | }
|
62 | return options;
|
63 | }, []);
|
64 |
|
65 | _this.setState({ options: childOptions });
|
66 | }
|
67 |
|
68 | if (typeof _this.state.value !== 'undefined' && (_this.state.options || _this.props.options)) {
|
69 | _this.checkItem(_this.state.value, _this.state.options || _this.props.options);
|
70 | }
|
71 | };
|
72 |
|
73 | _this.UNSAFE_componentWillReceiveProps = function (nextProps) {
|
74 | if (nextProps.value !== undefined && nextProps.value !== _this.state.value) {
|
75 | _this.setState({ value: nextProps.value }, function () {
|
76 | _this.checkItem(nextProps.value, _this.state.options || nextProps.options);
|
77 | });
|
78 | }
|
79 | };
|
80 |
|
81 | _this.handleChange = function (event, value) {
|
82 | if (value !== _this.state.value) {
|
83 | _this.setState({ value: value }, function () {
|
84 | _this.checkItem(value, _this.state.options || _this.props.options);
|
85 | });
|
86 | if (typeof _this.props.changeCallback === 'function') {
|
87 | _this.props.changeCallback(event, value);
|
88 | }
|
89 | }
|
90 | };
|
91 |
|
92 | _this.checkItem = function (value, options) {
|
93 | var index = _this.getIndex(value, options);
|
94 |
|
95 | if (index >= 0) {
|
96 | options[index].checked = true;
|
97 | }
|
98 | };
|
99 |
|
100 | _this.getIndex = function (value, options) {
|
101 | var optionIndex = -1;
|
102 |
|
103 | options.map(function (radio, index) {
|
104 | if (radio.value === value) {
|
105 | optionIndex = index;
|
106 | return;
|
107 | }
|
108 | });
|
109 |
|
110 | return optionIndex;
|
111 | };
|
112 |
|
113 | _this.getOptions = function () {
|
114 | var groupName = _this.props.name;
|
115 |
|
116 | var _this$props = _this.props,
|
117 | options = _this$props.options,
|
118 | label = _this$props.label,
|
119 | name = _this$props.name,
|
120 | value = _this$props.value,
|
121 | description = _this$props.description,
|
122 | changeCallback = _this$props.changeCallback,
|
123 | other = _objectWithoutProperties(_this$props, ['options', 'label', 'name', 'value', 'description', 'changeCallback']);
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 | if (_this.props.children) {
|
130 | return _this.props.children.map(function (child, index) {
|
131 | if (child.type === _Radio2.default) {
|
132 | return _react2.default.cloneElement(child, {
|
133 | key: index,
|
134 | name: groupName,
|
135 | checked: _this.state.value === child.props.value,
|
136 | changeCallback: _this.handleChange
|
137 | });
|
138 | }
|
139 | return child;
|
140 | });
|
141 | }
|
142 |
|
143 |
|
144 | return _this.props.options.map(function (option) {
|
145 | return _react2.default.createElement(_Radio2.default, _extends({
|
146 | key: option.value,
|
147 | value: option.value,
|
148 | label: option.label,
|
149 | description: option.description,
|
150 | name: groupName,
|
151 | checked: _this.state.value === option.value,
|
152 | optClass: option.optClass,
|
153 | changeCallback: _this.handleChange
|
154 | }, other));
|
155 | });
|
156 | };
|
157 |
|
158 | return _this;
|
159 | }
|
160 |
|
161 | _createClass(RadioGroup, [{
|
162 | key: 'render',
|
163 | value: function render() {
|
164 | var cx = _bind2.default.bind(_style2.default);
|
165 | var radioGroupClass = cx(_style2.default['radio-group'], this.props.optClass);
|
166 |
|
167 | return _react2.default.createElement(
|
168 | 'div',
|
169 | { className: radioGroupClass },
|
170 | this.props.label ? _react2.default.createElement(
|
171 | 'label',
|
172 | { className: _style2.default['radio-group-label'] },
|
173 | this.props.label
|
174 | ) : null,
|
175 | this.getOptions()
|
176 | );
|
177 | }
|
178 | }]);
|
179 |
|
180 | return RadioGroup;
|
181 | }(_react2.default.Component);
|
182 |
|
183 | RadioGroup.defaultProps = {
|
184 | disabled: false
|
185 | };
|
186 | RadioGroup.propTypes = {
|
187 | |
188 |
|
189 |
|
190 | label: _propTypes2.default.string,
|
191 | |
192 |
|
193 |
|
194 | name: _propTypes2.default.string.isRequired,
|
195 | |
196 |
|
197 |
|
198 | disabled: _propTypes2.default.bool,
|
199 | |
200 |
|
201 |
|
202 | options: _propTypes2.default.array,
|
203 | |
204 |
|
205 |
|
206 | value: _propTypes2.default.oneOfType([_propTypes2.default.bool, _propTypes2.default.string]),
|
207 | |
208 |
|
209 |
|
210 | changeCallback: _propTypes2.default.func,
|
211 | |
212 |
|
213 |
|
214 | description: _propTypes2.default.string
|
215 | };
|
216 | exports.default = RadioGroup; |
\ | No newline at end of file |