Skip to content

Commit bc3a910

Browse files
刘欢claude
andcommitted
refactor: optimize disabled handle boundary calculation
- Simplify boundary calculation with for loop instead of map/filter chain - Unify disabled array handling in useDisabled hook to avoid repetitive checks - Remove duplicate disabledIsBoolean checks by converting upfront 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent fbb2822 commit bc3a910

2 files changed

Lines changed: 30 additions & 30 deletions

File tree

src/Slider.tsx

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -405,20 +405,24 @@ const Slider = React.forwardRef<SliderRef, SliderProps<number | number[]>>((prop
405405

406406
// Calculate boundaries from disabled handles (treat as fixed anchors)
407407
const pushDist = typeof mergedPush === 'number' ? mergedPush : 0;
408-
const minBound = Math.max(
409-
mergedMin,
410-
...rawValues
411-
.slice(0, valueIndex)
412-
.map((v, i) => (isHandleDisabled(i) ? v + pushDist : mergedMin))
413-
.filter((v) => v > mergedMin),
414-
);
415-
const maxBound = Math.min(
416-
mergedMax,
417-
...rawValues
418-
.slice(valueIndex + 1)
419-
.map((v, i) => (isHandleDisabled(i + valueIndex + 1) ? v - pushDist : mergedMax))
420-
.filter((v) => v < mergedMax),
421-
);
408+
let minBound = mergedMin;
409+
let maxBound = mergedMax;
410+
411+
// Find nearest disabled handle on the left as min boundary
412+
for (let i = valueIndex - 1; i >= 0; i -= 1) {
413+
if (isHandleDisabled(i)) {
414+
minBound = Math.max(minBound, rawValues[i] + pushDist);
415+
break;
416+
}
417+
}
418+
419+
// Find nearest disabled handle on the right as max boundary
420+
for (let i = valueIndex + 1; i < rawValues.length; i += 1) {
421+
if (isHandleDisabled(i)) {
422+
maxBound = Math.min(maxBound, rawValues[i] - pushDist);
423+
break;
424+
}
425+
}
422426

423427
cloneNextValues[valueIndex] = Math.max(minBound, Math.min(maxBound, newValue));
424428
focusIndex = valueIndex;

src/hooks/useDisabled.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,32 @@ const useDisabled = (
55
mergedValue?: number | number[],
66
): [boolean, (index: number) => boolean, boolean] => {
77

8-
const disabledIsArray = Array.isArray(rawDisabled);
9-
const disabledIsBoolean = typeof rawDisabled === 'boolean';
108
const values = React.useMemo(
119
() => (Array.isArray(mergedValue) ? mergedValue : [mergedValue]),
1210
[mergedValue],
1311
);
1412

15-
const disabled = React.useMemo(() => {
16-
if (disabledIsBoolean) {
17-
return rawDisabled;
13+
const disabledArray = React.useMemo(() => {
14+
if (typeof rawDisabled === 'boolean') {
15+
return values.map(() => rawDisabled);
1816
}
19-
return disabledIsArray ? values.every((_, index) => rawDisabled[index]) : false;
17+
return Array.isArray(rawDisabled) ? rawDisabled : values.map(() => false);
2018
}, [rawDisabled, mergedValue]);
2119

20+
const disabled = React.useMemo(() => {
21+
return values.every((_, index) => disabledArray[index]);
22+
}, [disabledArray, values]);
23+
2224
const isHandleDisabled = React.useCallback(
2325
(index: number): boolean => {
24-
if (disabledIsBoolean) {
25-
return rawDisabled;
26-
}
27-
return rawDisabled[index] ?? disabled;
26+
return disabledArray[index] ?? disabled;
2827
},
29-
[rawDisabled, disabled],
28+
[disabledArray, disabled],
3029
);
3130

3231
const hasDisabledHandle = React.useMemo(() => {
33-
if (disabledIsBoolean) {
34-
return rawDisabled;
35-
}
36-
return rawDisabled.some((d) => d);
37-
}, [rawDisabled]);
32+
return disabledArray.some((d) => d);
33+
}, [disabledArray]);
3834

3935
return [
4036
disabled,

0 commit comments

Comments
 (0)