1 | "use strict";
|
2 |
|
3 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4 | exports.__esModule = true;
|
5 | exports.default = void 0;
|
6 | var React = _interopRequireWildcard(require("react"));
|
7 | var _invariant = _interopRequireDefault(require("invariant"));
|
8 | var _uncontrollable = require("uncontrollable");
|
9 | var _createChainedFunction = _interopRequireDefault(require("./createChainedFunction"));
|
10 | var _ElementChildren = require("./ElementChildren");
|
11 | var _ButtonGroup = _interopRequireDefault(require("./ButtonGroup"));
|
12 | var _ToggleButton = _interopRequireDefault(require("./ToggleButton"));
|
13 | var _jsxRuntime = require("react/jsx-runtime");
|
14 | function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
15 | function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
16 | const ToggleButtonGroup = React.forwardRef((props, ref) => {
|
17 | const {
|
18 | children,
|
19 | type = 'radio',
|
20 | name,
|
21 | value,
|
22 | onChange,
|
23 | vertical = false,
|
24 | ...controlledProps
|
25 | } = (0, _uncontrollable.useUncontrolled)(props, {
|
26 | value: 'onChange'
|
27 | });
|
28 | const getValues = () => value == null ? [] : [].concat(value);
|
29 | const handleToggle = (inputVal, event) => {
|
30 | if (!onChange) {
|
31 | return;
|
32 | }
|
33 | const values = getValues();
|
34 | const isActive = values.indexOf(inputVal) !== -1;
|
35 | if (type === 'radio') {
|
36 | if (!isActive) onChange(inputVal, event);
|
37 | return;
|
38 | }
|
39 | if (isActive) {
|
40 | onChange(values.filter(n => n !== inputVal), event);
|
41 | } else {
|
42 | onChange([...values, inputVal], event);
|
43 | }
|
44 | };
|
45 | !(type !== 'radio' || !!name) ? process.env.NODE_ENV !== "production" ? (0, _invariant.default)(false, 'A `name` is required to group the toggle buttons when the `type` ' + 'is set to "radio"') : invariant(false) : void 0;
|
46 | return (0, _jsxRuntime.jsx)(_ButtonGroup.default, {
|
47 | ...controlledProps,
|
48 | ref: ref,
|
49 | vertical: vertical,
|
50 | children: (0, _ElementChildren.map)(children, child => {
|
51 | const values = getValues();
|
52 | const {
|
53 | value: childVal,
|
54 | onChange: childOnChange
|
55 | } = child.props;
|
56 | const handler = e => handleToggle(childVal, e);
|
57 | return React.cloneElement(child, {
|
58 | type,
|
59 | name: child.name || name,
|
60 | checked: values.indexOf(childVal) !== -1,
|
61 | onChange: (0, _createChainedFunction.default)(childOnChange, handler)
|
62 | });
|
63 | })
|
64 | });
|
65 | });
|
66 | var _default = Object.assign(ToggleButtonGroup, {
|
67 | Button: _ToggleButton.default
|
68 | });
|
69 | exports.default = _default;
|
70 | module.exports = exports.default; |
\ | No newline at end of file |