Skip to content

Commit ee3ccc1

Browse files
committed
feat(swiper): add defaultCurrent prop
1 parent 6c8abe9 commit ee3ccc1

File tree

4 files changed

+9
-7
lines changed

4 files changed

+9
-7
lines changed

src/swiper/Swiper.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ const Swiper = forwardRefWithStatics(
6868
const rootDiv = useRef<HTMLDivElement>(null); // 根节点
6969
const swiperContainer = useRef<HTMLDivElement>(null); // swiper容器节点
7070
const swiperSource = useRef<SwiperChangeSource>('autoplay'); // swiper变化来源
71-
const previousIndex = useRef(current || defaultCurrent || 0); // 上一次轮播页索引
71+
const previousIndex = useRef<number>(current ?? defaultCurrent ?? 0); // 上一次轮播页索引
7272
const nextIndex = useRef(previousIndex.current);
7373
const items = useRef<SwiperItemReference[]>([]); // swiper子项
7474
const [itemCount, setItemCount] = useState(0); // 轮播子项数量
@@ -417,7 +417,8 @@ const Swiper = forwardRefWithStatics(
417417
});
418418

419419
useEffect(() => {
420-
// 初始化卡片的位置
420+
if (!items.current.length) return;
421+
421422
previousIndex.current = calculateItemIndex(previousIndex.current, items.current.length, loop);
422423
updateSwiperItemClassName(previousIndex.current, loop);
423424
setDotIndex(() => previousIndex.current);
@@ -426,11 +427,12 @@ const Swiper = forwardRefWithStatics(
426427

427428
useEffect(() => {
428429
if (currentIsNull) return;
430+
if (!items.current.length) return;
429431
nextIndex.current = calculateItemIndex(current, items.current.length, loop);
430432
if (previousIndex.current !== nextIndex.current) {
431433
enterSwitching(directionAxis);
432434
}
433-
}, [calculateItemIndex, current, directionAxis, enterSwitching, loop, currentIsNull]);
435+
}, [calculateItemIndex, current, directionAxis, enterSwitching, loop, currentIsNull, itemCount]);
434436

435437
useEffect(() => {
436438
onChange?.(previousIndex.current, { source: swiperSource.current });

src/swiper/defaultProps.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { TdSwiperProps } from './type';
77
export const swiperDefaultProps: TdSwiperProps = {
88
animation: 'slide',
99
autoplay: true,
10-
current: 0,
10+
defaultCurrent: 0,
1111
direction: 'horizontal',
1212
duration: 300,
1313
interval: 5000,

src/swiper/swiper.en-US.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
:: BASE_DOC ::
22

33
## API
4-
54
### Swiper Props
65

76
name | type | default | description | required
87
-- | -- | -- | -- | --
98
className | String | - | className of component | N
10-
style | Object | - | CSS(Cascading Style Sheets),Typescript`React.CSSProperties` | N
9+
style | Object | - | CSS(Cascading Style Sheets),Typescript: `React.CSSProperties` | N
1110
animation | String | slide | options: slide | N
1211
autoplay | Boolean | true | \- | N
1312
current | Number | 0 | \- | N
13+
defaultCurrent | Number | 0 | uncontrolled property | N
1414
direction | String | horizontal | options: horizontal/vertical | N
1515
duration | Number | 300 | \- | N
1616
height | String / Number | - | \- | N

src/swiper/swiper.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
:: BASE_DOC ::
22

33
## API
4-
54
### Swiper Props
65

76
名称 | 类型 | 默认值 | 描述 | 必传
@@ -11,6 +10,7 @@ style | Object | - | 样式,TS 类型:`React.CSSProperties` | N
1110
animation | String | slide | 轮播切换动画效果类型。可选项:slide | N
1211
autoplay | Boolean | true | 是否自动播放 | N
1312
current | Number | 0 | 当前轮播在哪一项(下标) | N
13+
defaultCurrent | Number | 0 | 当前轮播在哪一项(下标)。非受控属性 | N
1414
direction | String | horizontal | 轮播滑动方向,包括横向滑动和纵向滑动两个方向。可选项:horizontal/vertical | N
1515
duration | Number | 300 | 滑动动画时长 | N
1616
height | String / Number | - | 当使用垂直方向滚动时的高度 | N

0 commit comments

Comments
 (0)