Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add scrollReverse props, autoplay reverse #6589

Merged
merged 3 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/components/swiper/swiper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export type SwiperProps = {
defaultIndex?: number
allowTouchMove?: boolean
autoplay?: boolean
scrollReverse?: boolean
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感觉可以给 autoplay 加个 reverse:

autoplay?: boolean | 'reverse';

autoplayInterval?: number
loop?: boolean
direction?: 'horizontal' | 'vertical'
Expand Down Expand Up @@ -293,11 +294,15 @@ export const Swiper = forwardRef<SwiperRef, SwiperProps>(
}
})

const { autoplay, autoplayInterval } = props
const { autoplay, autoplayInterval, scrollReverse } = props

const runTimeSwiper = () => {
timeoutRef.current = window.setTimeout(() => {
swipeNext()
if (scrollReverse) {
swipePrev()
} else {
swipeNext()
}
runTimeSwiper()
}, autoplayInterval)
}
Expand Down
17 changes: 17 additions & 0 deletions src/components/swiper/tests/swiper.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,23 @@ describe('Swiper', () => {
jest.useRealTimers()
})

test('auto play reverse', () => {
jest.useFakeTimers()
render(
<Swiper autoplay scrollReverse>
{items}
</Swiper>
)

// trigger once
act(() => {
jest.runOnlyPendingTimers()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个默认就是第一个,即便跑了时间,其实还是第一个,这样是测不出来的。需要切换到后面,然后再跑时间

})

expect($$(`.${classPrefix}-track-inner`)[0]).toHaveStyle('transform: none')
jest.useRealTimers()
})

test('loop', async () => {
const { container } = render(
<Swiper loop defaultIndex={3}>
Expand Down
Loading