UNPKG

3.88 kBJavaScriptView Raw
1import _extends from "@babel/runtime-corejs2/helpers/esm/extends";
2import _objectWithoutPropertiesLoose from "@babel/runtime-corejs2/helpers/esm/objectWithoutPropertiesLoose";
3import _inheritsLoose from "@babel/runtime-corejs2/helpers/esm/inheritsLoose";
4import PropTypes from 'prop-types';
5import React from 'react';
6import invariant from 'invariant';
7import uncontrollable from 'uncontrollable';
8import chainFunction from './utils/createChainedFunction';
9import ValidChildren from './utils/ValidComponentChildren';
10import ButtonGroup from './ButtonGroup';
11import ToggleButton from './ToggleButton';
12var propTypes = {
13 /**
14 * An HTML `<input>` name for each child button.
15 *
16 * __Required if `type` is set to `'radio'`__
17 */
18 name: PropTypes.string,
19
20 /**
21 * The value, or array of values, of the active (pressed) buttons
22 *
23 * @controllable onChange
24 */
25 value: PropTypes.any,
26
27 /**
28 * Callback fired when a button is pressed, depending on whether the `type`
29 * is `'radio'` or `'checkbox'`, `onChange` will be called with the value or
30 * array of active values
31 *
32 * @controllable values
33 */
34 onChange: PropTypes.func,
35
36 /**
37 * The input `type` of the rendered buttons, determines the toggle behavior
38 * of the buttons
39 */
40 type: PropTypes.oneOf(['checkbox', 'radio']).isRequired
41};
42var defaultProps = {
43 type: 'radio'
44};
45
46var ToggleButtonGroup =
47/*#__PURE__*/
48function (_React$Component) {
49 _inheritsLoose(ToggleButtonGroup, _React$Component);
50
51 function ToggleButtonGroup() {
52 return _React$Component.apply(this, arguments) || this;
53 }
54
55 var _proto = ToggleButtonGroup.prototype;
56
57 _proto.getValues = function getValues() {
58 var value = this.props.value;
59 return value == null ? [] : [].concat(value);
60 };
61
62 _proto.handleToggle = function handleToggle(value) {
63 var _this$props = this.props,
64 type = _this$props.type,
65 onChange = _this$props.onChange;
66 var values = this.getValues();
67 var isActive = values.indexOf(value) !== -1;
68
69 if (type === 'radio') {
70 if (!isActive) {
71 onChange(value);
72 }
73
74 return;
75 }
76
77 if (isActive) {
78 onChange(values.filter(function (n) {
79 return n !== value;
80 }));
81 } else {
82 onChange(values.concat([value]));
83 }
84 };
85
86 _proto.render = function render() {
87 var _this = this;
88
89 var _this$props2 = this.props,
90 children = _this$props2.children,
91 type = _this$props2.type,
92 name = _this$props2.name,
93 props = _objectWithoutPropertiesLoose(_this$props2, ["children", "type", "name"]);
94
95 var values = this.getValues();
96 !(type !== 'radio' || !!name) ? process.env.NODE_ENV !== "production" ? invariant(false, 'A `name` is required to group the toggle buttons when the `type` ' + 'is set to "radio"') : invariant(false) : void 0;
97 delete props.onChange;
98 delete props.value; // the data attribute is required b/c twbs css uses it in the selector
99
100 return React.createElement(ButtonGroup, _extends({}, props, {
101 "data-toggle": "buttons"
102 }), ValidChildren.map(children, function (child) {
103 var _child$props = child.props,
104 value = _child$props.value,
105 onChange = _child$props.onChange;
106
107 var handler = function handler() {
108 return _this.handleToggle(value);
109 };
110
111 return React.cloneElement(child, {
112 type: type,
113 name: child.name || name,
114 checked: values.indexOf(value) !== -1,
115 onChange: chainFunction(onChange, handler)
116 });
117 }));
118 };
119
120 return ToggleButtonGroup;
121}(React.Component);
122
123ToggleButtonGroup.propTypes = propTypes;
124ToggleButtonGroup.defaultProps = defaultProps;
125var UncontrolledToggleButtonGroup = uncontrollable(ToggleButtonGroup, {
126 value: 'onChange'
127});
128UncontrolledToggleButtonGroup.Button = ToggleButton;
129export default UncontrolledToggleButtonGroup;
\No newline at end of file