Skip to content

Commit

Permalink
fix(swiper): 异步加载 indicator 不显示 (#2167)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-huxiyang authored Apr 15, 2024
1 parent 0217d34 commit ed27a5f
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/packages/swiper/swiper.taro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import React, {
ReactNode,
useEffect,
useImperativeHandle,
useRef,
useMemo,
useState,
} from 'react'
import {
Expand Down Expand Up @@ -61,13 +61,19 @@ export const Swiper = forwardRef((props: Partial<SwiperProps>, ref) => {
...props,
}
const [current, setCurrent] = useState(defaultValue)
const childrenCount = useRef(Children.toArray(children).length)
const childrenCount = useMemo(() => {
let c = 0
React.Children.map(children, (child, index) => {
c += 1
})
return c
}, [children])
useEffect(() => {
setCurrent(defaultValue)
}, [defaultValue])
const renderIndicator = () => {
if (React.isValidElement(indicator)) return indicator
if (indicator === true) {
if (indicator) {
return (
<div
className={classNames({
Expand All @@ -77,7 +83,7 @@ export const Swiper = forwardRef((props: Partial<SwiperProps>, ref) => {
>
<Indicator
current={current}
total={childrenCount.current}
total={childrenCount}
direction={direction}
/>
</div>
Expand All @@ -96,16 +102,16 @@ export const Swiper = forwardRef((props: Partial<SwiperProps>, ref) => {
},
next: () => {
if (loop) {
setCurrent((current + 1) % childrenCount.current)
setCurrent((current + 1) % childrenCount)
} else {
setCurrent(current + 1 >= childrenCount.current ? current : current + 1)
setCurrent(current + 1 >= childrenCount ? current : current + 1)
}
},
prev: () => {
if (loop) {
let next = current - 1
next = next < 0 ? childrenCount.current + next : next
setCurrent(next % childrenCount.current)
next = next < 0 ? childrenCount + next : next
setCurrent(next % childrenCount)
} else {
setCurrent(current - 1 <= 0 ? 0 : current - 1)
}
Expand Down

0 comments on commit ed27a5f

Please sign in to comment.