82 lines
3.8 KiB
JavaScript
82 lines
3.8 KiB
JavaScript
|
"use strict";
|
||
|
|
||
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
||
|
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
|
||
|
Object.defineProperty(exports, "__esModule", {
|
||
|
value: true
|
||
|
});
|
||
|
exports.default = void 0;
|
||
|
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
||
|
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
||
|
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
||
|
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
|
||
|
var React = _interopRequireWildcard(require("react"));
|
||
|
var _classnames = _interopRequireDefault(require("classnames"));
|
||
|
var _useMergedState3 = _interopRequireDefault(require("rc-util/lib/hooks/useMergedState"));
|
||
|
var _KeyCode = _interopRequireDefault(require("rc-util/lib/KeyCode"));
|
||
|
var _excluded = ["prefixCls", "className", "checked", "defaultChecked", "disabled", "loadingIcon", "checkedChildren", "unCheckedChildren", "onClick", "onChange", "onKeyDown"];
|
||
|
var Switch = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
|
||
|
var _classNames;
|
||
|
var _ref$prefixCls = _ref.prefixCls,
|
||
|
prefixCls = _ref$prefixCls === void 0 ? 'rc-switch' : _ref$prefixCls,
|
||
|
className = _ref.className,
|
||
|
checked = _ref.checked,
|
||
|
defaultChecked = _ref.defaultChecked,
|
||
|
disabled = _ref.disabled,
|
||
|
loadingIcon = _ref.loadingIcon,
|
||
|
checkedChildren = _ref.checkedChildren,
|
||
|
unCheckedChildren = _ref.unCheckedChildren,
|
||
|
onClick = _ref.onClick,
|
||
|
onChange = _ref.onChange,
|
||
|
onKeyDown = _ref.onKeyDown,
|
||
|
restProps = (0, _objectWithoutProperties2.default)(_ref, _excluded);
|
||
|
var _useMergedState = (0, _useMergedState3.default)(false, {
|
||
|
value: checked,
|
||
|
defaultValue: defaultChecked
|
||
|
}),
|
||
|
_useMergedState2 = (0, _slicedToArray2.default)(_useMergedState, 2),
|
||
|
innerChecked = _useMergedState2[0],
|
||
|
setInnerChecked = _useMergedState2[1];
|
||
|
function triggerChange(newChecked, event) {
|
||
|
var mergedChecked = innerChecked;
|
||
|
if (!disabled) {
|
||
|
mergedChecked = newChecked;
|
||
|
setInnerChecked(mergedChecked);
|
||
|
onChange === null || onChange === void 0 ? void 0 : onChange(mergedChecked, event);
|
||
|
}
|
||
|
return mergedChecked;
|
||
|
}
|
||
|
function onInternalKeyDown(e) {
|
||
|
if (e.which === _KeyCode.default.LEFT) {
|
||
|
triggerChange(false, e);
|
||
|
} else if (e.which === _KeyCode.default.RIGHT) {
|
||
|
triggerChange(true, e);
|
||
|
}
|
||
|
onKeyDown === null || onKeyDown === void 0 ? void 0 : onKeyDown(e);
|
||
|
}
|
||
|
function onInternalClick(e) {
|
||
|
var ret = triggerChange(!innerChecked, e);
|
||
|
// [Legacy] trigger onClick with value
|
||
|
onClick === null || onClick === void 0 ? void 0 : onClick(ret, e);
|
||
|
}
|
||
|
var switchClassName = (0, _classnames.default)(prefixCls, className, (_classNames = {}, (0, _defineProperty2.default)(_classNames, "".concat(prefixCls, "-checked"), innerChecked), (0, _defineProperty2.default)(_classNames, "".concat(prefixCls, "-disabled"), disabled), _classNames));
|
||
|
return /*#__PURE__*/React.createElement("button", (0, _extends2.default)({}, restProps, {
|
||
|
type: "button",
|
||
|
role: "switch",
|
||
|
"aria-checked": innerChecked,
|
||
|
disabled: disabled,
|
||
|
className: switchClassName,
|
||
|
ref: ref,
|
||
|
onKeyDown: onInternalKeyDown,
|
||
|
onClick: onInternalClick
|
||
|
}), loadingIcon, /*#__PURE__*/React.createElement("span", {
|
||
|
className: "".concat(prefixCls, "-inner")
|
||
|
}, /*#__PURE__*/React.createElement("span", {
|
||
|
className: "".concat(prefixCls, "-inner-checked")
|
||
|
}, checkedChildren), /*#__PURE__*/React.createElement("span", {
|
||
|
className: "".concat(prefixCls, "-inner-unchecked")
|
||
|
}, unCheckedChildren)));
|
||
|
});
|
||
|
Switch.displayName = 'Switch';
|
||
|
var _default = Switch;
|
||
|
exports.default = _default;
|