@fjyueke/arco-mcp 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +85 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +244 -0
- package/metaData/components/action-sheet/doc.md +102 -0
- package/metaData/components/action-sheet/example/index.tsx +199 -0
- package/metaData/components/action-sheet/example/methods.tsx +6 -0
- package/metaData/components/avatar/doc.md +57 -0
- package/metaData/components/avatar/example/group.tsx +69 -0
- package/metaData/components/avatar/example/index.tsx +196 -0
- package/metaData/components/badge/doc.md +28 -0
- package/metaData/components/badge/example/index.tsx +128 -0
- package/metaData/components/button/doc.md +45 -0
- package/metaData/components/button/example/index.tsx +264 -0
- package/metaData/components/carousel/doc.md +89 -0
- package/metaData/components/carousel/example/index.tsx +1481 -0
- package/metaData/components/cell/doc.md +68 -0
- package/metaData/components/cell/example/arrow.tsx +13 -0
- package/metaData/components/cell/example/cell.tsx +71 -0
- package/metaData/components/cell/example/group.tsx +63 -0
- package/metaData/components/cell/example/index.tsx +19 -0
- package/metaData/components/checkbox/doc.md +91 -0
- package/metaData/components/checkbox/example/checkbox.tsx +158 -0
- package/metaData/components/checkbox/example/group.tsx +103 -0
- package/metaData/components/checkbox/example/index.tsx +25 -0
- package/metaData/components/circle-progress/doc.md +40 -0
- package/metaData/components/circle-progress/example/index.tsx +323 -0
- package/metaData/components/collapse/doc.md +74 -0
- package/metaData/components/collapse/example/collapse.tsx +183 -0
- package/metaData/components/collapse/example/group.tsx +136 -0
- package/metaData/components/collapse/example/index.tsx +16 -0
- package/metaData/components/collapse/example/utils.tsx +20 -0
- package/metaData/components/context-provider/doc.md +44 -0
- package/metaData/components/context-provider/example/index.tsx +208 -0
- package/metaData/components/count-down/doc.md +40 -0
- package/metaData/components/count-down/example/hooks.tsx +118 -0
- package/metaData/components/count-down/example/index.tsx +63 -0
- package/metaData/components/date-picker/doc.md +102 -0
- package/metaData/components/date-picker/example/index.tsx +545 -0
- package/metaData/components/dialog/doc.md +200 -0
- package/metaData/components/dialog/example/index.tsx +328 -0
- package/metaData/components/dialog/example/methods.tsx +56 -0
- package/metaData/components/divider/doc.md +25 -0
- package/metaData/components/divider/example/index.tsx +87 -0
- package/metaData/components/dropdown/doc.md +80 -0
- package/metaData/components/dropdown/example/dropdown.tsx +308 -0
- package/metaData/components/dropdown/example/index.tsx +19 -0
- package/metaData/components/dropdown/example/options.tsx +103 -0
- package/metaData/components/dropdown-menu/doc.md +91 -0
- package/metaData/components/dropdown-menu/example/dropdown-menu.tsx +270 -0
- package/metaData/components/dropdown-menu/example/index.tsx +16 -0
- package/metaData/components/ellipsis/doc.md +33 -0
- package/metaData/components/ellipsis/example/index.tsx +93 -0
- package/metaData/components/ellipsis/example/js-ellipsis.tsx +229 -0
- package/metaData/components/ellipsis/example/native-ellipsis.tsx +48 -0
- package/metaData/components/form/doc.md +90 -0
- package/metaData/components/form/example/form-item-context.tsx +8 -0
- package/metaData/components/form/example/form-item.tsx +378 -0
- package/metaData/components/form/example/index.tsx +87 -0
- package/metaData/components/form/example/linked-container.tsx +66 -0
- package/metaData/components/grid/doc.md +39 -0
- package/metaData/components/grid/example/index.tsx +252 -0
- package/metaData/components/icon/doc.md +15 -0
- package/metaData/components/icon/example/index.tsx +32 -0
- package/metaData/components/image/doc.md +62 -0
- package/metaData/components/image/example/index.tsx +528 -0
- package/metaData/components/image-picker/doc.md +116 -0
- package/metaData/components/image-picker/example/add-icon.tsx +14 -0
- package/metaData/components/image-picker/example/index.tsx +175 -0
- package/metaData/components/image-preview/doc.md +119 -0
- package/metaData/components/image-preview/example/index.tsx +1290 -0
- package/metaData/components/image-preview/example/methods.tsx +72 -0
- package/metaData/components/index-bar/doc.md +83 -0
- package/metaData/components/index-bar/example/group.tsx +93 -0
- package/metaData/components/index-bar/example/index.tsx +271 -0
- package/metaData/components/index-bar/example/side-bar.tsx +82 -0
- package/metaData/components/input/doc.md +60 -0
- package/metaData/components/input/example/hooks.tsx +259 -0
- package/metaData/components/input/example/index.tsx +134 -0
- package/metaData/components/input/example/props.tsx +183 -0
- package/metaData/components/keyboard/doc.md +73 -0
- package/metaData/components/keyboard/example/index.tsx +238 -0
- package/metaData/components/load-more/doc.md +48 -0
- package/metaData/components/load-more/example/index.tsx +395 -0
- package/metaData/components/loading/doc.md +34 -0
- package/metaData/components/loading/example/index.tsx +312 -0
- package/metaData/components/masking/doc.md +93 -0
- package/metaData/components/masking/example/index.tsx +410 -0
- package/metaData/components/masking/example/methods.tsx +88 -0
- package/metaData/components/nav-bar/doc.md +40 -0
- package/metaData/components/nav-bar/example/back-icon.tsx +28 -0
- package/metaData/components/nav-bar/example/index.tsx +305 -0
- package/metaData/components/notice-bar/doc.md +34 -0
- package/metaData/components/notice-bar/example/index.tsx +294 -0
- package/metaData/components/notify/doc.md +81 -0
- package/metaData/components/notify/example/index.tsx +171 -0
- package/metaData/components/notify/example/methods.tsx +83 -0
- package/metaData/components/pagination/doc.md +41 -0
- package/metaData/components/pagination/example/arrow.tsx +14 -0
- package/metaData/components/pagination/example/index.tsx +292 -0
- package/metaData/components/picker/doc.md +89 -0
- package/metaData/components/picker/example/index.tsx +217 -0
- package/metaData/components/picker-view/doc.md +57 -0
- package/metaData/components/picker-view/example/cascader.tsx +141 -0
- package/metaData/components/picker-view/example/index.tsx +297 -0
- package/metaData/components/picker-view/example/multi-picker.tsx +66 -0
- package/metaData/components/picker-view/example/picker-cell.tsx +416 -0
- package/metaData/components/popover/doc.md +144 -0
- package/metaData/components/popover/example/index.tsx +23 -0
- package/metaData/components/popover/example/menu.tsx +145 -0
- package/metaData/components/popover/example/popover-inner.tsx +147 -0
- package/metaData/components/popover/example/popover.tsx +268 -0
- package/metaData/components/popup/doc.md +101 -0
- package/metaData/components/popup/example/index.tsx +125 -0
- package/metaData/components/popup/example/methods.tsx +6 -0
- package/metaData/components/popup-swiper/doc.md +108 -0
- package/metaData/components/popup-swiper/example/index.tsx +332 -0
- package/metaData/components/popup-swiper/example/methods.tsx +6 -0
- package/metaData/components/portal/doc.md +14 -0
- package/metaData/components/portal/example/index.tsx +35 -0
- package/metaData/components/progress/doc.md +42 -0
- package/metaData/components/progress/example/index.tsx +256 -0
- package/metaData/components/pull-refresh/doc.md +37 -0
- package/metaData/components/pull-refresh/example/android-pull-refresh.tsx +260 -0
- package/metaData/components/pull-refresh/example/index.tsx +44 -0
- package/metaData/components/pull-refresh/example/ios-pull-refresh.tsx +216 -0
- package/metaData/components/radio/doc.md +89 -0
- package/metaData/components/radio/example/group.tsx +92 -0
- package/metaData/components/radio/example/index.tsx +25 -0
- package/metaData/components/radio/example/radio.tsx +22 -0
- package/metaData/components/rate/doc.md +33 -0
- package/metaData/components/rate/example/index.tsx +281 -0
- package/metaData/components/search-bar/doc.md +97 -0
- package/metaData/components/search-bar/example/association.tsx +89 -0
- package/metaData/components/search-bar/example/cancel-button.tsx +34 -0
- package/metaData/components/search-bar/example/highlight.tsx +66 -0
- package/metaData/components/search-bar/example/index.tsx +251 -0
- package/metaData/components/show-monitor/doc.md +34 -0
- package/metaData/components/show-monitor/example/index.tsx +646 -0
- package/metaData/components/skeleton/doc.md +101 -0
- package/metaData/components/skeleton/example/elements.tsx +290 -0
- package/metaData/components/skeleton/example/index.tsx +96 -0
- package/metaData/components/skeleton/example/skeleton-context.tsx +7 -0
- package/metaData/components/slider/doc.md +42 -0
- package/metaData/components/slider/example/index.tsx +326 -0
- package/metaData/components/slider/example/marks.tsx +50 -0
- package/metaData/components/slider/example/popover.tsx +38 -0
- package/metaData/components/slider/example/thumb.tsx +48 -0
- package/metaData/components/slider/example/useSliderIcon.tsx +38 -0
- package/metaData/components/slider/example/useSliderInit.tsx +170 -0
- package/metaData/components/stepper/doc.md +46 -0
- package/metaData/components/stepper/example/index.tsx +166 -0
- package/metaData/components/stepper/example/useButtonClick.tsx +74 -0
- package/metaData/components/stepper/example/useInputEvent.tsx +65 -0
- package/metaData/components/stepper/example/useValue.tsx +43 -0
- package/metaData/components/steps/doc.md +57 -0
- package/metaData/components/steps/example/index.tsx +123 -0
- package/metaData/components/steps/example/step.tsx +126 -0
- package/metaData/components/sticky/doc.md +45 -0
- package/metaData/components/sticky/example/index.tsx +430 -0
- package/metaData/components/swipe-action/doc.md +44 -0
- package/metaData/components/swipe-action/example/index.tsx +338 -0
- package/metaData/components/swipe-action/example/item.tsx +40 -0
- package/metaData/components/swipe-load/doc.md +43 -0
- package/metaData/components/swipe-load/example/index.tsx +339 -0
- package/metaData/components/swipe-load/example/type.tsx +153 -0
- package/metaData/components/switch/doc.md +38 -0
- package/metaData/components/switch/example/index.tsx +237 -0
- package/metaData/components/tab-bar/doc.md +57 -0
- package/metaData/components/tab-bar/example/index.tsx +18 -0
- package/metaData/components/tab-bar/example/item.tsx +74 -0
- package/metaData/components/tab-bar/example/tab-bar.tsx +105 -0
- package/metaData/components/tabs/doc.md +187 -0
- package/metaData/components/tabs/example/index.tsx +514 -0
- package/metaData/components/tabs/example/tab-cell-underline.tsx +320 -0
- package/metaData/components/tabs/example/tab-cell.tsx +433 -0
- package/metaData/components/tabs/example/tab-pane.tsx +349 -0
- package/metaData/components/tag/doc.md +80 -0
- package/metaData/components/tag/example/index.tsx +23 -0
- package/metaData/components/tag/example/list.tsx +97 -0
- package/metaData/components/tag/example/tag.tsx +85 -0
- package/metaData/components/textarea/doc.md +67 -0
- package/metaData/components/textarea/example/index.tsx +258 -0
- package/metaData/components/toast/doc.md +90 -0
- package/metaData/components/toast/example/index.tsx +335 -0
- package/metaData/components/toast/example/methods.tsx +87 -0
- package/metaData/components/transition/doc.md +19 -0
- package/metaData/components/transition/example/index.tsx +113 -0
- package/metaData/components/uploader/doc.md +82 -0
- package/metaData/components/uploader/example/index.tsx +208 -0
- package/metaData/components-index.json +242 -0
- package/package.json +56 -0
|
@@ -0,0 +1,1481 @@
|
|
|
1
|
+
import React, {
|
|
2
|
+
useRef,
|
|
3
|
+
useState,
|
|
4
|
+
useEffect,
|
|
5
|
+
forwardRef,
|
|
6
|
+
Ref,
|
|
7
|
+
useImperativeHandle,
|
|
8
|
+
useCallback,
|
|
9
|
+
CSSProperties,
|
|
10
|
+
ReactNode,
|
|
11
|
+
useMemo,
|
|
12
|
+
useContext,
|
|
13
|
+
} from 'react';
|
|
14
|
+
import { nextTick, cls } from '@arco-design/mobile-utils';
|
|
15
|
+
import { ContextLayout, GlobalContext } from '../context-provider';
|
|
16
|
+
import {
|
|
17
|
+
useRefState,
|
|
18
|
+
useListenResize,
|
|
19
|
+
useUpdateEffect,
|
|
20
|
+
getStyleWithVendor,
|
|
21
|
+
useSystem,
|
|
22
|
+
useSwiperInnerScroll,
|
|
23
|
+
useLatestRef,
|
|
24
|
+
} from '../_helpers';
|
|
25
|
+
|
|
26
|
+
export interface CarouselListItem {
|
|
27
|
+
/**
|
|
28
|
+
* 图片链接
|
|
29
|
+
* @en image resource
|
|
30
|
+
*/
|
|
31
|
+
src: string;
|
|
32
|
+
/**
|
|
33
|
+
* 图片底部固定的文字
|
|
34
|
+
* @en Fixed text at the bottom of the image
|
|
35
|
+
*/
|
|
36
|
+
text?: ReactNode;
|
|
37
|
+
/**
|
|
38
|
+
* 点击图片回调
|
|
39
|
+
* @en Callback function for clicking the image
|
|
40
|
+
*/
|
|
41
|
+
onClick?: (e: React.MouseEvent<HTMLImageElement, MouseEvent>) => void;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface CarouselProps {
|
|
45
|
+
/**
|
|
46
|
+
* 样式类名
|
|
47
|
+
* @en Custom classname
|
|
48
|
+
*/
|
|
49
|
+
className?: string;
|
|
50
|
+
/**
|
|
51
|
+
* 最外层容器自定义样式
|
|
52
|
+
* @en Outermost container custom stylesheet
|
|
53
|
+
*/
|
|
54
|
+
wrapStyle?: CSSProperties;
|
|
55
|
+
/**
|
|
56
|
+
* 内层轮播容器自定义样式
|
|
57
|
+
* @en Inner carousel container custom stylesheet
|
|
58
|
+
*/
|
|
59
|
+
style?: CSSProperties;
|
|
60
|
+
/**
|
|
61
|
+
* 轮播内部内容
|
|
62
|
+
* @en Carousel internal content
|
|
63
|
+
*/
|
|
64
|
+
children?: ReactNode;
|
|
65
|
+
/**
|
|
66
|
+
* 轮播图片列表,省略子元素的简写,传入图片url数组,也可通过`text`指定图片底部的蒙层文字内容
|
|
67
|
+
* @en Carousel image list, omit the shorthand of children elements, input the image url array, or specify the content of the masked text at the bottom of the image through `text`
|
|
68
|
+
*/
|
|
69
|
+
list?: CarouselListItem[];
|
|
70
|
+
/**
|
|
71
|
+
* 自动切换轮播滑块时,动画的执行时间(ms)
|
|
72
|
+
* @en Animation duration(ms) when the carousel slider is automatically toggled,
|
|
73
|
+
* @default 500
|
|
74
|
+
*/
|
|
75
|
+
animateDuration?: number;
|
|
76
|
+
/**
|
|
77
|
+
* 手动切换轮播滑块时,当手指释放后,动画的执行时间(ms)
|
|
78
|
+
* @en Animation duration(ms) after the finger is released When manually switching the carousel slider
|
|
79
|
+
* @default 300
|
|
80
|
+
*/
|
|
81
|
+
animateDurationSlide?: number;
|
|
82
|
+
/**
|
|
83
|
+
* 是否开启循环轮播,当指定轮播滑块宽度时该属性不生效
|
|
84
|
+
* @en Whether to enable circular rotation, it does not take effect when the slider width is specified
|
|
85
|
+
* @default true
|
|
86
|
+
*/
|
|
87
|
+
loop?: boolean;
|
|
88
|
+
/**
|
|
89
|
+
* 是否开启自动播放
|
|
90
|
+
* @en Whether to enable autoplay
|
|
91
|
+
* @default true
|
|
92
|
+
*/
|
|
93
|
+
autoPlay?: boolean;
|
|
94
|
+
/**
|
|
95
|
+
* 自动播放方向
|
|
96
|
+
* @en Direction when playing auto
|
|
97
|
+
* @default "normal"
|
|
98
|
+
*/
|
|
99
|
+
autoPlayDirection?: 'normal' | 'reverse';
|
|
100
|
+
/**
|
|
101
|
+
* 是否响应手势滑动
|
|
102
|
+
* @en Whether to respond to gesture swipe
|
|
103
|
+
* @default true
|
|
104
|
+
*/
|
|
105
|
+
swipeable?: boolean;
|
|
106
|
+
/**
|
|
107
|
+
* 开启自动播放时,每一个滑块的停留时间(ms)
|
|
108
|
+
* @en The stay duration of each slider when autoplay is turned on
|
|
109
|
+
* @default 4000
|
|
110
|
+
*/
|
|
111
|
+
stayDuration?: number;
|
|
112
|
+
/**
|
|
113
|
+
* 指定容器宽度,默认为基于父容器100%
|
|
114
|
+
* @en Width of the box, the default value is 100% based on the parent container
|
|
115
|
+
*/
|
|
116
|
+
boxWidth?: number | string;
|
|
117
|
+
/**
|
|
118
|
+
* 指定容器高度,默认为自适应高度;当设置`vertical=true`时,该属性为必填
|
|
119
|
+
* @en Height of the box. The property is required when vertical is true
|
|
120
|
+
*/
|
|
121
|
+
boxHeight?: number | string;
|
|
122
|
+
/**
|
|
123
|
+
* 指定初始容器宽度,常用于ssr首屏初始化,水平轮播时可用
|
|
124
|
+
* @en Initial box width, commonly used for ssr first screen initialization, available when horizontal rotation
|
|
125
|
+
*/
|
|
126
|
+
baseBoxWidth?: number;
|
|
127
|
+
/**
|
|
128
|
+
* 指定初始容器宽度,常用于ssr首屏初始化,垂直轮播时可用
|
|
129
|
+
* @en Initial box height, commonly used for ssr first screen initialization, available when vertical rotation
|
|
130
|
+
*/
|
|
131
|
+
baseBoxHeight?: number;
|
|
132
|
+
/**
|
|
133
|
+
* 指定轮播滑块宽度,为0或不传时为默认的父容器宽度
|
|
134
|
+
* @en Width of the carousel slider, the default value is parent container width if it is 0 or not input
|
|
135
|
+
* @default 0
|
|
136
|
+
*/
|
|
137
|
+
width?: number;
|
|
138
|
+
/**
|
|
139
|
+
* 指定轮播滑块高度,如不指定则为自适应高度
|
|
140
|
+
* @en Height of the carousel slider, it will be adaptive if not input
|
|
141
|
+
*/
|
|
142
|
+
height?: number;
|
|
143
|
+
/**
|
|
144
|
+
* 默认选中的滑块索引
|
|
145
|
+
* @en Default selected slider index
|
|
146
|
+
* @default 0
|
|
147
|
+
*/
|
|
148
|
+
initialIndex?: number;
|
|
149
|
+
/**
|
|
150
|
+
* 自定义轮播索引
|
|
151
|
+
* @en Custom indicator
|
|
152
|
+
*/
|
|
153
|
+
renderIndicator?: (currentIndex: number, total: number, lastIndex: number) => ReactNode;
|
|
154
|
+
/**
|
|
155
|
+
* 轮播索引位置
|
|
156
|
+
* @en Position of the indicator
|
|
157
|
+
* @default "center"
|
|
158
|
+
*/
|
|
159
|
+
indicatorPos?: 'start' | 'center' | 'end';
|
|
160
|
+
/**
|
|
161
|
+
* 垂直轮播索引横向位置
|
|
162
|
+
* @en Horizontal position of the vertical indicator
|
|
163
|
+
* @default "left"
|
|
164
|
+
*/
|
|
165
|
+
indicatorVerticalPos?: 'left' | 'right';
|
|
166
|
+
/**
|
|
167
|
+
* 轮播索引是否放轮播图外面
|
|
168
|
+
* @en Whether the indicator is placed outside the carousel
|
|
169
|
+
*/
|
|
170
|
+
indicatorOutside?: boolean;
|
|
171
|
+
/**
|
|
172
|
+
* 是否展示轮播索引
|
|
173
|
+
* @en Whether to show the indicator
|
|
174
|
+
* @default true
|
|
175
|
+
*/
|
|
176
|
+
showIndicator?: boolean;
|
|
177
|
+
/**
|
|
178
|
+
* children 只有一个时隐藏轮播索引
|
|
179
|
+
* @en Whether to hide the indicator if just one child
|
|
180
|
+
* @default true
|
|
181
|
+
*/
|
|
182
|
+
hideSingleIndicator?: boolean;
|
|
183
|
+
/**
|
|
184
|
+
* 轮播索引样式类型
|
|
185
|
+
* @en the style type of the indicator
|
|
186
|
+
* @default "square"
|
|
187
|
+
*/
|
|
188
|
+
indicatorType?: 'square' | 'circle';
|
|
189
|
+
/**
|
|
190
|
+
* 轮播索引类名
|
|
191
|
+
* @en Indicator classname
|
|
192
|
+
*/
|
|
193
|
+
indicatorClass?: string;
|
|
194
|
+
/**
|
|
195
|
+
* 轮播索引是否反色,默认根据索引是否放在外面决定
|
|
196
|
+
* @en Whether the carousel indicator is inverse color, the default is determined according to whether the indicator is placed outside
|
|
197
|
+
*/
|
|
198
|
+
indicatorInverse?: boolean;
|
|
199
|
+
/**
|
|
200
|
+
* 子元素间距
|
|
201
|
+
* @en Children elements spacing
|
|
202
|
+
* @default 0
|
|
203
|
+
*/
|
|
204
|
+
spaceBetween?: number;
|
|
205
|
+
/**
|
|
206
|
+
* 前后两端露出距离,设置值时不能循环轮播
|
|
207
|
+
* @en The exposed distance of the front and rear. When the value is set, the carousel cannot be rotated.
|
|
208
|
+
* @default 0
|
|
209
|
+
*/
|
|
210
|
+
offsetBetween?: number | { left?: number; right?: number };
|
|
211
|
+
/**
|
|
212
|
+
* 手动控制当前展示元素
|
|
213
|
+
* @en Manually control the current display element
|
|
214
|
+
*/
|
|
215
|
+
currentIndex?: number;
|
|
216
|
+
/**
|
|
217
|
+
* 容器高度自适应
|
|
218
|
+
* @en Whether the container height is adaptive
|
|
219
|
+
* @default false
|
|
220
|
+
*/
|
|
221
|
+
autoHeight?: boolean;
|
|
222
|
+
/**
|
|
223
|
+
* 滑动切换距离阈值(宽度比例),范围为[0, 1],如果该属性和`distanceToChange`属性均设置,则实际计算结果更大的生效
|
|
224
|
+
* @en Sliding switching distance threshold (width ratio), the range is [0, 1]. If the property and the `distanceToChange` property are both set, the actual calculation result will take effect with a larger value.
|
|
225
|
+
* @default 0.3
|
|
226
|
+
*/
|
|
227
|
+
percentToChange?: number;
|
|
228
|
+
/**
|
|
229
|
+
* 滑动切换距离阈值(固定px宽度),如果该属性和`percentToChange`属性均设置,则实际计算结果更大的生效
|
|
230
|
+
* @en Sliding switching distance threshold (fixed px width), if both this property and the `percentToChange` property are set, the actual calculation result will take effect with a larger one
|
|
231
|
+
* @default 10
|
|
232
|
+
*/
|
|
233
|
+
distanceToChange?: number;
|
|
234
|
+
/**
|
|
235
|
+
* 滑动切换速度阈值(手指从按下到抬起之间的滑动速度,单位为px/s),与滑动切换距离阈值同时设置时,满足其中一个即生效
|
|
236
|
+
* @en The sliding switching speed threshold (the sliding speed of the finger from pressing to lifting, in px/s), when it is set at the same time as the sliding switching distance threshold, it will take effect if one of them is satisfied.
|
|
237
|
+
* @default 200
|
|
238
|
+
*/
|
|
239
|
+
speedToChange?: number;
|
|
240
|
+
/**
|
|
241
|
+
* 是否垂直轮播,设置后`boxHeight`属性必填
|
|
242
|
+
* @en Whether to rotate vertically, the `boxHeight` property is required after setting
|
|
243
|
+
*/
|
|
244
|
+
vertical?: boolean;
|
|
245
|
+
/**
|
|
246
|
+
* 只加载当前页相邻的n个内容,为0时会销毁所有相邻内容,不传则加载所有内容
|
|
247
|
+
* @en Only load the n sliders adjacent to the current slider. If it is 0, all adjacent content will be destroyed. If not input, all sliders will be loaded.
|
|
248
|
+
*/
|
|
249
|
+
lazyloadCount?: number;
|
|
250
|
+
/**
|
|
251
|
+
* 触发onTouchStopped的最小阈值
|
|
252
|
+
* @en Minimum threshold to trigger onTouchStopped
|
|
253
|
+
* @default 0
|
|
254
|
+
*/
|
|
255
|
+
stopTouchThreshold?: number;
|
|
256
|
+
/**
|
|
257
|
+
* 当不可循环时,是否开启滑动到最前或最后时的回弹效果
|
|
258
|
+
* @en When it is not loopable, whether to enable the rebound effect when sliding to the front or the end
|
|
259
|
+
* @default false
|
|
260
|
+
*/
|
|
261
|
+
bounceWhenNoLoop?: boolean;
|
|
262
|
+
/**
|
|
263
|
+
* 当开启最前或最后时的回弹效果时的阻尼系数
|
|
264
|
+
* @en The damping coefficient when the front or rear rebound effect is turned on
|
|
265
|
+
* @default 3
|
|
266
|
+
*/
|
|
267
|
+
bounceDampRate?: number;
|
|
268
|
+
/**
|
|
269
|
+
* 非active的滑块的大小比例,[0, 1]的小数,设置后切换时将有放大效果
|
|
270
|
+
* @en The size ratio of the inactive slider, the value range is [0, 1]. After setting, it will have a magnification effect when switching.
|
|
271
|
+
*/
|
|
272
|
+
inactiveScale?: number;
|
|
273
|
+
/**
|
|
274
|
+
* 触摸事件是否需要 stopPropagation
|
|
275
|
+
* @en Whether the touch event requires stopPropagation
|
|
276
|
+
* @default true
|
|
277
|
+
*/
|
|
278
|
+
stopPropagation?: boolean;
|
|
279
|
+
/**
|
|
280
|
+
* 是否需要fake首尾item,用于offsetBetween不等于0时循环轮播的衔接
|
|
281
|
+
* @en Whether to fake the first and last items, which is used for the connection of the circular rotation when the offsetBetween is not equal to 0
|
|
282
|
+
* @default false
|
|
283
|
+
*/
|
|
284
|
+
fakeItem?: boolean;
|
|
285
|
+
/**
|
|
286
|
+
* 滑动到最后时是否允许留白,仅在loop=false且设置了width时有效
|
|
287
|
+
* @en Whether to allow white space when sliding to the end, only valid when loop=false and width is set
|
|
288
|
+
* @default false
|
|
289
|
+
*/
|
|
290
|
+
allowEndBlank?: boolean;
|
|
291
|
+
/**
|
|
292
|
+
* 在iOS下是否需要在切屏时做DOM强刷优化,用于修复iOS息屏时自动播放的蜜汁渲染问题
|
|
293
|
+
* @en Whether to do DOM forced refresh optimization when the screen is off under iOS, to fix the rendering problem of automatic playback when the iOS screen is off
|
|
294
|
+
* @default true
|
|
295
|
+
*/
|
|
296
|
+
iOSVisibleOptimize?: boolean;
|
|
297
|
+
/**
|
|
298
|
+
* 在轮播图内部渲染额外元素,该元素不随轮播滑动,但处于手指可交互热区
|
|
299
|
+
* @en Render an additional element inside the carousel, which does not slide with the carousel, but is in a finger-interactive hotspot
|
|
300
|
+
*/
|
|
301
|
+
renderExtra?: (currentIndex: number) => ReactNode;
|
|
302
|
+
/**
|
|
303
|
+
* 自定义手指滑动跟手的距离计算方式,posDis表示touchmove的距离,wrapSize表示容器在滑动方向的尺寸,childSize表示滑块在滑动方向的尺寸
|
|
304
|
+
* @en Customize the calculation method of the finger swipe distance. posDis - touchmove distance, wrapSize - container size in the sliding direction, childSize - slider size in the sliding direction
|
|
305
|
+
* @default (posDis, wrapSize, childSize) => childSize * (posDis / wrapSize)
|
|
306
|
+
*/
|
|
307
|
+
distanceProcessor?: (posDis: number, wrapSize: number, childSize: number) => number;
|
|
308
|
+
/**
|
|
309
|
+
* 组件内部的滚动容器,用于豁免滑动事件响应
|
|
310
|
+
* @en The scroll container inside the component, used to exempt the sliding event response
|
|
311
|
+
*/
|
|
312
|
+
getInnerScrollContainer?: () => (HTMLElement | null)[] | HTMLElement | null;
|
|
313
|
+
/**
|
|
314
|
+
* 当轮播不支持循环且滑到最前面或最后面,还想再滑动时触发
|
|
315
|
+
* @en Triggered when the carousel does not support looping and slides to the front or back, but want to slide again
|
|
316
|
+
*/
|
|
317
|
+
onTouchStopped?: (direction: -1 | 1) => void;
|
|
318
|
+
/**
|
|
319
|
+
* 轮播滑块切换时触发
|
|
320
|
+
* @en Triggered when the carousel slider is toggled
|
|
321
|
+
* */
|
|
322
|
+
onChange?: (index: number) => void;
|
|
323
|
+
/**
|
|
324
|
+
* 轮播滑块切换,动画完成后触发
|
|
325
|
+
* @en Triggered after the carousel slider toggle animation is complete
|
|
326
|
+
* */
|
|
327
|
+
onAfterChange?: (index: number, oldIndex: number) => void;
|
|
328
|
+
/**
|
|
329
|
+
* 轮播内容touchstart事件
|
|
330
|
+
* @en Carousel content touchstart event
|
|
331
|
+
* */
|
|
332
|
+
onTouchStart?: (e: TouchEvent, total: number, index: number) => void | boolean;
|
|
333
|
+
/**
|
|
334
|
+
* 轮播内容touchmove事件
|
|
335
|
+
* @en Carousel content touchmove event
|
|
336
|
+
* */
|
|
337
|
+
onTouchMove?: (e: TouchEvent, total: number, index: number) => void | boolean;
|
|
338
|
+
/**
|
|
339
|
+
* 轮播内容touchend / touchcancel事件
|
|
340
|
+
* @en Carousel content touchend / touchcancel event
|
|
341
|
+
* */
|
|
342
|
+
onTouchEnd?: (e: TouchEvent, total: number, index: number) => void | boolean;
|
|
343
|
+
/**
|
|
344
|
+
* 轮播切换动画开始时回调
|
|
345
|
+
* @en Callback when the carousel transition animation starts
|
|
346
|
+
* */
|
|
347
|
+
onTransitionStart?: () => void;
|
|
348
|
+
/**
|
|
349
|
+
* 轮播切换动画结束时回调
|
|
350
|
+
* @en Callback when the carousel transition animation ends
|
|
351
|
+
* */
|
|
352
|
+
onTransitionEnd?: () => void;
|
|
353
|
+
/**
|
|
354
|
+
* 轮播左右滑动时回调,用于监听滑动距离以做滑动同步交互
|
|
355
|
+
* @en Callback when the carousel slides left and right, used to monitor the sliding distance for sliding synchronization interaction
|
|
356
|
+
* */
|
|
357
|
+
onDistanceChange?: (distance: number, wrapSize: number, activeIndex: number) => void;
|
|
358
|
+
/**
|
|
359
|
+
* 自定义页面展示隐藏监听,默认在document监听visibilitychange事件,返回function用于在组件卸载时移除监听
|
|
360
|
+
* @en Custom page visibility listener. By default, the visibilitychange event is monitored in the document. The return function is used to remove the listener when the component is unloaded.
|
|
361
|
+
* */
|
|
362
|
+
onPageVisibleChange?: (
|
|
363
|
+
updateWhenVisible: () => void,
|
|
364
|
+
updateWhenInvisible: () => void,
|
|
365
|
+
) => () => void | undefined;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
export interface CarouselRef {
|
|
369
|
+
/**
|
|
370
|
+
* 最外层元素 DOM
|
|
371
|
+
* @en The outermost element DOM
|
|
372
|
+
*/
|
|
373
|
+
dom: HTMLDivElement | null;
|
|
374
|
+
/**
|
|
375
|
+
* 轮播图子项包裹容器 DOM,承载了 touch 相关事件
|
|
376
|
+
* @en Wrapper DOM of carousel slider items, carry touch related events
|
|
377
|
+
*/
|
|
378
|
+
wrap: HTMLDivElement | null;
|
|
379
|
+
/**
|
|
380
|
+
* 轮播图每个子项的 DOM
|
|
381
|
+
* @en Carousel slider DOM
|
|
382
|
+
* */
|
|
383
|
+
items: (HTMLDivElement | null)[];
|
|
384
|
+
/**
|
|
385
|
+
* 在某些条件下内部会禁用循环轮播,这里表示循环是否被禁用
|
|
386
|
+
* @en The loop rotation is disabled internally under certain conditions, here indicates whether the loop is disabled
|
|
387
|
+
*/
|
|
388
|
+
noLoop: boolean;
|
|
389
|
+
/**
|
|
390
|
+
* 手动重新计算布局
|
|
391
|
+
* @en Recalculate the layout manually
|
|
392
|
+
*/
|
|
393
|
+
updateData: () => void;
|
|
394
|
+
/**
|
|
395
|
+
* 手动更新当前选中索引,rightNow 表示是否立刻跳转到目标索引,否则执行过渡动画到目标索引
|
|
396
|
+
* @en Manually update the currently selected index, rightNow indicates whether to jump to the target index immediately, otherwise perform a transition animation to the target index
|
|
397
|
+
* */
|
|
398
|
+
changeIndex: (newIndex: number, rightNow?: boolean, direction?: 'right' | 'left') => void;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* 轮播组件,支持自定义轮播索引样式及滑块宽度。**需要注意的是,如果正使用`fastclick`,需要为 touchstart 的 target 添加`needsclick`类**(<a href="https://github.com/ftlabs/fastclick#ignore-certain-elements-with-needsclick" target="_blank">详情戳这里</a>),以规避`fastclick`逻辑与组件内部的手势冲突。(如果使用了`list`属性则无需额外添加)
|
|
403
|
+
* @en Carousel component, supports custom carousel index style and slider width. **Note that if you are using `fastclick`, you need to add a `needsclick` class to the touchstart target ** (<a href="https://github.com/ftlabs/fastclick#ignore-certain-elements- with-needsclick" target="_blank">click here for details</a>) to avoid `fastclick` logic conflicts with gestures inside the component. (no need to add if `list` is set)
|
|
404
|
+
* @type 信息展示
|
|
405
|
+
* @type_en Data Display
|
|
406
|
+
* @name 轮播图
|
|
407
|
+
* @name_en Carousel
|
|
408
|
+
*/
|
|
409
|
+
const Carousel = forwardRef((props: CarouselProps, ref: Ref<CarouselRef>) => {
|
|
410
|
+
const {
|
|
411
|
+
className,
|
|
412
|
+
wrapStyle,
|
|
413
|
+
style,
|
|
414
|
+
children,
|
|
415
|
+
list = [],
|
|
416
|
+
animateDuration = 500,
|
|
417
|
+
animateDurationSlide = 300,
|
|
418
|
+
loop = true,
|
|
419
|
+
autoPlay = true,
|
|
420
|
+
autoPlayDirection = 'normal',
|
|
421
|
+
swipeable = true,
|
|
422
|
+
stayDuration = 4000,
|
|
423
|
+
boxWidth,
|
|
424
|
+
boxHeight,
|
|
425
|
+
baseBoxWidth,
|
|
426
|
+
baseBoxHeight,
|
|
427
|
+
width = 0,
|
|
428
|
+
height,
|
|
429
|
+
initialIndex = 0,
|
|
430
|
+
renderIndicator,
|
|
431
|
+
indicatorPos = 'center',
|
|
432
|
+
indicatorVerticalPos = 'left',
|
|
433
|
+
indicatorOutside,
|
|
434
|
+
showIndicator = true,
|
|
435
|
+
hideSingleIndicator = true,
|
|
436
|
+
indicatorType = 'square',
|
|
437
|
+
indicatorClass = '',
|
|
438
|
+
indicatorInverse,
|
|
439
|
+
spaceBetween = 0,
|
|
440
|
+
offsetBetween = 0,
|
|
441
|
+
currentIndex,
|
|
442
|
+
autoHeight = false,
|
|
443
|
+
percentToChange = 0.3,
|
|
444
|
+
distanceToChange = 10,
|
|
445
|
+
speedToChange = 100,
|
|
446
|
+
vertical,
|
|
447
|
+
lazyloadCount,
|
|
448
|
+
inactiveScale = 1,
|
|
449
|
+
stopPropagation = true,
|
|
450
|
+
fakeItem = false,
|
|
451
|
+
allowEndBlank = false,
|
|
452
|
+
bounceWhenNoLoop = false,
|
|
453
|
+
bounceDampRate = 3,
|
|
454
|
+
iOSVisibleOptimize = true,
|
|
455
|
+
renderExtra,
|
|
456
|
+
distanceProcessor,
|
|
457
|
+
getInnerScrollContainer,
|
|
458
|
+
onChange,
|
|
459
|
+
onAfterChange,
|
|
460
|
+
onTouchStart,
|
|
461
|
+
onTouchMove,
|
|
462
|
+
onTouchEnd,
|
|
463
|
+
onTransitionStart,
|
|
464
|
+
onTransitionEnd,
|
|
465
|
+
onDistanceChange,
|
|
466
|
+
stopTouchThreshold = 0,
|
|
467
|
+
onTouchStopped,
|
|
468
|
+
onPageVisibleChange,
|
|
469
|
+
} = props;
|
|
470
|
+
|
|
471
|
+
const { useRtl } = useContext(GlobalContext);
|
|
472
|
+
const horizontalUseRtl = !vertical && useRtl;
|
|
473
|
+
const domRef = useRef<HTMLDivElement | null>(null);
|
|
474
|
+
const wrapRef = useRef<HTMLDivElement | null>(null);
|
|
475
|
+
const innerRef = useRef<HTMLDivElement | null>(null);
|
|
476
|
+
const itemsRef = useRef<(HTMLDivElement | null)[]>([]);
|
|
477
|
+
const fakeWrapRef = useRef<HTMLDivElement | null>(null);
|
|
478
|
+
const timerRef = useRef<number | null>(null);
|
|
479
|
+
const movedChildRef = useRef(-1);
|
|
480
|
+
const touchStartTimeRef = useRef(0);
|
|
481
|
+
const touchStartedRef = useRef(false);
|
|
482
|
+
const touchMovedRef = useRef(false);
|
|
483
|
+
const [wrapWidth, setWrapWidth] = useState(baseBoxWidth || 0);
|
|
484
|
+
const [wrapHeight, setWrapHeight] = useState(baseBoxHeight || 0);
|
|
485
|
+
const touchStartXRef = useRef(0);
|
|
486
|
+
const touchStartYRef = useRef(0);
|
|
487
|
+
const scrollingRef = useRef<boolean | null>(null);
|
|
488
|
+
const touchStoppedRef = useRef(false);
|
|
489
|
+
const bouncingRef = useRef(false);
|
|
490
|
+
const [currentItemHeight, setCurrentItemHeight] = useState<number | string>('auto');
|
|
491
|
+
const [posAdjusting, posAdjustingRef, setPosAdjusting] = useRefState(false);
|
|
492
|
+
const [distance, distanceRef, setDistance] = useRefState(0);
|
|
493
|
+
const [isAutoJump, setIsAutoJump] = useState(true);
|
|
494
|
+
const [index, indexRef, setIndex] = useRefState(
|
|
495
|
+
currentIndex !== void 0 ? currentIndex : initialIndex,
|
|
496
|
+
);
|
|
497
|
+
const [transforms, transformsRef, setTransforms] = useRefState<number[]>([]);
|
|
498
|
+
const [direction, directionRef, setStateDirection] = useRefState<'left' | 'right' | ''>(
|
|
499
|
+
(autoPlayDirection === 'reverse' || horizontalUseRtl) && autoPlay ? 'right' : 'left',
|
|
500
|
+
);
|
|
501
|
+
const lastDirectionRef = useRef('');
|
|
502
|
+
const lastShownIndexRef = useRef(-1);
|
|
503
|
+
const { allChildren, fakeTwoChildren } = getAllChildren();
|
|
504
|
+
const total: number = allChildren.length;
|
|
505
|
+
const shownTotal = fakeTwoChildren ? 2 : total;
|
|
506
|
+
const indicatorArr = useMemo(() => {
|
|
507
|
+
const arr: number[] = [];
|
|
508
|
+
for (let i = 0; i < shownTotal; i += 1) {
|
|
509
|
+
arr.push(i);
|
|
510
|
+
}
|
|
511
|
+
return arr;
|
|
512
|
+
}, [shownTotal]);
|
|
513
|
+
const rtlRatio = horizontalUseRtl ? -1 : 1;
|
|
514
|
+
const leftDirec = horizontalUseRtl ? 'right' : 'left';
|
|
515
|
+
const rightDirec = horizontalUseRtl ? 'left' : 'right';
|
|
516
|
+
const leftOffset = typeof offsetBetween === 'number' ? offsetBetween : offsetBetween.left || 0;
|
|
517
|
+
const rightOffset =
|
|
518
|
+
typeof offsetBetween === 'number' ? offsetBetween : offsetBetween.right || 0;
|
|
519
|
+
const wrapSize = vertical ? wrapHeight : wrapWidth;
|
|
520
|
+
const childWidth = (width || wrapWidth) - spaceBetween - (leftOffset + rightOffset);
|
|
521
|
+
const childHeight = (height || wrapHeight) - spaceBetween - (leftOffset + rightOffset);
|
|
522
|
+
const childSize = vertical ? childHeight : childWidth;
|
|
523
|
+
const noLoop =
|
|
524
|
+
!loop ||
|
|
525
|
+
(!vertical && Boolean(width)) ||
|
|
526
|
+
(vertical && Boolean(height)) ||
|
|
527
|
+
total < 2 ||
|
|
528
|
+
((leftOffset > 0 || rightOffset > 0) && !fakeItem);
|
|
529
|
+
const shownIndex = getShownIndex(index);
|
|
530
|
+
const dynamicAnimateDuration = getDynamicDuration(isAutoJump);
|
|
531
|
+
const cubic = isAutoJump ? 'auto' : 'slide';
|
|
532
|
+
const indexFixed = currentIndex !== void 0;
|
|
533
|
+
const needTouchEvent = swipeable && !indexFixed && childSize > 0;
|
|
534
|
+
const userSetBoxWidth = boxWidth || baseBoxWidth;
|
|
535
|
+
const userSetBoxHeight = boxHeight || baseBoxHeight;
|
|
536
|
+
const noInterval = noLoop || !autoPlay || indexFixed || childSize <= 0;
|
|
537
|
+
const inactiveValidScale = useMemo(
|
|
538
|
+
() => Math.max(0, Math.min(inactiveScale, 1)),
|
|
539
|
+
[inactiveScale],
|
|
540
|
+
);
|
|
541
|
+
const [fakeIndexes, setFakeIndexes] = useState<{ index: number; side: 'left' | 'right' }[]>([]);
|
|
542
|
+
const system = useSystem();
|
|
543
|
+
// 开启自动循环时iOS会有渲染问题需要强刷dom,但不需要autoPlay的不用强刷,这里判断下
|
|
544
|
+
// @en When the automatic loop is turned on, there will be rendering problems in iOS. Need to brush the dom, but if you don't need autoPlay, don't need to brush.
|
|
545
|
+
const needRefreshDom = !noInterval && system === 'ios' && iOSVisibleOptimize;
|
|
546
|
+
|
|
547
|
+
const setDirection = useCallback((newDirec: 'left' | 'right' | '') => {
|
|
548
|
+
setStateDirection(direc => {
|
|
549
|
+
lastDirectionRef.current = direc;
|
|
550
|
+
return newDirec;
|
|
551
|
+
});
|
|
552
|
+
}, []);
|
|
553
|
+
|
|
554
|
+
const wrapSizeRef = useLatestRef(wrapSize);
|
|
555
|
+
const updateDataRef = useLatestRef(updateData);
|
|
556
|
+
const setPlayIntervalRef = useLatestRef(setPlayInterval);
|
|
557
|
+
|
|
558
|
+
const handleTouchStart = useCallback(
|
|
559
|
+
(e: TouchEvent) => {
|
|
560
|
+
if (onTouchStart && onTouchStart(e, shownTotal, getShownIndex(indexRef.current))) {
|
|
561
|
+
return;
|
|
562
|
+
}
|
|
563
|
+
if (posAdjustingRef.current) {
|
|
564
|
+
return;
|
|
565
|
+
}
|
|
566
|
+
// touchMove触发时,阻止handleTouchStart多次执行(如点击事件)
|
|
567
|
+
// @en When touchMove is triggered, prevent handleTouchStart from executing multiple times (such as click events)
|
|
568
|
+
if (touchStartedRef.current && touchMovedRef.current) {
|
|
569
|
+
return;
|
|
570
|
+
}
|
|
571
|
+
touchStartedRef.current = true;
|
|
572
|
+
touchMovedRef.current = false;
|
|
573
|
+
clear();
|
|
574
|
+
const evt = e.touches[0];
|
|
575
|
+
touchStartXRef.current = evt.clientX || 0;
|
|
576
|
+
touchStartYRef.current = evt.clientY || 0;
|
|
577
|
+
scrollingRef.current = null;
|
|
578
|
+
touchStoppedRef.current = false;
|
|
579
|
+
setPosAdjusting(false);
|
|
580
|
+
touchStartTimeRef.current = new Date().getTime();
|
|
581
|
+
},
|
|
582
|
+
[shownTotal, onTouchStart],
|
|
583
|
+
);
|
|
584
|
+
|
|
585
|
+
const triggerTouchStopped = useCallback(
|
|
586
|
+
(posDis: number) => {
|
|
587
|
+
const needStop = Math.abs(posDis) > stopTouchThreshold;
|
|
588
|
+
if (!touchStoppedRef.current && needStop) {
|
|
589
|
+
onTouchStopped && onTouchStopped(posDis >= 0 ? -1 : 1);
|
|
590
|
+
}
|
|
591
|
+
touchStoppedRef.current = needStop;
|
|
592
|
+
},
|
|
593
|
+
[stopTouchThreshold, onTouchStopped],
|
|
594
|
+
);
|
|
595
|
+
|
|
596
|
+
const handleTouchMove = useCallback(
|
|
597
|
+
(e: TouchEvent) => {
|
|
598
|
+
if (onTouchMove && onTouchMove(e, shownTotal, getShownIndex(indexRef.current))) {
|
|
599
|
+
return;
|
|
600
|
+
}
|
|
601
|
+
stopPropagation && e.stopPropagation();
|
|
602
|
+
if (!touchStartedRef.current || posAdjustingRef.current) {
|
|
603
|
+
e.cancelable && e.preventDefault();
|
|
604
|
+
return;
|
|
605
|
+
}
|
|
606
|
+
touchMovedRef.current = true;
|
|
607
|
+
const evt = e.changedTouches[0];
|
|
608
|
+
const touchMoveX = Math.max(0, evt.clientX || 0);
|
|
609
|
+
const touchMoveY = evt.clientY || 0;
|
|
610
|
+
const posDisX = touchMoveX - touchStartXRef.current;
|
|
611
|
+
const posDisY = touchMoveY - touchStartYRef.current;
|
|
612
|
+
const absDisX = Math.abs(posDisX);
|
|
613
|
+
const absDisY = Math.abs(posDisY);
|
|
614
|
+
const originDis = vertical ? posDisY : posDisX;
|
|
615
|
+
const processor = distanceProcessor || ((dis, size, child) => child * (dis / size));
|
|
616
|
+
const posDis = processor(originDis, wrapSizeRef.current, childSize);
|
|
617
|
+
if (total < 2) {
|
|
618
|
+
triggerTouchStopped(posDis);
|
|
619
|
+
return;
|
|
620
|
+
}
|
|
621
|
+
if (scrollingRef.current === null) {
|
|
622
|
+
scrollingRef.current = vertical ? absDisY < absDisX : absDisX < absDisY;
|
|
623
|
+
}
|
|
624
|
+
// 如果是在滚动页面则禁用轮播图手势
|
|
625
|
+
// @en Disable carousel gestures if scrolling
|
|
626
|
+
if (scrollingRef.current) {
|
|
627
|
+
setDistance(0);
|
|
628
|
+
// bugfix: 未触发横滑就不触发touchend逻辑,且这个场景不需要触发onTouchStopped,因此直接设置touchStoppedRef
|
|
629
|
+
// @en bugfix: The touchend event will not be triggered if the horizontal swipe is not triggered, and this scene does not need to trigger onTouchStopped.
|
|
630
|
+
touchStoppedRef.current = true;
|
|
631
|
+
// 不会触发jumpTo,但touchstart时clear了,所以要手动设置一次interval
|
|
632
|
+
// @en JumpTo will not be triggered, but it is cleared in touchstart event, so need to manually set interval once
|
|
633
|
+
setPlayIntervalRef.current();
|
|
634
|
+
return;
|
|
635
|
+
}
|
|
636
|
+
setDirection(posDis >= 0 ? 'right' : 'left');
|
|
637
|
+
const comparedPos = posDis * rtlRatio;
|
|
638
|
+
if (
|
|
639
|
+
noLoop &&
|
|
640
|
+
((indexRef.current === 0 && comparedPos > 0) ||
|
|
641
|
+
(indexRef.current === total - 1 && comparedPos < 0))
|
|
642
|
+
) {
|
|
643
|
+
triggerTouchStopped(posDis);
|
|
644
|
+
if (bounceWhenNoLoop && bounceDampRate) {
|
|
645
|
+
e.cancelable && e.preventDefault();
|
|
646
|
+
bouncingRef.current = true;
|
|
647
|
+
setDistance(posDis / bounceDampRate);
|
|
648
|
+
} else {
|
|
649
|
+
setDistance(0);
|
|
650
|
+
}
|
|
651
|
+
return;
|
|
652
|
+
}
|
|
653
|
+
e.cancelable && e.preventDefault();
|
|
654
|
+
touchStoppedRef.current = false;
|
|
655
|
+
const maxDis = childSize;
|
|
656
|
+
setDistance(posDis > 0 ? Math.min(maxDis, posDis) : Math.max(-1 * maxDis, posDis));
|
|
657
|
+
},
|
|
658
|
+
[
|
|
659
|
+
shownTotal,
|
|
660
|
+
noLoop,
|
|
661
|
+
vertical,
|
|
662
|
+
childSize,
|
|
663
|
+
stopPropagation,
|
|
664
|
+
bounceWhenNoLoop,
|
|
665
|
+
bounceDampRate,
|
|
666
|
+
horizontalUseRtl,
|
|
667
|
+
onTouchMove,
|
|
668
|
+
triggerTouchStopped,
|
|
669
|
+
],
|
|
670
|
+
);
|
|
671
|
+
|
|
672
|
+
useImperativeHandle(
|
|
673
|
+
ref,
|
|
674
|
+
() => ({
|
|
675
|
+
dom: domRef.current,
|
|
676
|
+
wrap: wrapRef.current,
|
|
677
|
+
items: itemsRef.current,
|
|
678
|
+
noLoop,
|
|
679
|
+
updateData,
|
|
680
|
+
changeIndex,
|
|
681
|
+
}),
|
|
682
|
+
[updateData, changeIndex],
|
|
683
|
+
);
|
|
684
|
+
|
|
685
|
+
useListenResize(updateData, [updateData]);
|
|
686
|
+
|
|
687
|
+
useSwiperInnerScroll(getInnerScrollContainer);
|
|
688
|
+
|
|
689
|
+
const updateWhenVisible = useCallback(() => {
|
|
690
|
+
fakeWrapRef.current && (fakeWrapRef.current.style.display = 'none');
|
|
691
|
+
wrapRef.current && (wrapRef.current.style.display = '');
|
|
692
|
+
// 部分情况下页面隐藏后容器宽度变成0,因此页面返回后需重新取一次容器尺寸
|
|
693
|
+
// @en In some cases, the width of the container becomes 0 after the page is invisible, so the container size needs to be resized after the page is visible.
|
|
694
|
+
updateDataRef.current();
|
|
695
|
+
}, []);
|
|
696
|
+
|
|
697
|
+
const updateWhenInvisible = useCallback(() => {
|
|
698
|
+
clear();
|
|
699
|
+
if (wrapRef.current && fakeWrapRef.current) {
|
|
700
|
+
fakeWrapRef.current.style.height = `${wrapRef.current.offsetHeight || 0}px`;
|
|
701
|
+
fakeWrapRef.current.style.display = 'block';
|
|
702
|
+
wrapRef.current.style.display = 'none';
|
|
703
|
+
}
|
|
704
|
+
}, []);
|
|
705
|
+
|
|
706
|
+
useEffect(() => {
|
|
707
|
+
if (onPageVisibleChange) {
|
|
708
|
+
return onPageVisibleChange(updateWhenVisible, updateWhenInvisible);
|
|
709
|
+
}
|
|
710
|
+
// ios 在页面隐藏后返回时会有dom结构正确但渲染错乱的情况,因此在页面隐藏时先用占位符,返回后重新渲染一次dom
|
|
711
|
+
// @en In iOS, When the page is invisible, the DOM will be correct but the rendering is disordered. So use a placeholder when the page is invisible, and re-render the DOM after it is visible.
|
|
712
|
+
const update = () => {
|
|
713
|
+
if (document.visibilityState === 'visible') {
|
|
714
|
+
updateWhenVisible();
|
|
715
|
+
} else {
|
|
716
|
+
updateWhenInvisible();
|
|
717
|
+
}
|
|
718
|
+
};
|
|
719
|
+
document.addEventListener('visibilitychange', update);
|
|
720
|
+
return () => {
|
|
721
|
+
document.removeEventListener('visibilitychange', update);
|
|
722
|
+
};
|
|
723
|
+
}, [onPageVisibleChange]);
|
|
724
|
+
|
|
725
|
+
useEffect(() => {
|
|
726
|
+
if (distanceToChange > 0 && distanceToChange < 1) {
|
|
727
|
+
console.warn(
|
|
728
|
+
`[Carousel Props Warning] The value of the prop \`distanceToChange\` is too small(${distanceToChange}). Do you meant to use the prop \`percentToChange\`?`,
|
|
729
|
+
);
|
|
730
|
+
}
|
|
731
|
+
}, [distanceToChange]);
|
|
732
|
+
|
|
733
|
+
useEffect(() => {
|
|
734
|
+
if (vertical && !boxHeight) {
|
|
735
|
+
console.warn(
|
|
736
|
+
'[Carousel Rendering Warning] When the prop `vertical` is specified and the prop `boxHeight` is not specified, you need to set a fixed `height` style for the outermost element.',
|
|
737
|
+
);
|
|
738
|
+
}
|
|
739
|
+
}, [vertical, boxHeight]);
|
|
740
|
+
|
|
741
|
+
useEffect(() => {
|
|
742
|
+
if (wrapRef.current && needTouchEvent) {
|
|
743
|
+
wrapRef.current.addEventListener('touchstart', handleTouchStart);
|
|
744
|
+
wrapRef.current.addEventListener('touchmove', handleTouchMove);
|
|
745
|
+
}
|
|
746
|
+
return () => {
|
|
747
|
+
if (wrapRef.current && needTouchEvent) {
|
|
748
|
+
wrapRef.current.removeEventListener('touchstart', handleTouchStart);
|
|
749
|
+
wrapRef.current.removeEventListener('touchmove', handleTouchMove);
|
|
750
|
+
}
|
|
751
|
+
};
|
|
752
|
+
}, [handleTouchStart, handleTouchMove, needTouchEvent]);
|
|
753
|
+
|
|
754
|
+
useEffect(() => {
|
|
755
|
+
// 布局改变时重新计算
|
|
756
|
+
// @en Recalculate when layout changes
|
|
757
|
+
updateData();
|
|
758
|
+
return () => {
|
|
759
|
+
clear();
|
|
760
|
+
};
|
|
761
|
+
}, [
|
|
762
|
+
userSetBoxWidth,
|
|
763
|
+
userSetBoxHeight,
|
|
764
|
+
childWidth,
|
|
765
|
+
childHeight,
|
|
766
|
+
stayDuration,
|
|
767
|
+
noInterval,
|
|
768
|
+
autoPlayDirection,
|
|
769
|
+
]);
|
|
770
|
+
|
|
771
|
+
useUpdateEffect(() => {
|
|
772
|
+
if (currentIndex !== void 0) {
|
|
773
|
+
jumpTo(currentIndex);
|
|
774
|
+
}
|
|
775
|
+
}, [currentIndex]);
|
|
776
|
+
|
|
777
|
+
useUpdateEffect(() => {
|
|
778
|
+
onDistanceChange && onDistanceChange(distance, wrapSize, index);
|
|
779
|
+
}, [distance, wrapSize, index]);
|
|
780
|
+
|
|
781
|
+
useEffect(() => {
|
|
782
|
+
// children改变,索引超出新children范围时兼容
|
|
783
|
+
// @en When children change, it needs to be compatible when the index exceeds the range of the new children
|
|
784
|
+
if (allChildren.length && indexRef.current >= allChildren.length) {
|
|
785
|
+
jumpTo(allChildren.length - 1, true, true);
|
|
786
|
+
} else if (indexRef.current < 0) {
|
|
787
|
+
jumpTo(0, true, true);
|
|
788
|
+
}
|
|
789
|
+
}, [allChildren.length]);
|
|
790
|
+
|
|
791
|
+
useEffect(() => {
|
|
792
|
+
// bugfix: 修复ios轮播循环时闪动问题
|
|
793
|
+
// @en bugfix: Fix the flickering problem when the carousel loops in iOS
|
|
794
|
+
if (isAutoJump && posAdjusting) {
|
|
795
|
+
setTimeout(() => {
|
|
796
|
+
getFakeChild();
|
|
797
|
+
}, 100);
|
|
798
|
+
} else {
|
|
799
|
+
getFakeChild();
|
|
800
|
+
}
|
|
801
|
+
}, [index, direction, childSize, total, horizontalUseRtl]);
|
|
802
|
+
|
|
803
|
+
useEffect(() => {
|
|
804
|
+
nextTick(() => {
|
|
805
|
+
updateLayoutData();
|
|
806
|
+
});
|
|
807
|
+
}, [shownIndex, autoHeight, allChildren]);
|
|
808
|
+
|
|
809
|
+
useUpdateEffect(() => {
|
|
810
|
+
if (typeof onChange === 'function') {
|
|
811
|
+
onChange(shownIndex);
|
|
812
|
+
}
|
|
813
|
+
}, [shownIndex]);
|
|
814
|
+
|
|
815
|
+
useEffect(() => {
|
|
816
|
+
return () => {
|
|
817
|
+
lastShownIndexRef.current = shownIndex;
|
|
818
|
+
};
|
|
819
|
+
}, [shownIndex]);
|
|
820
|
+
|
|
821
|
+
useEffect(() => {
|
|
822
|
+
if (!fakeItem || posAdjusting) {
|
|
823
|
+
return;
|
|
824
|
+
}
|
|
825
|
+
setCurrentFakeIndex();
|
|
826
|
+
}, [posAdjusting, index, direction, total, childSize, horizontalUseRtl]);
|
|
827
|
+
|
|
828
|
+
function getShownIndex(nowIndex: number) {
|
|
829
|
+
const validIndex = nowIndex === total ? 0 : nowIndex;
|
|
830
|
+
const shownInd = nowIndex === -1 ? total - 1 : validIndex;
|
|
831
|
+
return fakeTwoChildren ? shownInd % 2 : shownInd;
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
function getDynamicDuration(autoJump: boolean) {
|
|
835
|
+
return autoJump ? animateDuration : animateDurationSlide;
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
function setCurrentHeight() {
|
|
839
|
+
if (!innerRef.current || !autoHeight || vertical) {
|
|
840
|
+
return;
|
|
841
|
+
}
|
|
842
|
+
const allItemDom = itemsRef.current;
|
|
843
|
+
const nowIndex = getShownIndex(indexRef.current);
|
|
844
|
+
const currentHeight = (allItemDom[nowIndex] || {}).offsetHeight || 'auto';
|
|
845
|
+
setCurrentItemHeight(currentHeight);
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
function getAllChildren() {
|
|
849
|
+
let allChild: React.ReactNode[] = [];
|
|
850
|
+
let fakeTwo = false;
|
|
851
|
+
if (list && list.length) {
|
|
852
|
+
let actualList = list;
|
|
853
|
+
if (list.length === 2 && fakeItem) {
|
|
854
|
+
fakeTwo = true;
|
|
855
|
+
actualList = list.concat(list);
|
|
856
|
+
}
|
|
857
|
+
allChild = actualList.map((item, i) => (
|
|
858
|
+
<>
|
|
859
|
+
<img
|
|
860
|
+
key={i}
|
|
861
|
+
className="carousel-item-img needsclick"
|
|
862
|
+
src={item.src}
|
|
863
|
+
alt=""
|
|
864
|
+
onClick={item.onClick}
|
|
865
|
+
/>
|
|
866
|
+
{item.text ? <div className="carousel-item-text">{item.text}</div> : null}
|
|
867
|
+
</>
|
|
868
|
+
));
|
|
869
|
+
} else if (children && Object.prototype.toString.call(children) === '[object Array]') {
|
|
870
|
+
allChild = children as React.ReactNode[];
|
|
871
|
+
if (allChild.length === 2 && fakeItem) {
|
|
872
|
+
fakeTwo = true;
|
|
873
|
+
allChild = [...allChild, ...allChild];
|
|
874
|
+
}
|
|
875
|
+
} else {
|
|
876
|
+
allChild = children ? [children] : [];
|
|
877
|
+
}
|
|
878
|
+
return { allChildren: allChild, fakeTwoChildren: fakeTwo };
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
function getDefaultDirection() {
|
|
882
|
+
const nowIndex = indexRef.current;
|
|
883
|
+
if (nowIndex === total - 1) {
|
|
884
|
+
return horizontalUseRtl ? 'right' : 'left';
|
|
885
|
+
}
|
|
886
|
+
if (nowIndex === 0) {
|
|
887
|
+
return horizontalUseRtl ? 'left' : 'right';
|
|
888
|
+
}
|
|
889
|
+
return '';
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
function changeIndex(newIndex: number, rightNow?: boolean, userSetDirec?: 'right' | 'left') {
|
|
893
|
+
if (posAdjustingRef.current) {
|
|
894
|
+
return;
|
|
895
|
+
}
|
|
896
|
+
if (userSetDirec) {
|
|
897
|
+
// rtl 模式取反
|
|
898
|
+
const direcMap: Record<string, 'right' | 'left'> = {
|
|
899
|
+
left: leftDirec,
|
|
900
|
+
right: rightDirec,
|
|
901
|
+
};
|
|
902
|
+
const direc = direcMap[userSetDirec];
|
|
903
|
+
setDirection(direc);
|
|
904
|
+
nextTick(() => {
|
|
905
|
+
jumpTo(newIndex, true, rightNow, direc);
|
|
906
|
+
});
|
|
907
|
+
} else {
|
|
908
|
+
jumpTo(newIndex, true, rightNow);
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
function jumpTo(
|
|
913
|
+
newIndex: number,
|
|
914
|
+
autoJump = true,
|
|
915
|
+
rightNow?: boolean,
|
|
916
|
+
direc?: 'right' | 'left',
|
|
917
|
+
) {
|
|
918
|
+
const oldIndex = getShownIndex(indexRef.current);
|
|
919
|
+
const changedIndex = newIndex !== oldIndex ? getShownIndex(newIndex) : -1;
|
|
920
|
+
if (direc) {
|
|
921
|
+
setDirection(direc);
|
|
922
|
+
} else if (autoJump) {
|
|
923
|
+
setDirection(autoPlayDirection === 'reverse' || horizontalUseRtl ? 'right' : 'left');
|
|
924
|
+
} else if (newIndex === indexRef.current) {
|
|
925
|
+
setDirection(distanceRef.current > 0 ? 'right' : 'left');
|
|
926
|
+
} else {
|
|
927
|
+
setDirection('');
|
|
928
|
+
}
|
|
929
|
+
setPosAdjusting(true);
|
|
930
|
+
setIsAutoJump(autoJump);
|
|
931
|
+
setDistance(0);
|
|
932
|
+
setIndex(newIndex);
|
|
933
|
+
touchStartTimeRef.current = 0;
|
|
934
|
+
onTransitionStart && onTransitionStart();
|
|
935
|
+
setTimeout(
|
|
936
|
+
() => {
|
|
937
|
+
setPosAdjusting(false);
|
|
938
|
+
nextTick(() => {
|
|
939
|
+
setDirection(getDefaultDirection());
|
|
940
|
+
const newTransform = transformsRef.current.slice();
|
|
941
|
+
let transChanged = false;
|
|
942
|
+
if (newIndex === -1 && movedChildRef.current >= 0) {
|
|
943
|
+
newTransform[movedChildRef.current] = 0;
|
|
944
|
+
transChanged = true;
|
|
945
|
+
setIndex(total - 1);
|
|
946
|
+
} else if (newIndex === total && movedChildRef.current >= 0) {
|
|
947
|
+
newTransform[movedChildRef.current] = 0;
|
|
948
|
+
transChanged = true;
|
|
949
|
+
setIndex(0);
|
|
950
|
+
}
|
|
951
|
+
transChanged && setTransforms(newTransform);
|
|
952
|
+
setIsAutoJump(true);
|
|
953
|
+
onTransitionEnd && onTransitionEnd();
|
|
954
|
+
if (changedIndex >= 0) {
|
|
955
|
+
onAfterChange && onAfterChange(changedIndex, oldIndex);
|
|
956
|
+
}
|
|
957
|
+
nextTick(() => {
|
|
958
|
+
setPlayIntervalRef.current();
|
|
959
|
+
});
|
|
960
|
+
});
|
|
961
|
+
},
|
|
962
|
+
rightNow ? 0 : getDynamicDuration(autoJump),
|
|
963
|
+
);
|
|
964
|
+
autoJump && setDirection('');
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
function setPlayInterval() {
|
|
968
|
+
clear();
|
|
969
|
+
if (noInterval) {
|
|
970
|
+
return;
|
|
971
|
+
}
|
|
972
|
+
timerRef.current = delayTimeout(() => {
|
|
973
|
+
jumpTo(autoPlayDirection === 'reverse' ? indexRef.current - 1 : indexRef.current + 1);
|
|
974
|
+
}, stayDuration);
|
|
975
|
+
}
|
|
976
|
+
|
|
977
|
+
function updateLayoutData() {
|
|
978
|
+
if (wrapRef.current) {
|
|
979
|
+
setWrapWidth(wrapRef.current.offsetWidth);
|
|
980
|
+
setWrapHeight(wrapRef.current.offsetHeight);
|
|
981
|
+
}
|
|
982
|
+
setCurrentHeight();
|
|
983
|
+
}
|
|
984
|
+
|
|
985
|
+
function updateData() {
|
|
986
|
+
updateLayoutData();
|
|
987
|
+
setPlayIntervalRef.current();
|
|
988
|
+
}
|
|
989
|
+
|
|
990
|
+
function clear() {
|
|
991
|
+
if (timerRef.current) {
|
|
992
|
+
clearTimeout(timerRef.current);
|
|
993
|
+
timerRef.current = null;
|
|
994
|
+
}
|
|
995
|
+
}
|
|
996
|
+
|
|
997
|
+
function getFakeChild() {
|
|
998
|
+
if (noLoop) {
|
|
999
|
+
// 循环状态从有到无时,重置 transforms
|
|
1000
|
+
// @en reset transforms when loop status changes to false
|
|
1001
|
+
setTransforms([]);
|
|
1002
|
+
return;
|
|
1003
|
+
}
|
|
1004
|
+
const nowIndex = indexRef.current;
|
|
1005
|
+
const newTransform = transformsRef.current.slice();
|
|
1006
|
+
const dis = Math.max(0, childSize);
|
|
1007
|
+
allChildren.forEach((_, childIndex) => {
|
|
1008
|
+
if (nowIndex === 0 && directionRef.current === rightDirec && childIndex === total - 1) {
|
|
1009
|
+
movedChildRef.current = childIndex;
|
|
1010
|
+
newTransform[childIndex] = -1 * total * dis * rtlRatio;
|
|
1011
|
+
} else if (
|
|
1012
|
+
nowIndex === total - 1 &&
|
|
1013
|
+
directionRef.current === leftDirec &&
|
|
1014
|
+
childIndex === 0
|
|
1015
|
+
) {
|
|
1016
|
+
movedChildRef.current = childIndex;
|
|
1017
|
+
newTransform[childIndex] = total * dis * rtlRatio;
|
|
1018
|
+
} else if (nowIndex >= 0 && nowIndex < total) {
|
|
1019
|
+
newTransform[childIndex] = 0;
|
|
1020
|
+
}
|
|
1021
|
+
});
|
|
1022
|
+
setTransforms(newTransform);
|
|
1023
|
+
}
|
|
1024
|
+
|
|
1025
|
+
function delayTimeout(callback: TimerHandler, timeout?: number): number {
|
|
1026
|
+
return setTimeout(callback, timeout);
|
|
1027
|
+
}
|
|
1028
|
+
|
|
1029
|
+
function handleTouchEnd(e: React.TouchEvent<HTMLDivElement>) {
|
|
1030
|
+
if (!needTouchEvent) {
|
|
1031
|
+
return;
|
|
1032
|
+
}
|
|
1033
|
+
if (onTouchEnd && onTouchEnd(e.nativeEvent, shownTotal, getShownIndex(indexRef.current))) {
|
|
1034
|
+
return;
|
|
1035
|
+
}
|
|
1036
|
+
stopPropagation && e.stopPropagation();
|
|
1037
|
+
if (!touchStartedRef.current) {
|
|
1038
|
+
return;
|
|
1039
|
+
}
|
|
1040
|
+
touchStartedRef.current = false;
|
|
1041
|
+
if (!touchMovedRef.current) {
|
|
1042
|
+
setPlayIntervalRef.current();
|
|
1043
|
+
return;
|
|
1044
|
+
}
|
|
1045
|
+
touchMovedRef.current = false;
|
|
1046
|
+
// bugfix: 回弹判断逻辑需在touchMovedRef标识重置逻辑之后,否则会在触发回弹后导致标识无法重置引起滑动判断问题
|
|
1047
|
+
// @en bugfix: The logic for the bounce judgment needs to be after the touchMovedRef reset logic, otherwise it will cause the problem of slide judgment after the bounce is triggered.
|
|
1048
|
+
if (bouncingRef.current) {
|
|
1049
|
+
bouncingRef.current = false;
|
|
1050
|
+
jumpTo(index, false);
|
|
1051
|
+
return;
|
|
1052
|
+
}
|
|
1053
|
+
if (posAdjustingRef.current || touchStoppedRef.current) {
|
|
1054
|
+
return;
|
|
1055
|
+
}
|
|
1056
|
+
const touchEndTime = new Date().getTime();
|
|
1057
|
+
const dis = Math.abs(distance);
|
|
1058
|
+
const speed = (dis / (touchEndTime - touchStartTimeRef.current)) * 1000;
|
|
1059
|
+
const maxSlice = childSize * percentToChange;
|
|
1060
|
+
const needJump = (dis > maxSlice && dis > distanceToChange) || speed > speedToChange;
|
|
1061
|
+
const comparedDis = distance * rtlRatio;
|
|
1062
|
+
if (comparedDis > 0 && needJump) {
|
|
1063
|
+
jumpTo(index - 1, false);
|
|
1064
|
+
} else if (comparedDis < 0 && needJump) {
|
|
1065
|
+
jumpTo(index + 1, false);
|
|
1066
|
+
} else {
|
|
1067
|
+
jumpTo(index, false);
|
|
1068
|
+
}
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
function getSlideTransitionStyle() {
|
|
1072
|
+
return {
|
|
1073
|
+
transitionProperty: posAdjusting ? 'transform' : 'none',
|
|
1074
|
+
transitionDuration: posAdjusting ? `${dynamicAnimateDuration}ms` : '0ms',
|
|
1075
|
+
};
|
|
1076
|
+
}
|
|
1077
|
+
|
|
1078
|
+
function getInnerStyle(): CSSProperties {
|
|
1079
|
+
const transitionStyle = getStyleWithVendor(getSlideTransitionStyle());
|
|
1080
|
+
const innerSize = childSize * total;
|
|
1081
|
+
const dis = index * childSize * -1 * rtlRatio + distance + rtlRatio * leftOffset;
|
|
1082
|
+
const min = childSize > 0 ? leftOffset : 0;
|
|
1083
|
+
const max = allowEndBlank ? dis * rtlRatio : -1 * innerSize + wrapSize - rightOffset;
|
|
1084
|
+
const minTransform = horizontalUseRtl ? -1 * max : min;
|
|
1085
|
+
const maxTransform = horizontalUseRtl ? -1 * min : max;
|
|
1086
|
+
const comparedDis = rtlRatio * distance;
|
|
1087
|
+
const noLoopDis =
|
|
1088
|
+
bounceWhenNoLoop &&
|
|
1089
|
+
((index === 0 && comparedDis > 0) || (index === total - 1 && comparedDis < 0))
|
|
1090
|
+
? Math.min(minTransform + distance, Math.max(maxTransform + distance, dis))
|
|
1091
|
+
: Math.min(minTransform, Math.max(maxTransform, dis));
|
|
1092
|
+
// noLoop时需要在最后也露出spaceBetween的空白,所以当滑到最后一个时transform向左移动相应宽度
|
|
1093
|
+
// @en When noLoop is used, the blank space of spaceBetween needs to be exposed at the end, so when sliding to the last one, the transform value moves to the left by the corresponding width
|
|
1094
|
+
const translateDis = noLoop
|
|
1095
|
+
? noLoopDis - (total > 1 && index === total - 1 ? spaceBetween : 0) * rtlRatio
|
|
1096
|
+
: dis;
|
|
1097
|
+
const initTranslateDis = -1 * index * 100;
|
|
1098
|
+
// bugfix: prop `style` did not match in ssr
|
|
1099
|
+
const transStr =
|
|
1100
|
+
childSize > 0
|
|
1101
|
+
? `${translateDis}${translateDis ? 'px' : ''}`
|
|
1102
|
+
: `${initTranslateDis}${initTranslateDis ? '%' : ''}`;
|
|
1103
|
+
if (vertical) {
|
|
1104
|
+
const translateStyle = getStyleWithVendor({
|
|
1105
|
+
transform: `translateY(${transStr}) translateZ(0)`,
|
|
1106
|
+
});
|
|
1107
|
+
return {
|
|
1108
|
+
...transitionStyle,
|
|
1109
|
+
...translateStyle,
|
|
1110
|
+
width: '100%',
|
|
1111
|
+
};
|
|
1112
|
+
}
|
|
1113
|
+
const translateStyle = getStyleWithVendor({
|
|
1114
|
+
transform: `translateX(${transStr}) translateZ(0)`,
|
|
1115
|
+
});
|
|
1116
|
+
return {
|
|
1117
|
+
width: Math.max(0, innerSize) || 'auto',
|
|
1118
|
+
height,
|
|
1119
|
+
...transitionStyle,
|
|
1120
|
+
...translateStyle,
|
|
1121
|
+
};
|
|
1122
|
+
}
|
|
1123
|
+
|
|
1124
|
+
function getCarouselStyle(): CSSProperties {
|
|
1125
|
+
if (vertical) {
|
|
1126
|
+
return {
|
|
1127
|
+
height: userSetBoxHeight,
|
|
1128
|
+
...(style || {}),
|
|
1129
|
+
};
|
|
1130
|
+
}
|
|
1131
|
+
const heightStyle =
|
|
1132
|
+
currentItemHeight && currentItemHeight !== 'auto'
|
|
1133
|
+
? {
|
|
1134
|
+
height: currentItemHeight,
|
|
1135
|
+
}
|
|
1136
|
+
: {};
|
|
1137
|
+
return {
|
|
1138
|
+
width: userSetBoxWidth,
|
|
1139
|
+
...heightStyle,
|
|
1140
|
+
...(style || {}),
|
|
1141
|
+
};
|
|
1142
|
+
}
|
|
1143
|
+
|
|
1144
|
+
// 当最前或最后的滑块通过transform手动被放置到最后或最前时,在展示层可以认为是第total个或第-1个模块
|
|
1145
|
+
// @en When the front or last slider is manually placed to the rear or the front through transform, it can be considered as the total or -1 module in the display layer
|
|
1146
|
+
function getShownChildIndex(curIndex: number) {
|
|
1147
|
+
let childIndex = curIndex;
|
|
1148
|
+
if (transforms[childIndex] * rtlRatio > 0) {
|
|
1149
|
+
childIndex = total;
|
|
1150
|
+
} else if (transforms[childIndex] * rtlRatio < 0) {
|
|
1151
|
+
childIndex = -1;
|
|
1152
|
+
}
|
|
1153
|
+
return childIndex;
|
|
1154
|
+
}
|
|
1155
|
+
|
|
1156
|
+
// 当两侧有露出内容时,为了循环衔接上需要fake前后的dom来做视觉填充
|
|
1157
|
+
// @en When there is exposed content on both sides, it is necessary to fake the front and rear dom to make visual filling in order to connect circularly.
|
|
1158
|
+
function setCurrentFakeIndex() {
|
|
1159
|
+
const newIndexes: typeof fakeIndexes = fakeIndexes.slice();
|
|
1160
|
+
if (childSize <= 0) {
|
|
1161
|
+
if (index === 0) {
|
|
1162
|
+
newIndexes[0] = { index: total - 1, side: 'left' };
|
|
1163
|
+
}
|
|
1164
|
+
if (index === total - 1) {
|
|
1165
|
+
newIndexes[2] = { index: 0, side: 'right' };
|
|
1166
|
+
}
|
|
1167
|
+
setFakeIndexes(newIndexes);
|
|
1168
|
+
return;
|
|
1169
|
+
}
|
|
1170
|
+
if (
|
|
1171
|
+
(index === 1 && direction === rightDirec) ||
|
|
1172
|
+
(index === 0 && direction !== rightDirec)
|
|
1173
|
+
) {
|
|
1174
|
+
newIndexes[0] = { index: total - 1, side: 'left' };
|
|
1175
|
+
}
|
|
1176
|
+
if ((index === 0 && direction === rightDirec) || index === -1) {
|
|
1177
|
+
newIndexes[1] = { index: total - 2, side: 'left' };
|
|
1178
|
+
}
|
|
1179
|
+
if (
|
|
1180
|
+
(index === total - 2 && direction === leftDirec) ||
|
|
1181
|
+
(index === total - 1 && direction !== leftDirec)
|
|
1182
|
+
) {
|
|
1183
|
+
newIndexes[2] = { index: 0, side: 'right' };
|
|
1184
|
+
}
|
|
1185
|
+
if ((index === total - 1 && direction === leftDirec) || index === total) {
|
|
1186
|
+
newIndexes[3] = { index: 1, side: 'right' };
|
|
1187
|
+
}
|
|
1188
|
+
setFakeIndexes(newIndexes);
|
|
1189
|
+
}
|
|
1190
|
+
|
|
1191
|
+
function getItemTranslateStyle(childIndex: number, ratio: number) {
|
|
1192
|
+
const dis = index - childIndex;
|
|
1193
|
+
const prefix = dis > 0 ? 1 : -1;
|
|
1194
|
+
const transPercent = 1 - inactiveValidScale;
|
|
1195
|
+
const ratioWithRtl = ratio * rtlRatio;
|
|
1196
|
+
let trans = 0;
|
|
1197
|
+
if (Math.abs(dis) > 1) {
|
|
1198
|
+
trans = (prefix * (Math.abs(dis) - 1) - ratioWithRtl) * transPercent * rtlRatio;
|
|
1199
|
+
} else if ((dis === -1 && ratioWithRtl > 0) || (dis === 1 && ratioWithRtl < 0)) {
|
|
1200
|
+
trans = -1 * ratio * transPercent;
|
|
1201
|
+
}
|
|
1202
|
+
return `translate${vertical ? 'Y' : 'X'}(${trans * 100}%)`;
|
|
1203
|
+
}
|
|
1204
|
+
|
|
1205
|
+
function getItemScaleStyle(childIndex: number, ratio: number) {
|
|
1206
|
+
const originScale = inactiveValidScale;
|
|
1207
|
+
// active滑块前后两侧的滑块,根据滑动距离等比放大
|
|
1208
|
+
// @en The front and rear sides of the active slider, and zoom in proportionally according to the sliding distance
|
|
1209
|
+
if (
|
|
1210
|
+
(childIndex === index - 1 && direction === rightDirec) ||
|
|
1211
|
+
(childIndex === index + 1 && direction === leftDirec)
|
|
1212
|
+
) {
|
|
1213
|
+
return originScale + (1 - originScale) * Math.abs(ratio);
|
|
1214
|
+
}
|
|
1215
|
+
// active的滑块,根据滑动距离等比缩小
|
|
1216
|
+
// @en The active slider, zoom out proportionally according to the sliding distance
|
|
1217
|
+
if (childIndex === index) {
|
|
1218
|
+
return 1 - (1 - originScale) * Math.abs(ratio);
|
|
1219
|
+
}
|
|
1220
|
+
// 其他保持inactiveScale即可
|
|
1221
|
+
// @en Others remain inactiveScale
|
|
1222
|
+
return originScale;
|
|
1223
|
+
}
|
|
1224
|
+
|
|
1225
|
+
function getItemOriginStyle(childIndex: number) {
|
|
1226
|
+
let origin = 0.5;
|
|
1227
|
+
const dis = childIndex - index;
|
|
1228
|
+
if (dis === 0) {
|
|
1229
|
+
// 当前active的卡片,随滑动方向切换origin
|
|
1230
|
+
// @en The currently active card, switch origin with the sliding direction
|
|
1231
|
+
origin =
|
|
1232
|
+
direction === 'right' || (direction === '' && lastDirectionRef.current === 'left')
|
|
1233
|
+
? 0
|
|
1234
|
+
: 1;
|
|
1235
|
+
} else if (dis <= -1) {
|
|
1236
|
+
// 当前active之前的卡片,origin均靠最右
|
|
1237
|
+
// @en The cards in front of the currently active card, the origin is on the far right
|
|
1238
|
+
origin = horizontalUseRtl ? 0 : 1;
|
|
1239
|
+
} else if (dis >= 1) {
|
|
1240
|
+
// 当前active之后的卡片,origin均靠最左
|
|
1241
|
+
// @en The cards after the currently active card, the origin is on the far left
|
|
1242
|
+
origin = horizontalUseRtl ? 1 : 0;
|
|
1243
|
+
}
|
|
1244
|
+
const originStr = `${origin * 100}%`;
|
|
1245
|
+
return vertical ? `center ${originStr}` : `${originStr} center`;
|
|
1246
|
+
}
|
|
1247
|
+
|
|
1248
|
+
function getItemInnerStyle(curIndex: number): CSSProperties {
|
|
1249
|
+
const transitionStyle = getSlideTransitionStyle();
|
|
1250
|
+
const childIndex = getShownChildIndex(curIndex);
|
|
1251
|
+
const ratio = distance / childSize || 0;
|
|
1252
|
+
// 有放大效果的item,随着distance变化而变化scale和transform-origin
|
|
1253
|
+
// @en Item with magnification effect, scale and transform-origin change with distance
|
|
1254
|
+
// transform效果与fake dom时item的transform需隔离,且scale后会影响item的padding展示
|
|
1255
|
+
// @en The transform effect of the item needs to be isolated from the fake dom, and the padding display of the item will be affected after scale.
|
|
1256
|
+
// 因此需要缩放时多包裹一层item-inner,单独处理缩放逻辑
|
|
1257
|
+
// @en Therefore, need to wrap an additional layer of item-inner when scaling, and handle the scaling logic separately
|
|
1258
|
+
const scale = getItemScaleStyle(childIndex, ratio);
|
|
1259
|
+
const transformStyle: CSSProperties = {
|
|
1260
|
+
transform: `${getItemTranslateStyle(childIndex, ratio)} scale(${scale})`,
|
|
1261
|
+
transformOrigin: getItemOriginStyle(childIndex),
|
|
1262
|
+
};
|
|
1263
|
+
return getStyleWithVendor({
|
|
1264
|
+
...transitionStyle,
|
|
1265
|
+
...transformStyle,
|
|
1266
|
+
});
|
|
1267
|
+
}
|
|
1268
|
+
|
|
1269
|
+
function getItemFakeStyle(childIndex: number): CSSProperties {
|
|
1270
|
+
const styleMap = {
|
|
1271
|
+
leftDirec: vertical ? 'top' : leftDirec,
|
|
1272
|
+
rightDirec: vertical ? 'bottom' : rightDirec,
|
|
1273
|
+
otherDirec: vertical ? 'left' : 'top',
|
|
1274
|
+
trans: vertical ? 'Y' : 'X',
|
|
1275
|
+
};
|
|
1276
|
+
return {
|
|
1277
|
+
position: 'absolute',
|
|
1278
|
+
[styleMap.otherDirec]: 0,
|
|
1279
|
+
...(childIndex >= 0
|
|
1280
|
+
? {
|
|
1281
|
+
[styleMap.leftDirec]: childSize > 0 ? '100%' : `${total * 100}%`,
|
|
1282
|
+
transform: `translate${styleMap.trans}(${
|
|
1283
|
+
(childIndex - total) * rtlRatio * 100
|
|
1284
|
+
}%)`,
|
|
1285
|
+
}
|
|
1286
|
+
: {
|
|
1287
|
+
[styleMap.rightDirec]: '100%',
|
|
1288
|
+
transform: `translate${styleMap.trans}(${
|
|
1289
|
+
(childIndex + 1) * rtlRatio * 100
|
|
1290
|
+
}%)`,
|
|
1291
|
+
}),
|
|
1292
|
+
};
|
|
1293
|
+
}
|
|
1294
|
+
|
|
1295
|
+
function getItemStyle(childIndex: number, isFake?: boolean): CSSProperties {
|
|
1296
|
+
const initialStartSpace = spaceBetween + leftOffset;
|
|
1297
|
+
const initialEndSpace = spaceBetween + rightOffset;
|
|
1298
|
+
const initalSize = vertical ? userSetBoxHeight : '100%';
|
|
1299
|
+
const horizontalPaddingStart = horizontalUseRtl ? 'paddingRight' : 'paddingLeft';
|
|
1300
|
+
const horizontalPaddingEnd = horizontalUseRtl ? 'paddingLeft' : 'paddingRight';
|
|
1301
|
+
const styleMap = {
|
|
1302
|
+
size: vertical ? 'height' : 'width',
|
|
1303
|
+
paddingStart: vertical ? 'paddingTop' : horizontalPaddingStart,
|
|
1304
|
+
paddingEnd: vertical ? 'paddingBottom' : horizontalPaddingEnd,
|
|
1305
|
+
translate: vertical ? 'translateY' : 'translateX',
|
|
1306
|
+
};
|
|
1307
|
+
// bugfix: item 为半透明状态下 fakeItem 和普通 item 重叠露馅问题
|
|
1308
|
+
// @en bugfix: Solve the problem of overlapping fakeItem and the normal item when item is translucent
|
|
1309
|
+
const transformStyle: CSSProperties = isFake
|
|
1310
|
+
? {
|
|
1311
|
+
visibility:
|
|
1312
|
+
(childIndex === total && transforms[0] > 0) ||
|
|
1313
|
+
(childIndex === -1 && transforms[total - 1] < 0)
|
|
1314
|
+
? 'hidden'
|
|
1315
|
+
: 'initial',
|
|
1316
|
+
}
|
|
1317
|
+
: {
|
|
1318
|
+
transform: `${styleMap.translate}(${transforms[childIndex] || 0}px)`,
|
|
1319
|
+
};
|
|
1320
|
+
// childSize <= 0 说明是未全部加载完成的首屏情况 做一下特殊处理
|
|
1321
|
+
// @en childSize <= 0 indicates that the first screen is not fully loaded, so do some special treatment
|
|
1322
|
+
const layoutStyle: CSSProperties =
|
|
1323
|
+
childSize > 0
|
|
1324
|
+
? {
|
|
1325
|
+
[styleMap.size]: childSize - spaceBetween,
|
|
1326
|
+
[styleMap.paddingStart]: spaceBetween,
|
|
1327
|
+
boxSizing: 'content-box',
|
|
1328
|
+
}
|
|
1329
|
+
: {
|
|
1330
|
+
[styleMap.size]: initalSize,
|
|
1331
|
+
[styleMap.paddingStart]: initialStartSpace,
|
|
1332
|
+
[styleMap.paddingEnd]: initialEndSpace,
|
|
1333
|
+
boxSizing: 'border-box',
|
|
1334
|
+
transform: `${styleMap.translate}(${
|
|
1335
|
+
childIndex === index
|
|
1336
|
+
? 0
|
|
1337
|
+
: (index - childIndex) * (spaceBetween + leftOffset + rightOffset)
|
|
1338
|
+
}px)`,
|
|
1339
|
+
};
|
|
1340
|
+
const fakeStyle: CSSProperties = isFake ? getItemFakeStyle(childIndex) : {};
|
|
1341
|
+
if (isFake && childSize < 0) {
|
|
1342
|
+
delete fakeStyle.transform;
|
|
1343
|
+
}
|
|
1344
|
+
return getStyleWithVendor({
|
|
1345
|
+
...transformStyle,
|
|
1346
|
+
...layoutStyle,
|
|
1347
|
+
...fakeStyle,
|
|
1348
|
+
});
|
|
1349
|
+
}
|
|
1350
|
+
|
|
1351
|
+
function renderChild(child: ReactNode, childIndex: number, prefix: string) {
|
|
1352
|
+
// 兼容线上逻辑,不需要切换放大效果的,不包裹item-inner
|
|
1353
|
+
// @en No need to switch the magnification effect, do not wrap item-inner
|
|
1354
|
+
return inactiveValidScale === 1 ? (
|
|
1355
|
+
child
|
|
1356
|
+
) : (
|
|
1357
|
+
<div className={`${prefix}-item-inner`} style={getItemInnerStyle(childIndex)}>
|
|
1358
|
+
{child}
|
|
1359
|
+
</div>
|
|
1360
|
+
);
|
|
1361
|
+
}
|
|
1362
|
+
|
|
1363
|
+
function renderFakeItem(prefix: string, fakeIndex: number, side: 'left' | 'right') {
|
|
1364
|
+
if (!fakeItem || fakeIndex === void 0) {
|
|
1365
|
+
return null;
|
|
1366
|
+
}
|
|
1367
|
+
const posIndex = side === 'left' ? fakeIndex - total : fakeIndex + total;
|
|
1368
|
+
return (
|
|
1369
|
+
<div
|
|
1370
|
+
key={`${fakeIndex}${posIndex}`}
|
|
1371
|
+
data-fake-index={fakeIndex}
|
|
1372
|
+
data-index={posIndex}
|
|
1373
|
+
className={cls(`${prefix}-item carousel-item fake-item fake-${side}`, { vertical })}
|
|
1374
|
+
style={getItemStyle(posIndex, true)}
|
|
1375
|
+
>
|
|
1376
|
+
{renderChild(allChildren[fakeIndex], posIndex, prefix)}
|
|
1377
|
+
</div>
|
|
1378
|
+
);
|
|
1379
|
+
}
|
|
1380
|
+
|
|
1381
|
+
function renderAllFakeItem(prefix: string) {
|
|
1382
|
+
return fakeIndexes.map(fakeIndex =>
|
|
1383
|
+
renderFakeItem(prefix, fakeIndex.index, fakeIndex.side),
|
|
1384
|
+
);
|
|
1385
|
+
}
|
|
1386
|
+
|
|
1387
|
+
function renderCarousel({ prefixCls }) {
|
|
1388
|
+
const prefix = `${prefixCls}-carousel`;
|
|
1389
|
+
return (
|
|
1390
|
+
<div className={cls(`${prefix}-wrap`, className)} style={wrapStyle} ref={domRef}>
|
|
1391
|
+
{needRefreshDom ? (
|
|
1392
|
+
<div
|
|
1393
|
+
key="fake-carousel"
|
|
1394
|
+
className={`${prefix} wrap-placeholder`}
|
|
1395
|
+
ref={fakeWrapRef}
|
|
1396
|
+
/>
|
|
1397
|
+
) : null}
|
|
1398
|
+
<div
|
|
1399
|
+
key="carousel"
|
|
1400
|
+
className={prefix}
|
|
1401
|
+
onTouchEnd={handleTouchEnd}
|
|
1402
|
+
onTouchCancel={handleTouchEnd}
|
|
1403
|
+
ref={wrapRef}
|
|
1404
|
+
style={getCarouselStyle()}
|
|
1405
|
+
>
|
|
1406
|
+
<div
|
|
1407
|
+
className={cls(`${prefix}-inner ${cubic}`, { vertical })}
|
|
1408
|
+
style={getInnerStyle()}
|
|
1409
|
+
ref={innerRef}
|
|
1410
|
+
>
|
|
1411
|
+
{allChildren.map((child, childIndex) => {
|
|
1412
|
+
const inView =
|
|
1413
|
+
lazyloadCount !== void 0
|
|
1414
|
+
? childIndex >= index - lazyloadCount &&
|
|
1415
|
+
childIndex <= index + lazyloadCount
|
|
1416
|
+
: true;
|
|
1417
|
+
return (
|
|
1418
|
+
<div
|
|
1419
|
+
key={childIndex}
|
|
1420
|
+
className={cls(`${prefix}-item carousel-item normal-item`, {
|
|
1421
|
+
active: childIndex === index,
|
|
1422
|
+
vertical,
|
|
1423
|
+
'ssr-float': childSize <= 0 && childIndex !== index,
|
|
1424
|
+
})}
|
|
1425
|
+
style={getItemStyle(childIndex)}
|
|
1426
|
+
ref={childRef => {
|
|
1427
|
+
itemsRef.current[childIndex] = childRef;
|
|
1428
|
+
}}
|
|
1429
|
+
>
|
|
1430
|
+
{inView || transforms[childIndex]
|
|
1431
|
+
? renderChild(child, childIndex, prefix)
|
|
1432
|
+
: null}
|
|
1433
|
+
</div>
|
|
1434
|
+
);
|
|
1435
|
+
})}
|
|
1436
|
+
{renderAllFakeItem(prefix)}
|
|
1437
|
+
</div>
|
|
1438
|
+
{renderExtra?.(shownIndex)}
|
|
1439
|
+
</div>
|
|
1440
|
+
{showIndicator && (total > 1 || !hideSingleIndicator) ? (
|
|
1441
|
+
<div
|
|
1442
|
+
key="carousel-indicator"
|
|
1443
|
+
className={cls(
|
|
1444
|
+
`${prefix}-indicator pos-${indicatorPos} ${prefix}-indicator-${
|
|
1445
|
+
vertical ? 'vertical' : 'horizontal'
|
|
1446
|
+
}`,
|
|
1447
|
+
{ [`vertical ver-pos-${indicatorVerticalPos}`]: vertical },
|
|
1448
|
+
{
|
|
1449
|
+
inverse:
|
|
1450
|
+
indicatorInverse === void 0
|
|
1451
|
+
? indicatorOutside
|
|
1452
|
+
: indicatorInverse,
|
|
1453
|
+
},
|
|
1454
|
+
{ outside: indicatorOutside },
|
|
1455
|
+
)}
|
|
1456
|
+
>
|
|
1457
|
+
{renderIndicator
|
|
1458
|
+
? renderIndicator(shownIndex, shownTotal, lastShownIndexRef.current)
|
|
1459
|
+
: indicatorArr.map((_, i) => (
|
|
1460
|
+
<i
|
|
1461
|
+
key={i}
|
|
1462
|
+
className={cls(
|
|
1463
|
+
'indicator',
|
|
1464
|
+
`type-${indicatorType}`,
|
|
1465
|
+
indicatorClass,
|
|
1466
|
+
{
|
|
1467
|
+
active: i === shownIndex,
|
|
1468
|
+
},
|
|
1469
|
+
)}
|
|
1470
|
+
/>
|
|
1471
|
+
))}
|
|
1472
|
+
</div>
|
|
1473
|
+
) : null}
|
|
1474
|
+
</div>
|
|
1475
|
+
);
|
|
1476
|
+
}
|
|
1477
|
+
|
|
1478
|
+
return <ContextLayout>{renderCarousel}</ContextLayout>;
|
|
1479
|
+
});
|
|
1480
|
+
|
|
1481
|
+
export default Carousel;
|