@momentum-ui/react-collaboration
Version:
Cisco Momentum UI Framework for React Collaboration Applications
58 lines • 3.14 kB
JavaScript
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
import React, { forwardRef, useRef, useCallback, useEffect, useState } from 'react';
import classnames from 'classnames';
import { Button as MdcButton } from '@momentum-design/components/dist/react';
import { DEFAULTS } from './ButtonPillToggle.constants';
import { DEFAULTS as BUTTON_PILL_DEFAULTS } from '../ButtonPill/ButtonPill.constants';
/**
* @deprecated Use the equivalent from momentum.design (NPM: `@momentum-design/components/dist/react`)
*/
var ButtonPillToggle = forwardRef(function (props, providedRef) {
var _a = props.isSelected, isSelected = _a === void 0 ? DEFAULTS.SELECTED : _a, // controlled
_b = props.initialIsSelected, // controlled
initialIsSelected = _b === void 0 ? DEFAULTS.SELECTED : _b, // uncontrolled
className = props.className, _c = props.outline, outline = _c === void 0 ? DEFAULTS.OUTLINE : _c, _d = props.size, size = _d === void 0 ? BUTTON_PILL_DEFAULTS.SIZE : _d, otherProps = __rest(props, ["isSelected", "initialIsSelected", "className", "outline", "size"]);
var isControlled = isSelected !== undefined;
var internalRef = useRef();
var ref = providedRef || internalRef;
var _e = useState(isControlled ? isSelected : initialIsSelected), selected = _e[0], setSelected = _e[1];
useEffect(function () {
if (isControlled) {
setSelected(isSelected);
}
}, [isControlled, isSelected]);
// MdcButton is uncontrolled, so we need to handle the state manually
var handleToggle = useCallback(function () {
var _a;
var newValue = isControlled ? isSelected : !selected;
if (!isControlled) {
setSelected(newValue);
}
(_a = otherProps.onChange) === null || _a === void 0 ? void 0 : _a.call(otherProps, newValue); // when the toggle is controlled, onChange is, ironically, the one causing the change of isSelected
}, [isControlled, isSelected, selected, otherProps]);
return (React.createElement(MdcButton, __assign({ active: selected, ref: ref, className: classnames(className), size: size, variant: outline ? 'secondary' : 'tertiary', onClick: handleToggle }, otherProps)));
});
ButtonPillToggle.displayName = 'ButtonPillToggle';
export default ButtonPillToggle;
//# sourceMappingURL=ButtonPillToggle.js.map