73 lines
3.1 KiB
JavaScript
73 lines
3.1 KiB
JavaScript
|
import _extends from "@babel/runtime/helpers/esm/extends";
|
||
|
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
||
|
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
|
||
|
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
|
||
|
var _excluded = ["prefixCls", "className", "checked", "defaultChecked", "disabled", "loadingIcon", "checkedChildren", "unCheckedChildren", "onClick", "onChange", "onKeyDown"];
|
||
|
import * as React from 'react';
|
||
|
import classNames from 'classnames';
|
||
|
import useMergedState from "rc-util/es/hooks/useMergedState";
|
||
|
import KeyCode from "rc-util/es/KeyCode";
|
||
|
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 = _objectWithoutProperties(_ref, _excluded);
|
||
|
var _useMergedState = useMergedState(false, {
|
||
|
value: checked,
|
||
|
defaultValue: defaultChecked
|
||
|
}),
|
||
|
_useMergedState2 = _slicedToArray(_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.LEFT) {
|
||
|
triggerChange(false, e);
|
||
|
} else if (e.which === KeyCode.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 = classNames(prefixCls, className, (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-checked"), innerChecked), _defineProperty(_classNames, "".concat(prefixCls, "-disabled"), disabled), _classNames));
|
||
|
return /*#__PURE__*/React.createElement("button", _extends({}, 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';
|
||
|
export default Switch;
|