-
-
Notifications
You must be signed in to change notification settings - Fork 186
Expand file tree
/
Copy pathStepHandler.tsx
More file actions
110 lines (94 loc) · 2.93 KB
/
Copy pathStepHandler.tsx
File metadata and controls
110 lines (94 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* eslint-disable react/no-unknown-property */
import { raf } from '@rc-component/util';
import { clsx } from 'clsx';
import * as React from 'react';
/**
* When click and hold on a button - the speed of auto changing the value.
*/
const STEP_INTERVAL = 200;
/**
* When click and hold on a button - the delay before auto changing the value.
*/
const STEP_DELAY = 600;
export interface StepHandlerProps {
prefixCls: string;
action: 'up' | 'down';
children?: React.ReactNode;
disabled?: boolean;
className?: string;
style?: React.CSSProperties;
onStep: (up: boolean, emitter: 'handler' | 'keyboard' | 'wheel') => void;
}
export default function StepHandler({
prefixCls,
action,
children,
disabled,
className,
style,
onStep,
}: StepHandlerProps) {
// ======================== MISC ========================
const isUpAction = action === 'up';
// ======================== Step ========================
const stepTimeoutRef = React.useRef<any>(undefined);
const frameIds = React.useRef<number[]>([]);
const onStopStep = () => {
clearTimeout(stepTimeoutRef.current);
};
// We will interval update step when hold mouse down
const onStepMouseDown = (e: React.MouseEvent) => {
e.preventDefault();
onStopStep();
onStep(isUpAction, 'handler');
// Loop step for interval
function loopStep() {
onStep(isUpAction, 'handler');
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(
() => () => {
onStopStep();
frameIds.current.forEach((id) => {
raf.cancel(id);
});
},
[],
);
// ======================= Render =======================
const actionClassName = `${prefixCls}-action`;
const mergedClassName = clsx(
actionClassName,
`${actionClassName}-${action}`,
{
[`${actionClassName}-${action}-disabled`]: disabled,
},
className,
);
// 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.
const safeOnStopStep = () => frameIds.current.push(raf(onStopStep));
return (
<span
unselectable="on"
role="button"
onMouseUp={safeOnStopStep}
onMouseLeave={safeOnStopStep}
onMouseDown={(e) => {
onStepMouseDown(e);
}}
aria-label={isUpAction ? 'Increase Value' : 'Decrease Value'}
aria-disabled={disabled}
className={mergedClassName}
style={style}
>
{children || <span unselectable="on" className={`${prefixCls}-action-${action}-inner`} />}
</span>
);
}