wix-style-react
Version:
153 lines (122 loc) • 6 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
var _excluded = ["dataHook", "children", "disabled", "defaultSelected", "onClick", "selected"];
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
import React from 'react';
import PropTypes from 'prop-types';
import { st, classes } from './SegmentedToggle.st.css';
import ToggleButton from './ToggleButton/ToggleButton';
import ToggleIcon from './ToggleIcon/ToggleIcon';
var SegmentedToggle = /*#__PURE__*/function (_React$Component) {
_inherits(SegmentedToggle, _React$Component);
var _super = _createSuper(SegmentedToggle);
function SegmentedToggle() {
var _this;
_classCallCheck(this, SegmentedToggle);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _super.call.apply(_super, [this].concat(args));
_defineProperty(_assertThisInitialized(_this), "state", {
selected: _this.props.defaultSelected
});
_defineProperty(_assertThisInitialized(_this), "_onClick", function (evt) {
var _this$props = _this.props,
onClick = _this$props.onClick,
selected = _this$props.selected;
var value = evt.currentTarget.value;
if (selected) {
return onClick && onClick(evt, value);
}
_this.setState({
selected: value
}, function () {
return onClick && onClick(evt, value);
});
});
_defineProperty(_assertThisInitialized(_this), "_addDividers", function (childrenNodes, disabled) {
var childrenNodesWithDividers = [];
var isTransparent = function isTransparent(childNode1, childNode2) {
return childNode1.props.selected !== childNode2.props.selected;
};
for (var i = 0; i < childrenNodes.length - 1; i++) {
var childNode1 = childrenNodes[i];
var childNode2 = childrenNodes[i + 1];
var transparent = isTransparent(childNode1, childNode2);
var divider = /*#__PURE__*/React.createElement("div", {
key: "divider-".concat(i),
className: st(classes.divider, {
disabled: disabled,
transparent: transparent
})
});
childrenNodesWithDividers.push(childNode1, divider);
}
var lastChild = childrenNodes[childrenNodes.length - 1];
childrenNodesWithDividers.push(lastChild);
return childrenNodesWithDividers;
});
return _this;
}
_createClass(SegmentedToggle, [{
key: "render",
value: function render() {
var _this2 = this;
var _this$props2 = this.props,
dataHook = _this$props2.dataHook,
children = _this$props2.children,
disabled = _this$props2.disabled,
defaultSelected = _this$props2.defaultSelected,
onClick = _this$props2.onClick,
selected = _this$props2.selected,
rest = _objectWithoutProperties(_this$props2, _excluded);
var selection = selected || this.state.selected;
var childrenNodes = React.Children.map(children, function (child, index) {
return /*#__PURE__*/React.cloneElement(child, {
disabled: disabled,
'data-click': "segmented-toggle-".concat(index + 1),
'aria-selected': child.props.value === selection,
onClick: _this2._onClick,
selected: child.props.value === selection
});
});
childrenNodes = this._addDividers(childrenNodes, disabled);
return /*#__PURE__*/React.createElement("div", _extends({}, rest, {
"data-hook": dataHook,
className: st(classes.root, {
disabled: disabled
})
}), childrenNodes);
}
}]);
return SegmentedToggle;
}(React.Component);
_defineProperty(SegmentedToggle, "displayName", 'SegmentedToggle');
_defineProperty(SegmentedToggle, "propTypes", {
/** Applies a data-hook HTML attribute that can be used in the tests */
dataHook: PropTypes.string,
/** Specifies the initially selected option */
defaultSelected: PropTypes.node,
/** Specifies whether an option is selected */
selected: PropTypes.node,
/** Defines a callback function which is called every time option is clicked. Returns a selected element and its value. */
onClick: PropTypes.func,
/** Specifies whether interactions are disabled. */
disabled: PropTypes.bool,
/** Accepts <SegmentedToggle.Icon/> or <SegmentedToggle.Button/> as child items to list down available options */
children: PropTypes.array.isRequired
});
_defineProperty(SegmentedToggle, "defaultProps", {
children: []
});
SegmentedToggle.Button = ToggleButton;
SegmentedToggle.Icon = ToggleIcon;
export default SegmentedToggle;