-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountControl.tsx
More file actions
247 lines (231 loc) 路 7.95 KB
/
Copy pathCountControl.tsx
File metadata and controls
247 lines (231 loc) 路 7.95 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
'use client';
import React, {useRef, useCallback, useEffect} from 'react';
import {Plus, Minus} from 'lucide-react';
interface CountControlProps {
label: string;
count: number;
onCountChange: (count: number) => void;
minValue?: number;
maxValue?: number;
minWarningMessage?: string;
maxWarningMessage?: string;
disabled?: boolean;
ariaLabel?: string;
}
export const CountControl: React.FC<CountControlProps> = ({
label,
count,
onCountChange,
minValue = 1,
maxValue = 10,
minWarningMessage = `Minimum ${minValue} required`,
maxWarningMessage = `Maximum ${maxValue} allowed`,
disabled = false,
ariaLabel,
}) => {
const intervalRef = useRef<NodeJS.Timeout | null>(null);
const timeoutRef = useRef<NodeJS.Timeout | null>(null);
const currentCountRef = useRef(count);
const isHoldingRef = useRef(false);
const [showMinWarning, setShowMinWarning] = React.useState(false);
const [showMaxWarning, setShowMaxWarning] = React.useState(false);
const [mountMinWarning, setMountMinWarning] = React.useState(false);
const [mountMaxWarning, setMountMaxWarning] = React.useState(false);
const hasShownMinWarningRef = useRef(false);
const hasShownMaxWarningRef = useRef(false);
const warningTimeoutRef = useRef<NodeJS.Timeout | null>(null);
const unmountTimeoutRef = useRef<NodeJS.Timeout | null>(null);
useEffect(() => {
currentCountRef.current = count;
}, [count]);
useEffect(() => {
if (warningTimeoutRef.current) {
clearTimeout(warningTimeoutRef.current);
}
if (unmountTimeoutRef.current) {
clearTimeout(unmountTimeoutRef.current);
}
if (count <= minValue && !hasShownMinWarningRef.current) {
hasShownMinWarningRef.current = true;
setMountMinWarning(true);
setTimeout(() => setShowMinWarning(true), 10);
warningTimeoutRef.current = setTimeout(() => {
setShowMinWarning(false);
unmountTimeoutRef.current = setTimeout(() => {
setMountMinWarning(false);
}, 500);
}, 3000);
} else if (count > minValue) {
hasShownMinWarningRef.current = false;
if (mountMinWarning) {
setShowMinWarning(false);
unmountTimeoutRef.current = setTimeout(() => {
setMountMinWarning(false);
}, 500);
}
}
if (count >= maxValue && !hasShownMaxWarningRef.current) {
hasShownMaxWarningRef.current = true;
setMountMaxWarning(true);
setTimeout(() => setShowMaxWarning(true), 10);
warningTimeoutRef.current = setTimeout(() => {
setShowMaxWarning(false);
unmountTimeoutRef.current = setTimeout(() => {
setMountMaxWarning(false);
}, 500);
}, 3000);
} else if (count < maxValue) {
hasShownMaxWarningRef.current = false;
if (mountMaxWarning) {
setShowMaxWarning(false);
unmountTimeoutRef.current = setTimeout(() => {
setMountMaxWarning(false);
}, 500);
}
}
return () => {
if (warningTimeoutRef.current) {
clearTimeout(warningTimeoutRef.current);
}
if (unmountTimeoutRef.current) {
clearTimeout(unmountTimeoutRef.current);
}
};
}, [count, mountMinWarning, mountMaxWarning, minValue, maxValue]);
const clearTimers = useCallback(() => {
if (intervalRef.current) {
clearInterval(intervalRef.current);
intervalRef.current = null;
}
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
timeoutRef.current = null;
}
}, []);
const handleClick =
(action: 'increment' | 'decrement') => (e: React.MouseEvent) => {
e.stopPropagation();
if (disabled) return;
if (!isHoldingRef.current) {
if (action === 'decrement' && count > minValue) {
onCountChange(count - 1);
} else if (action === 'increment' && count < maxValue) {
onCountChange(count + 1);
}
}
isHoldingRef.current = false;
};
const startAutoRepeat = useCallback(
(action: 'increment' | 'decrement') => {
timeoutRef.current = setTimeout(() => {
isHoldingRef.current = true;
intervalRef.current = setInterval(() => {
if (action === 'decrement') {
if (currentCountRef.current > minValue) {
onCountChange(currentCountRef.current - 1);
} else {
clearTimers();
}
} else {
if (currentCountRef.current < maxValue) {
onCountChange(currentCountRef.current + 1);
} else {
clearTimers();
}
}
}, 100);
}, 500);
},
[onCountChange, clearTimers, minValue, maxValue],
);
const handleMouseDown =
(action: 'increment' | 'decrement') =>
(e: React.MouseEvent | React.TouchEvent) => {
e.preventDefault();
if (disabled) return;
isHoldingRef.current = false;
if (
(action === 'increment' && count < maxValue) ||
(action === 'decrement' && count > minValue)
) {
startAutoRepeat(action);
}
};
const handleMouseUp = () => {
clearTimers();
};
const handleMouseLeave = () => {
clearTimers();
isHoldingRef.current = false;
};
const accessibleLabel = ariaLabel || label.toLowerCase();
return (
<div className="space-y-2">
<label className="text-sm font-medium text-gray-700 dark:text-gray-300">
{label}
</label>
<div className="flex items-center space-x-3">
<button
onClick={handleClick('decrement')}
onMouseDown={handleMouseDown('decrement')}
onMouseUp={handleMouseUp}
onMouseLeave={handleMouseLeave}
onTouchStart={handleMouseDown('decrement')}
onTouchEnd={handleMouseUp}
className="btn-control w-8 h-8 rounded-full flex items-center justify-center transition-all duration-200 disabled:opacity-50 disabled:cursor-not-allowed touch-manipulation hover:scale-105 hover:shadow-md"
disabled={disabled || count <= minValue}
title={`Decrease ${accessibleLabel} count (hold to repeat)`}
aria-label={`Decrease ${accessibleLabel} count`}
>
<Minus className="h-4 w-4" strokeWidth={2.5} />
</button>
<span
className="w-8 text-center text-lg font-semibold text-gray-900 dark:text-gray-100"
aria-label={`Current ${accessibleLabel} count: ${count}`}
>
{count}
</span>
<button
onClick={handleClick('increment')}
onMouseDown={handleMouseDown('increment')}
onMouseUp={handleMouseUp}
onMouseLeave={handleMouseLeave}
onTouchStart={handleMouseDown('increment')}
onTouchEnd={handleMouseUp}
className="btn-control w-8 h-8 rounded-full flex items-center justify-center transition-all duration-200 disabled:opacity-50 disabled:cursor-not-allowed touch-manipulation hover:scale-105 hover:shadow-md"
disabled={disabled || count >= maxValue}
title={`Increase ${accessibleLabel} count (hold to repeat)`}
aria-label={`Increase ${accessibleLabel} count`}
>
<Plus className="h-4 w-4" strokeWidth={2.5} />
</button>
</div>
<div
className={`overflow-hidden transition-all duration-500 ${
count <= minValue && mountMinWarning ? 'max-h-6 mt-1' : 'max-h-0 mt-0'
}`}
>
<p
className={`text-xs text-red-500 dark:text-red-400 transition-opacity duration-500 ${
showMinWarning ? 'opacity-100' : 'opacity-0'
}`}
>
{minWarningMessage}
</p>
</div>
<div
className={`overflow-hidden transition-all duration-500 ${
count >= maxValue && mountMaxWarning ? 'max-h-6 mt-1' : 'max-h-0 mt-0'
}`}
>
<p
className={`text-xs text-yellow-500 dark:text-yellow-400 transition-opacity duration-500 ${
showMaxWarning ? 'opacity-100' : 'opacity-0'
}`}
>
{maxWarningMessage}
</p>
</div>
</div>
);
};