Skip to content

Commit

Permalink
feat: [Carousel] support clickDragThreshold props
Browse files Browse the repository at this point in the history
  • Loading branch information
akai committed Jan 18, 2021
1 parent 353a6b9 commit 08b6916
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/components/Carousel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface CarouselProps {
className?: string;
startIndex?: number;
draggable?: boolean;
clickDragThreshold?: number;
duration?: number;
easing?: string;
threshold?: number;
Expand Down Expand Up @@ -54,6 +55,7 @@ export const Carousel = React.forwardRef<CarouselHandle, CarouselProps>((props,
duration = 300,
easing = 'ease',
threshold = 20,
clickDragThreshold = 10,
loop = true,
rtl = false,
autoPlay = props.autoplay || false,
Expand Down Expand Up @@ -88,6 +90,7 @@ export const Carousel = React.forwardRef<CarouselHandle, CarouselProps>((props,
);

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

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

if (!isDragging && Math.abs(dragOffset) > clickDragThreshold) {
setDragging(true);
}

// 阻尼
// if ((activeIndex === 0 && dragOffset > 0) || (activeIndex === count - 1 && dragOffset < 0)) {
// dragOffset *= 0.35;
Expand All @@ -310,6 +317,7 @@ export const Carousel = React.forwardRef<CarouselHandle, CarouselProps>((props,
e.stopPropagation();
const state = stateRef.current;
state.pressDown = false;
setDragging(false);
enableTransition();
if (state.endX) {
updateAfterDrag();
Expand Down Expand Up @@ -430,6 +438,7 @@ export const Carousel = React.forwardRef<CarouselHandle, CarouselProps>((props,
{
'Carousel--draggable': draggable,
'Carousel--rtl': rtl,
'Carousel--dragging': isDragging,
},
className,
)}
Expand Down
5 changes: 5 additions & 0 deletions src/components/Carousel/style.less
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
&--rtl {
direction: rtl;
}
&--dragging {
.Carousel-item {
pointer-events: none;
}
}
&-inner {
display: flex;
}
Expand Down
10 changes: 8 additions & 2 deletions storybook/stories/Carousel.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,19 @@ ManyImages.args = {
function TestMethods() {
const carouselRef = React.useRef(null);

function handleClick(e) {
console.log('click item:', e);
}

return (
<div>
<Carousel ref={carouselRef}>
{imgs.map((img, i) => (
<div key={i}>
<div key={i} onClick={handleClick}>
<p>{i}</p>
<img width="320" src={img} alt="" />
<a href="javascript:;">
<img width="320" src={img} alt="" />
</a>
</div>
))}
</Carousel>
Expand Down

0 comments on commit 08b6916

Please sign in to comment.