amis-rpc-design/node_modules/rc-input-number/lib/StepHandler.js

114 lines
5.4 KiB
JavaScript
Raw Normal View History

2023-10-07 19:42:30 +08:00
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = StepHandler;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var React = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _useMobile = _interopRequireDefault(require("rc-util/lib/hooks/useMobile"));
var _raf = _interopRequireDefault(require("rc-util/lib/raf"));
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
/* eslint-disable react/no-unknown-property */
/**
* When click and hold on a button - the speed of auto changing the value.
*/
var STEP_INTERVAL = 200;
/**
* When click and hold on a button - the delay before auto changing the value.
*/
var STEP_DELAY = 600;
function StepHandler(_ref) {
var prefixCls = _ref.prefixCls,
upNode = _ref.upNode,
downNode = _ref.downNode,
upDisabled = _ref.upDisabled,
downDisabled = _ref.downDisabled,
onStep = _ref.onStep;
// ======================== Step ========================
var stepTimeoutRef = React.useRef();
var frameIds = React.useRef([]);
var onStepRef = React.useRef();
onStepRef.current = onStep;
var onStopStep = function onStopStep() {
clearTimeout(stepTimeoutRef.current);
};
// We will interval update step when hold mouse down
var onStepMouseDown = function onStepMouseDown(e, up) {
e.preventDefault();
onStopStep();
onStepRef.current(up);
// Loop step for interval
function loopStep() {
onStepRef.current(up);
stepTimeoutRef.current = setTimeout(loopStep, STEP_INTERVAL);
}
// First time press will wait some time to trigger loop step update
stepTimeoutRef.current = setTimeout(loopStep, STEP_DELAY);
};
React.useEffect(function () {
return function () {
onStopStep();
frameIds.current.forEach(function (id) {
return _raf.default.cancel(id);
});
};
}, []);
// ======================= Render =======================
var isMobile = (0, _useMobile.default)();
if (isMobile) {
return null;
}
var handlerClassName = "".concat(prefixCls, "-handler");
var upClassName = (0, _classnames.default)(handlerClassName, "".concat(handlerClassName, "-up"), (0, _defineProperty2.default)({}, "".concat(handlerClassName, "-up-disabled"), upDisabled));
var downClassName = (0, _classnames.default)(handlerClassName, "".concat(handlerClassName, "-down"), (0, _defineProperty2.default)({}, "".concat(handlerClassName, "-down-disabled"), downDisabled));
// fix: https://github.com/ant-design/ant-design/issues/43088
// In Safari, When we fire onmousedown and onmouseup events in quick succession,
// there may be a problem that the onmouseup events are executed first,
// resulting in a disordered program execution.
// So, we need to use requestAnimationFrame to ensure that the onmouseup event is executed after the onmousedown event.
var safeOnStopStep = function safeOnStopStep() {
return frameIds.current.push((0, _raf.default)(onStopStep));
};
var sharedHandlerProps = {
unselectable: 'on',
role: 'button',
onMouseUp: safeOnStopStep,
onMouseLeave: safeOnStopStep
};
return /*#__PURE__*/React.createElement("div", {
className: "".concat(handlerClassName, "-wrap")
}, /*#__PURE__*/React.createElement("span", (0, _extends2.default)({}, sharedHandlerProps, {
onMouseDown: function onMouseDown(e) {
onStepMouseDown(e, true);
},
"aria-label": "Increase Value",
"aria-disabled": upDisabled,
className: upClassName
}), upNode || /*#__PURE__*/React.createElement("span", {
unselectable: "on",
className: "".concat(prefixCls, "-handler-up-inner")
})), /*#__PURE__*/React.createElement("span", (0, _extends2.default)({}, sharedHandlerProps, {
onMouseDown: function onMouseDown(e) {
onStepMouseDown(e, false);
},
"aria-label": "Decrease Value",
"aria-disabled": downDisabled,
className: downClassName
}), downNode || /*#__PURE__*/React.createElement("span", {
unselectable: "on",
className: "".concat(prefixCls, "-handler-down-inner")
})));
}