wix-style-react
Version:
163 lines (134 loc) • 6.39 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
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";
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 './CheckToggle.st.css';
import { dataHooks } from './constants';
import Tooltip from '../Tooltip';
import { TooltipCommonProps } from '../common/PropTypes/TooltipCommon';
import { withFocusable } from "wix-ui-core/dist/es/src/hocs/Focusable/FocusableHOC";
import ConfirmSmall from 'wix-ui-icons-common/ConfirmSmall';
import Confirm from 'wix-ui-icons-common/Confirm';
var icon = {
small: /*#__PURE__*/React.createElement(ConfirmSmall, null),
medium: /*#__PURE__*/React.createElement(Confirm, null)
};
/** CheckToggle */
var CheckToggle = /*#__PURE__*/function (_React$PureComponent) {
_inherits(CheckToggle, _React$PureComponent);
var _super = _createSuper(CheckToggle);
function CheckToggle() {
var _this;
_classCallCheck(this, CheckToggle);
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", {
checked: !!_this.props.checked
});
_defineProperty(_assertThisInitialized(_this), "_isControlled", function () {
return _this.props.hasOwnProperty('checked');
});
_defineProperty(_assertThisInitialized(_this), "_handleChange", function (changeEvent) {
var checked = _this.state.checked;
var onChange = _this.props.onChange;
_this.setState({
checked: !checked
}, function () {
if (onChange) onChange(changeEvent);
});
});
_defineProperty(_assertThisInitialized(_this), "_renderInput", function () {
var _ref = _this._isControlled() ? _this.props : _this.state,
checked = _ref.checked;
var _this$props = _this.props,
size = _this$props.size,
disabled = _this$props.disabled,
onChange = _this$props.onChange;
return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("input", {
type: "checkbox",
className: classes.input,
"data-hook": dataHooks.toggle,
checked: checked,
disabled: disabled,
onChange: _this._isControlled() ? onChange : _this._handleChange
}), /*#__PURE__*/React.createElement("span", {
className: classes.toggle
}, icon[size]));
});
_defineProperty(_assertThisInitialized(_this), "_renderTooltip", function () {
var _this$props2 = _this.props,
tooltipContent = _this$props2.tooltipContent,
tooltipProps = _this$props2.tooltipProps;
return /*#__PURE__*/React.createElement(Tooltip, _extends({
dataHook: dataHooks.tooltip,
content: tooltipContent
}, tooltipProps), _this._renderInput());
});
return _this;
}
_createClass(CheckToggle, [{
key: "render",
value: function render() {
var _ref2 = this._isControlled() ? this.props : this.state,
checked = _ref2.checked;
var _this$props3 = this.props,
dataHook = _this$props3.dataHook,
size = _this$props3.size,
skin = _this$props3.skin,
disabled = _this$props3.disabled,
tooltipContent = _this$props3.tooltipContent,
focusableOnFocus = _this$props3.focusableOnFocus,
focusableOnBlur = _this$props3.focusableOnBlur,
className = _this$props3.className;
return /*#__PURE__*/React.createElement("label", {
className: st(classes.root, {
checked: checked,
size: size,
skin: skin,
disabled: disabled
}, className),
"data-hook": dataHook,
onFocus: focusableOnFocus,
onBlur: focusableOnBlur
}, tooltipContent ? this._renderTooltip() : this._renderInput());
}
}]);
return CheckToggle;
}(React.PureComponent);
CheckToggle.displayName = 'CheckToggle';
CheckToggle.propTypes = {
/** Applies a data-hook HTML attribute that can be used in the tests */
dataHook: PropTypes.string,
/** Specifies a CSS class name to be appended to the component’s root element */
className: PropTypes.string,
/** Specifies whether a check is selected */
checked: PropTypes.bool,
/** Defines a callback function which is called every time the check value is changed */
onChange: PropTypes.func,
/** Specifies whether toggle interactions should be disabled */
disabled: PropTypes.bool,
/** Controls the size of the component */
size: PropTypes.oneOf(['small', 'medium']),
/** Controls the skin of the component */
skin: PropTypes.oneOf(['standard', 'success']),
/** Defines a message to be displayed in a tooltip. Tooltip is displayed on component hover. */
tooltipContent: PropTypes.node,
/** Allows to pass all common tooltip props. Check `<Tooltip/>` for a full API. */
tooltipProps: PropTypes.shape(TooltipCommonProps)
};
CheckToggle.defaultProps = {
disabled: false,
size: 'small',
skin: 'standard'
};
export default withFocusable(CheckToggle);