Skip to content

Commit 08b6916

Browse files
committed
feat: [Carousel] support clickDragThreshold props
1 parent 353a6b9 commit 08b6916

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

src/components/Carousel/index.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export interface CarouselProps {
77
className?: string;
88
startIndex?: number;
99
draggable?: boolean;
10+
clickDragThreshold?: number;
1011
duration?: number;
1112
easing?: string;
1213
threshold?: number;
@@ -54,6 +55,7 @@ export const Carousel = React.forwardRef<CarouselHandle, CarouselProps>((props,
5455
duration = 300,
5556
easing = 'ease',
5657
threshold = 20,
58+
clickDragThreshold = 10,
5759
loop = true,
5860
rtl = false,
5961
autoPlay = props.autoplay || false,
@@ -88,6 +90,7 @@ export const Carousel = React.forwardRef<CarouselHandle, CarouselProps>((props,
8890
);
8991

9092
const [activeIndex, setActiveIndex] = useState(getIndex(startIndex));
93+
const [isDragging, setDragging] = useState(false);
9194

9295
const enableTransition = useCallback(() => {
9396
innerRef.current.style.transition = `transform ${duration}ms ${easing}`;
@@ -296,6 +299,10 @@ export const Carousel = React.forwardRef<CarouselHandle, CarouselProps>((props,
296299
const nextOffset = nextIndex * state.wrapWidth;
297300
const dragOffset = state.endX - state.startX;
298301

302+
if (!isDragging && Math.abs(dragOffset) > clickDragThreshold) {
303+
setDragging(true);
304+
}
305+
299306
// 阻尼
300307
// if ((activeIndex === 0 && dragOffset > 0) || (activeIndex === count - 1 && dragOffset < 0)) {
301308
// dragOffset *= 0.35;
@@ -310,6 +317,7 @@ export const Carousel = React.forwardRef<CarouselHandle, CarouselProps>((props,
310317
e.stopPropagation();
311318
const state = stateRef.current;
312319
state.pressDown = false;
320+
setDragging(false);
313321
enableTransition();
314322
if (state.endX) {
315323
updateAfterDrag();
@@ -430,6 +438,7 @@ export const Carousel = React.forwardRef<CarouselHandle, CarouselProps>((props,
430438
{
431439
'Carousel--draggable': draggable,
432440
'Carousel--rtl': rtl,
441+
'Carousel--dragging': isDragging,
433442
},
434443
className,
435444
)}

src/components/Carousel/style.less

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
&--rtl {
1515
direction: rtl;
1616
}
17+
&--dragging {
18+
.Carousel-item {
19+
pointer-events: none;
20+
}
21+
}
1722
&-inner {
1823
display: flex;
1924
}

storybook/stories/Carousel.stories.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,19 @@ ManyImages.args = {
4444
function TestMethods() {
4545
const carouselRef = React.useRef(null);
4646

47+
function handleClick(e) {
48+
console.log('click item:', e);
49+
}
50+
4751
return (
4852
<div>
4953
<Carousel ref={carouselRef}>
5054
{imgs.map((img, i) => (
51-
<div key={i}>
55+
<div key={i} onClick={handleClick}>
5256
<p>{i}</p>
53-
<img width="320" src={img} alt="" />
57+
<a href="javascript:;">
58+
<img width="320" src={img} alt="" />
59+
</a>
5460
</div>
5561
))}
5662
</Carousel>

0 commit comments

Comments
 (0)