UNPKG

8.05 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _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
9var _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
11var _react = require('react');
12
13var _react2 = _interopRequireDefault(_react);
14
15var _propTypes = require('prop-types');
16
17var _propTypes2 = _interopRequireDefault(_propTypes);
18
19var _bind = require('classnames/bind');
20
21var _bind2 = _interopRequireDefault(_bind);
22
23var _Radio = require('./Radio');
24
25var _Radio2 = _interopRequireDefault(_Radio);
26
27var _style = require('./style.scss');
28
29var _style2 = _interopRequireDefault(_style);
30
31function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
32
33function _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
35function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
36
37function _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
39function _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
41var 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 // form an array of options based on the children that were passed in
56 // this can be done in the case of a RadioGroup with explicit children (see docs example)
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 // this means explicit radio buttons were defined (usually paired with other form fields)
126 // we create an options array in the state (because there is no options in props) for checkItem to use
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 // this means a normal RadioGroup with an options array was defined
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
183RadioGroup.defaultProps = {
184 disabled: false
185};
186RadioGroup.propTypes = {
187 /**
188 * Text shown above the radio group.
189 */
190 label: _propTypes2.default.string,
191 /**
192 * The name that will be applied to all radio buttons inside it.
193 */
194 name: _propTypes2.default.string.isRequired,
195 /**
196 * Whether the radio group is disabled.
197 */
198 disabled: _propTypes2.default.bool,
199 /**
200 * A list of options for the radio group.
201 */
202 options: _propTypes2.default.array,
203 /**
204 * Which option is checked.
205 */
206 value: _propTypes2.default.oneOfType([_propTypes2.default.bool, _propTypes2.default.string]),
207 /**
208 * A callback function to be called when an option is changed.
209 */
210 changeCallback: _propTypes2.default.func,
211 /**
212 * An optional string that appears below the label
213 */
214 description: _propTypes2.default.string
215};
216exports.default = RadioGroup;
\No newline at end of file