UNPKG

3.83 kBJavaScriptView Raw
1import _extends from "@babel/runtime/helpers/esm/extends";
2import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
3/* eslint-disable react/no-unknown-property */
4import * as React from 'react';
5import classNames from 'classnames';
6import useMobile from "rc-util/es/hooks/useMobile";
7import raf from "rc-util/es/raf";
8
9/**
10 * When click and hold on a button - the speed of auto changing the value.
11 */
12var STEP_INTERVAL = 200;
13
14/**
15 * When click and hold on a button - the delay before auto changing the value.
16 */
17var STEP_DELAY = 600;
18export default function StepHandler(_ref) {
19 var prefixCls = _ref.prefixCls,
20 upNode = _ref.upNode,
21 downNode = _ref.downNode,
22 upDisabled = _ref.upDisabled,
23 downDisabled = _ref.downDisabled,
24 onStep = _ref.onStep;
25 // ======================== Step ========================
26 var stepTimeoutRef = React.useRef();
27 var frameIds = React.useRef([]);
28 var onStepRef = React.useRef();
29 onStepRef.current = onStep;
30 var onStopStep = function onStopStep() {
31 clearTimeout(stepTimeoutRef.current);
32 };
33
34 // We will interval update step when hold mouse down
35 var onStepMouseDown = function onStepMouseDown(e, up) {
36 e.preventDefault();
37 onStopStep();
38 onStepRef.current(up);
39
40 // Loop step for interval
41 function loopStep() {
42 onStepRef.current(up);
43 stepTimeoutRef.current = setTimeout(loopStep, STEP_INTERVAL);
44 }
45
46 // First time press will wait some time to trigger loop step update
47 stepTimeoutRef.current = setTimeout(loopStep, STEP_DELAY);
48 };
49 React.useEffect(function () {
50 return function () {
51 onStopStep();
52 frameIds.current.forEach(function (id) {
53 return raf.cancel(id);
54 });
55 };
56 }, []);
57
58 // ======================= Render =======================
59 var isMobile = useMobile();
60 if (isMobile) {
61 return null;
62 }
63 var handlerClassName = "".concat(prefixCls, "-handler");
64 var upClassName = classNames(handlerClassName, "".concat(handlerClassName, "-up"), _defineProperty({}, "".concat(handlerClassName, "-up-disabled"), upDisabled));
65 var downClassName = classNames(handlerClassName, "".concat(handlerClassName, "-down"), _defineProperty({}, "".concat(handlerClassName, "-down-disabled"), downDisabled));
66
67 // fix: https://github.com/ant-design/ant-design/issues/43088
68 // In Safari, When we fire onmousedown and onmouseup events in quick succession,
69 // there may be a problem that the onmouseup events are executed first,
70 // resulting in a disordered program execution.
71 // So, we need to use requestAnimationFrame to ensure that the onmouseup event is executed after the onmousedown event.
72 var safeOnStopStep = function safeOnStopStep() {
73 return frameIds.current.push(raf(onStopStep));
74 };
75 var sharedHandlerProps = {
76 unselectable: 'on',
77 role: 'button',
78 onMouseUp: safeOnStopStep,
79 onMouseLeave: safeOnStopStep
80 };
81 return /*#__PURE__*/React.createElement("div", {
82 className: "".concat(handlerClassName, "-wrap")
83 }, /*#__PURE__*/React.createElement("span", _extends({}, sharedHandlerProps, {
84 onMouseDown: function onMouseDown(e) {
85 onStepMouseDown(e, true);
86 },
87 "aria-label": "Increase Value",
88 "aria-disabled": upDisabled,
89 className: upClassName
90 }), upNode || /*#__PURE__*/React.createElement("span", {
91 unselectable: "on",
92 className: "".concat(prefixCls, "-handler-up-inner")
93 })), /*#__PURE__*/React.createElement("span", _extends({}, sharedHandlerProps, {
94 onMouseDown: function onMouseDown(e) {
95 onStepMouseDown(e, false);
96 },
97 "aria-label": "Decrease Value",
98 "aria-disabled": downDisabled,
99 className: downClassName
100 }), downNode || /*#__PURE__*/React.createElement("span", {
101 unselectable: "on",
102 className: "".concat(prefixCls, "-handler-down-inner")
103 })));
104}
\No newline at end of file