@tonar/react-kit 0.1.0-beta.2 → 0.2.7
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/dist/components.es.js +3 -0
- package/dist/css/index.css +139 -0
- package/dist/hooks.es.js +369 -0
- package/dist/index.es.js +4 -0
- package/dist/js/components.whU5kkGn.js +422 -0
- package/dist/types/components/async-custom-show/index.d.ts +14 -12
- package/dist/types/components/carousel/hooks/index.d.ts +10 -64
- package/dist/types/components/carousel/index.d.ts +61 -61
- package/dist/types/components/custom-show/index.d.ts +12 -10
- package/dist/types/components/error-boundary/index.d.ts +17 -16
- package/dist/types/components/index.d.ts +6 -8
- package/dist/types/hooks/async-guard/index.d.ts +4 -1
- package/dist/types/hooks/index.d.ts +6 -5
- package/dist/types/hooks/observer/index.d.ts +36 -34
- package/dist/types/hooks/state/index.d.ts +20 -18
- package/dist/types/hooks/storage/index.d.ts +15 -14
- package/dist/types/hooks/timer/index.d.ts +5 -2
- package/dist/types/index.d.ts +13 -2
- package/package.json +8 -11
- package/dist/components.esm.js +0 -2
- package/dist/css/components.C24nnsjt.css +0 -1
- package/dist/hooks.esm.js +0 -195
- package/dist/index.esm.js +0 -3
- package/dist/js/components.C-It6ulM.js +0 -237
|
@@ -0,0 +1,422 @@
|
|
|
1
|
+
import '../css/index.css';
|
|
2
|
+
import { useCreateSafeRef, useRAfInterval } from "../hooks.es.js";
|
|
3
|
+
import { Component, cloneElement, forwardRef, useCallback, useEffect, useImperativeHandle, useMemo, useRef, useState } from "react";
|
|
4
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
5
|
+
|
|
6
|
+
//#region src/components/carousel/hooks/index.ts
|
|
7
|
+
/**
|
|
8
|
+
* @author sonion
|
|
9
|
+
* @description 无动画切换
|
|
10
|
+
* @param {HTMLElement} el - 元素
|
|
11
|
+
* @param {number} index - 要切换的索引
|
|
12
|
+
* @returns {void}
|
|
13
|
+
*/
|
|
14
|
+
const noAnimationSwitch = (el, index) => {
|
|
15
|
+
el.style.cssText = `transition-duration: 0s; --index: ${index}`;
|
|
16
|
+
el.clientLeft;
|
|
17
|
+
el.style.cssText = "";
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* @author sonion
|
|
21
|
+
* @description 更新翻页按钮的dom操作
|
|
22
|
+
* @param {HTMLElement} wrapper - 容器元素。就是轮播子项的父元素(轮播inner)的父元素
|
|
23
|
+
* @param {Array} showArrow - 左右箭头显示状态
|
|
24
|
+
* @returns {void}
|
|
25
|
+
*/
|
|
26
|
+
const arrowDomOperation = (wrapper, showArrow) => {
|
|
27
|
+
const arrow = wrapper.previousElementSibling;
|
|
28
|
+
if (!arrow) return;
|
|
29
|
+
const [showLeft, showRight] = showArrow;
|
|
30
|
+
arrow.children[0]?.classList.toggle("show", showLeft);
|
|
31
|
+
arrow.children[1]?.classList.toggle("show", showRight);
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* @author sonion
|
|
35
|
+
* @description 每一次切换是的dom操作(更新主轮播图位置、指示器显示、翻页按钮)
|
|
36
|
+
* @param {HTMLElement} el - 元素
|
|
37
|
+
* @param {number} index - 操作索引
|
|
38
|
+
* @param {Array} showArrow - 左右箭头显示状态
|
|
39
|
+
* @returns {void}
|
|
40
|
+
*/
|
|
41
|
+
const domOperation = (el, index, showArrow) => {
|
|
42
|
+
/** 轮播主要index */
|
|
43
|
+
el.style.setProperty("--index", `${index}`);
|
|
44
|
+
const wrapper = el.parentElement;
|
|
45
|
+
if (!wrapper) return;
|
|
46
|
+
/** 设置指示器 */
|
|
47
|
+
const indicator = wrapper.parentElement?.nextElementSibling;
|
|
48
|
+
if (indicator) {
|
|
49
|
+
indicator.querySelector(".active")?.classList.remove("active");
|
|
50
|
+
indicator.children[index >= indicator.children.length ? 0 : index]?.classList.add("active");
|
|
51
|
+
}
|
|
52
|
+
/** 设置箭头 */
|
|
53
|
+
arrowDomOperation(wrapper, showArrow);
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* @author sonion
|
|
57
|
+
* @description 计算是否显示左右箭头
|
|
58
|
+
* @param {ShowArrowHookParams} params - hook参数
|
|
59
|
+
* @param {ShowArrowType} params.showArrow - 箭头类型
|
|
60
|
+
* @param {number} params.length - 轮播图数量
|
|
61
|
+
* @param {number} params.cardWidth - 轮播图宽度
|
|
62
|
+
* @param {HTMLElement} params.wrapperRef - 轮播图容器
|
|
63
|
+
* @returns {(index: number)=>[boolean, boolean]} - 计算是否显示左右箭头的函数
|
|
64
|
+
*/
|
|
65
|
+
const useShowArrow = ({ showArrow, length, cardWidth, wrapperRef }) => {
|
|
66
|
+
const displayQuantity = useMemo(() => {
|
|
67
|
+
const wrapperWidth = (wrapperRef?.parentElement?.parentElement?.clientWidth || cardWidth) ?? 0;
|
|
68
|
+
if (!cardWidth) return 1;
|
|
69
|
+
return Math.floor(wrapperWidth / cardWidth);
|
|
70
|
+
}, [wrapperRef, cardWidth]);
|
|
71
|
+
return useCallback((index) => {
|
|
72
|
+
if (showArrow !== "auto") return [true, true];
|
|
73
|
+
let isRightShowArrow;
|
|
74
|
+
if (length > displayQuantity) isRightShowArrow = index + displayQuantity - 1 < length - 1;
|
|
75
|
+
else isRightShowArrow = false;
|
|
76
|
+
return [index > 0, isRightShowArrow];
|
|
77
|
+
}, [
|
|
78
|
+
displayQuantity,
|
|
79
|
+
length,
|
|
80
|
+
showArrow
|
|
81
|
+
]);
|
|
82
|
+
};
|
|
83
|
+
/**
|
|
84
|
+
* @author sonion
|
|
85
|
+
* @description 播放轮播图控制
|
|
86
|
+
* @param {PlayControlHookParams} params - hook参数
|
|
87
|
+
* @param {number} params.length - 轮播图数量
|
|
88
|
+
* @param {boolean} params.loop - 是否无缝轮播
|
|
89
|
+
* @param {ShowArrowType} params.showArrow - 箭头类型
|
|
90
|
+
* @param {number} params.cardWidth - 轮播图宽度
|
|
91
|
+
* @param {NonLoopEndFuncType} params.onNonLoopEnd - 非循环轮播结束回调
|
|
92
|
+
* @param {number} params.offsetOnEnd - 非循环轮播结束时的偏移量
|
|
93
|
+
*/
|
|
94
|
+
const usePlayControl = ({ length, loop, showArrow, cardWidth, onNonLoopEnd, offsetOnEnd = 0 }) => {
|
|
95
|
+
const FIRST = 0;
|
|
96
|
+
const LAST = length;
|
|
97
|
+
const indexRef = useRef(FIRST);
|
|
98
|
+
const [ref, setRef, isReadyRef] = useCreateSafeRef();
|
|
99
|
+
offsetOnEnd = ~~offsetOnEnd;
|
|
100
|
+
offsetOnEnd = offsetOnEnd < 0 || offsetOnEnd >= length ? 0 : offsetOnEnd;
|
|
101
|
+
const getShowArrow = useShowArrow({
|
|
102
|
+
showArrow,
|
|
103
|
+
length,
|
|
104
|
+
cardWidth,
|
|
105
|
+
wrapperRef: ref
|
|
106
|
+
});
|
|
107
|
+
useEffect(() => {
|
|
108
|
+
const wrapper = ref?.parentElement;
|
|
109
|
+
if (showArrow !== "auto" || !wrapper) return;
|
|
110
|
+
const arrowStatus = getShowArrow(indexRef.current);
|
|
111
|
+
arrowDomOperation(wrapper, arrowStatus);
|
|
112
|
+
}, [length]);
|
|
113
|
+
const stepChange = useCallback((direction) => {
|
|
114
|
+
if (!ref) return;
|
|
115
|
+
if (loop) {
|
|
116
|
+
if (indexRef.current >= LAST && direction !== "prev") {
|
|
117
|
+
indexRef.current = 0;
|
|
118
|
+
noAnimationSwitch(ref, indexRef.current);
|
|
119
|
+
} else if (indexRef.current <= FIRST && direction === "prev") {
|
|
120
|
+
indexRef.current = LAST;
|
|
121
|
+
noAnimationSwitch(ref, indexRef.current);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
direction === "prev" ? indexRef.current-- : indexRef.current++;
|
|
125
|
+
const arrowStatus = getShowArrow(indexRef.current);
|
|
126
|
+
domOperation(ref, indexRef.current, arrowStatus);
|
|
127
|
+
if (!loop && onNonLoopEnd) indexRef.current === length - offsetOnEnd - 1 && onNonLoopEnd(direction ?? "next", indexRef.current, length, offsetOnEnd);
|
|
128
|
+
}, [
|
|
129
|
+
length,
|
|
130
|
+
loop,
|
|
131
|
+
ref,
|
|
132
|
+
getShowArrow,
|
|
133
|
+
offsetOnEnd,
|
|
134
|
+
onNonLoopEnd,
|
|
135
|
+
LAST
|
|
136
|
+
]);
|
|
137
|
+
const jumpChange = useCallback((index) => {
|
|
138
|
+
const originalIndex = indexRef.current;
|
|
139
|
+
indexRef.current = index < 0 ? 0 : index > length ? length : index;
|
|
140
|
+
const arrowStatus = getShowArrow(indexRef.current);
|
|
141
|
+
ref && domOperation(ref, indexRef.current, arrowStatus);
|
|
142
|
+
if (!loop && onNonLoopEnd) {
|
|
143
|
+
const direction = originalIndex < indexRef.current ? "next" : "prev";
|
|
144
|
+
const _triggerIndex = length - offsetOnEnd - 1;
|
|
145
|
+
(indexRef.current >= _triggerIndex && originalIndex < _triggerIndex || indexRef.current <= _triggerIndex && originalIndex > _triggerIndex) && onNonLoopEnd(direction, indexRef.current, length, offsetOnEnd);
|
|
146
|
+
}
|
|
147
|
+
}, [
|
|
148
|
+
length,
|
|
149
|
+
loop,
|
|
150
|
+
ref,
|
|
151
|
+
getShowArrow,
|
|
152
|
+
offsetOnEnd,
|
|
153
|
+
onNonLoopEnd
|
|
154
|
+
]);
|
|
155
|
+
const getCurrentIndex = useCallback(() => indexRef.current, []);
|
|
156
|
+
useEffect(() => jumpChange(indexRef.current), [ref]);
|
|
157
|
+
return [
|
|
158
|
+
setRef,
|
|
159
|
+
stepChange,
|
|
160
|
+
jumpChange,
|
|
161
|
+
getCurrentIndex,
|
|
162
|
+
isReadyRef
|
|
163
|
+
];
|
|
164
|
+
};
|
|
165
|
+
/**
|
|
166
|
+
* @author sonion
|
|
167
|
+
* @description 创建自增id函数
|
|
168
|
+
* @returns {()=>string} - 自增id函数
|
|
169
|
+
*/
|
|
170
|
+
const createAutoIncrementId = () => {
|
|
171
|
+
const fix = Math.random().toString(36).slice(2);
|
|
172
|
+
let id = 0;
|
|
173
|
+
return () => `${fix}-${++id}`;
|
|
174
|
+
};
|
|
175
|
+
/** 自增id函数 */
|
|
176
|
+
const autoIncrementId = createAutoIncrementId();
|
|
177
|
+
/**
|
|
178
|
+
* @author sonion
|
|
179
|
+
* @description 创建无缝轮播结构
|
|
180
|
+
* @param {ReactElement} children - 轮播列表
|
|
181
|
+
* @param {boolean} loop - 是否无缝轮播
|
|
182
|
+
* @param {RerenderType} rerender - 子元素重渲染的依赖类型
|
|
183
|
+
* @returns {ReactElement[]} - 无缝轮播结构
|
|
184
|
+
*/
|
|
185
|
+
const useLoopChildren = (children, loop, rerender) => useMemo(() => {
|
|
186
|
+
if (!Array.isArray(children) || !children.length) throw new Error("Carousel children must be an array");
|
|
187
|
+
if (loop) {
|
|
188
|
+
const first = children[0];
|
|
189
|
+
const clonedChildren = [...children];
|
|
190
|
+
clonedChildren.push(cloneElement(first, { key: autoIncrementId() }));
|
|
191
|
+
return clonedChildren;
|
|
192
|
+
}
|
|
193
|
+
return children;
|
|
194
|
+
}, [rerender === "child" ? children : children?.length, loop]);
|
|
195
|
+
|
|
196
|
+
//#endregion
|
|
197
|
+
//#region src/components/carousel/index.module.scss
|
|
198
|
+
var index_module_default = {
|
|
199
|
+
"arrow": "arrow__5vdU-W",
|
|
200
|
+
"carousel": "carousel__5vdU-W",
|
|
201
|
+
"indicator": "indicator__5vdU-W",
|
|
202
|
+
"inner": "inner__5vdU-W",
|
|
203
|
+
"left": "left__5vdU-W",
|
|
204
|
+
"right": "right__5vdU-W",
|
|
205
|
+
"wrapper": "wrapper__5vdU-W"
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
//#endregion
|
|
209
|
+
//#region src/components/carousel/index.tsx
|
|
210
|
+
/**
|
|
211
|
+
* @author sonion
|
|
212
|
+
* @description 轮播组件,用于展示多个轮播项
|
|
213
|
+
* @param {CarouselProps} props - 组件的属性
|
|
214
|
+
* @param {number} [props.cardWidth] - 单个轮播项的宽度
|
|
215
|
+
* @param {number} [props.cardHeight] - 单个轮播项的高度
|
|
216
|
+
* @param {number} [props.wrapperWidth] - 轮播容器的宽度
|
|
217
|
+
* @param {number} [props.wrapperHeight] - 轮播容器的高度
|
|
218
|
+
* @param {number} [props.gapSize] - 轮播项之间的间隔
|
|
219
|
+
* @param {boolean} [props.loop] - 是否无缝循环
|
|
220
|
+
* @param {number} [props.duration=6000] - 切换时间(毫秒)
|
|
221
|
+
* @param {boolean} [props.autoPlay=true] - 是否自动播放
|
|
222
|
+
* @param {ShowArrowType} [props.showArrow='always'] - 箭头类型
|
|
223
|
+
* @param {IndicatorType} [props.indicatorType='line'] - 指示器类型
|
|
224
|
+
* @param {string} [props.className] - 主轮播类名,不包括指示器
|
|
225
|
+
* @param {CSSProperties} [props.style] - 主轮播样式,不包括指示器
|
|
226
|
+
* @param {ReactElement[]} children - 轮播项
|
|
227
|
+
* @param {RerenderType} [props.rerender='length'] - 轮播项更新依赖方式
|
|
228
|
+
* @param {(direction: 'prev' | 'next', current: number, total: number, offset: number) => void} [props.onNonLoopEnd] - 非循环模式下,轮播结束回调。
|
|
229
|
+
* @param {number} [props.offsetOnEnd=0] - 非循环模式下,轮播结束时的偏移量。就是提前第几张就算结束。暂不支持延后。
|
|
230
|
+
* @param {[FC<{ onClick: () => void; className?: string }>, FC<{ onClick: () => void; className?: string }>]} [props.arrows] - 自定义渲染箭头
|
|
231
|
+
*/
|
|
232
|
+
const Carousel = forwardRef(({ cardWidth, cardHeight, wrapperWidth, wrapperHeight, gapSize, loop = true, duration = 6e3, autoPlay = true, showArrow = "always", indicatorType = "line", className, style, children, rerender = "length", onNonLoopEnd, offsetOnEnd = 0, arrows }, ref) => {
|
|
233
|
+
const list = useLoopChildren(children, loop, rerender);
|
|
234
|
+
const [setBannerRef, stepChange, jumpChange, getCurrentIndex] = usePlayControl({
|
|
235
|
+
length: children.length,
|
|
236
|
+
loop,
|
|
237
|
+
showArrow,
|
|
238
|
+
cardWidth,
|
|
239
|
+
onNonLoopEnd,
|
|
240
|
+
offsetOnEnd
|
|
241
|
+
});
|
|
242
|
+
const [run, stop] = useRAfInterval(stepChange, duration);
|
|
243
|
+
useEffect(() => {
|
|
244
|
+
if (autoPlay) run();
|
|
245
|
+
return stop;
|
|
246
|
+
}, [
|
|
247
|
+
run,
|
|
248
|
+
stop,
|
|
249
|
+
autoPlay
|
|
250
|
+
]);
|
|
251
|
+
useImperativeHandle(ref, () => ({
|
|
252
|
+
run,
|
|
253
|
+
stop,
|
|
254
|
+
stepChange,
|
|
255
|
+
jumpChange,
|
|
256
|
+
getCurrentIndex
|
|
257
|
+
}), [
|
|
258
|
+
run,
|
|
259
|
+
stop,
|
|
260
|
+
stepChange,
|
|
261
|
+
jumpChange,
|
|
262
|
+
getCurrentIndex
|
|
263
|
+
]);
|
|
264
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsxs("div", {
|
|
265
|
+
className: `${index_module_default.carousel} ${className ?? ""}`,
|
|
266
|
+
style: {
|
|
267
|
+
...style,
|
|
268
|
+
"--wrapperWidth": wrapperWidth,
|
|
269
|
+
"--wrapperHeight": wrapperHeight,
|
|
270
|
+
"--cardWidth": cardWidth,
|
|
271
|
+
"--cardHeight": cardHeight,
|
|
272
|
+
"--gapSize": gapSize
|
|
273
|
+
},
|
|
274
|
+
onMouseEnter: stop,
|
|
275
|
+
onMouseLeave: autoPlay ? run : void 0,
|
|
276
|
+
children: [/* @__PURE__ */ jsxs("div", {
|
|
277
|
+
className: `${index_module_default.arrow} ${showArrow}`,
|
|
278
|
+
children: [arrows?.[0] ? arrows[0]({
|
|
279
|
+
onClick: () => stepChange("prev"),
|
|
280
|
+
className: index_module_default.left
|
|
281
|
+
}) : /* @__PURE__ */ jsx("i", {
|
|
282
|
+
className: index_module_default.left,
|
|
283
|
+
onClick: () => stepChange("prev"),
|
|
284
|
+
onKeyDown: (e) => e.key === "ArrowLeft" && stepChange("prev")
|
|
285
|
+
}), arrows?.[1] ? arrows[1]({
|
|
286
|
+
onClick: () => stepChange(),
|
|
287
|
+
className: index_module_default.right
|
|
288
|
+
}) : /* @__PURE__ */ jsx("i", {
|
|
289
|
+
className: index_module_default.right,
|
|
290
|
+
onClick: () => stepChange(),
|
|
291
|
+
onKeyDown: (e) => e.key === "ArrowRight" && stepChange()
|
|
292
|
+
})]
|
|
293
|
+
}), /* @__PURE__ */ jsx("div", {
|
|
294
|
+
className: index_module_default.wrapper,
|
|
295
|
+
children: /* @__PURE__ */ jsx("div", {
|
|
296
|
+
className: index_module_default.inner,
|
|
297
|
+
ref: setBannerRef,
|
|
298
|
+
children: list
|
|
299
|
+
})
|
|
300
|
+
})]
|
|
301
|
+
}), /* @__PURE__ */ jsx("div", {
|
|
302
|
+
className: `${index_module_default.indicator} ${indicatorType}`,
|
|
303
|
+
children: children.map((_, i) => /* @__PURE__ */ jsx("i", {
|
|
304
|
+
onClick: (e) => {
|
|
305
|
+
e.stopPropagation();
|
|
306
|
+
jumpChange(i);
|
|
307
|
+
},
|
|
308
|
+
onKeyDown: (e) => e.key === String(i) && jumpChange(i)
|
|
309
|
+
}, i))
|
|
310
|
+
})] });
|
|
311
|
+
});
|
|
312
|
+
Carousel.displayName = "Carousel";
|
|
313
|
+
|
|
314
|
+
//#endregion
|
|
315
|
+
//#region src/components/custom-show/index.tsx
|
|
316
|
+
/**
|
|
317
|
+
* @author sonion
|
|
318
|
+
* @description 根据条件渲染内容,替代用三目运算和短路规则条件渲染
|
|
319
|
+
* @param {CustomShowProps<T>} props 组件属性
|
|
320
|
+
* @param {T | null | undefined | false} props.when 条件
|
|
321
|
+
* @param {ReactNode} props.fallback 不满足条件时的渲染内容
|
|
322
|
+
* @param {(value: T | null | undefined | false) => ReactNode} props.children 满足条件时的渲染内容
|
|
323
|
+
* @returns {JSX.Element} - 组件节点
|
|
324
|
+
*/
|
|
325
|
+
function CustomShow({ when, fallback = void 0, children }) {
|
|
326
|
+
return /* @__PURE__ */ jsx(Fragment, { children: when ? children : fallback });
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
//#endregion
|
|
330
|
+
//#region src/components/async-custom-show/index.tsx
|
|
331
|
+
/**
|
|
332
|
+
* @author sonion
|
|
333
|
+
* @description 异步展示组件
|
|
334
|
+
* @param {AsyncCustomShowProps<T>} props - 组件属性
|
|
335
|
+
* @param {Promise<T> | undefined | null | false} props.when 异步判断 Promise
|
|
336
|
+
* @param {ReactNode} [props.fallback] 异步失败时的展示
|
|
337
|
+
* @param {(value: T | undefined | null | false) => ReactNode} props.children 异步成功时的展示
|
|
338
|
+
*/
|
|
339
|
+
function AsyncCustomShow({ when, fallback, children }) {
|
|
340
|
+
const [show, setShow] = useState(void 0);
|
|
341
|
+
useEffect(() => {
|
|
342
|
+
if (!when) return;
|
|
343
|
+
let active = true;
|
|
344
|
+
Promise.resolve(when).then((value) => active && setShow(value));
|
|
345
|
+
return () => void (active = false);
|
|
346
|
+
}, [when]);
|
|
347
|
+
return /* @__PURE__ */ jsx(Fragment, { children: show ? children(show) : fallback });
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
//#endregion
|
|
351
|
+
//#region \0@oxc-project+runtime@0.140.0/helpers/esm/typeof.js
|
|
352
|
+
function _typeof(o) {
|
|
353
|
+
"@babel/helpers - typeof";
|
|
354
|
+
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
|
|
355
|
+
return typeof o;
|
|
356
|
+
} : function(o) {
|
|
357
|
+
return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
|
|
358
|
+
}, _typeof(o);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
//#endregion
|
|
362
|
+
//#region \0@oxc-project+runtime@0.140.0/helpers/esm/toPrimitive.js
|
|
363
|
+
function toPrimitive(t, r) {
|
|
364
|
+
if ("object" != _typeof(t) || !t) return t;
|
|
365
|
+
var e = t[Symbol.toPrimitive];
|
|
366
|
+
if (void 0 !== e) {
|
|
367
|
+
var i = e.call(t, r || "default");
|
|
368
|
+
if ("object" != _typeof(i)) return i;
|
|
369
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
370
|
+
}
|
|
371
|
+
return ("string" === r ? String : Number)(t);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
//#endregion
|
|
375
|
+
//#region \0@oxc-project+runtime@0.140.0/helpers/esm/toPropertyKey.js
|
|
376
|
+
function toPropertyKey(t) {
|
|
377
|
+
var i = toPrimitive(t, "string");
|
|
378
|
+
return "symbol" == _typeof(i) ? i : i + "";
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
//#endregion
|
|
382
|
+
//#region \0@oxc-project+runtime@0.140.0/helpers/esm/defineProperty.js
|
|
383
|
+
function _defineProperty(e, r, t) {
|
|
384
|
+
return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
385
|
+
value: t,
|
|
386
|
+
enumerable: !0,
|
|
387
|
+
configurable: !0,
|
|
388
|
+
writable: !0
|
|
389
|
+
}) : e[r] = t, e;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
//#endregion
|
|
393
|
+
//#region src/components/error-boundary/index.tsx
|
|
394
|
+
/**
|
|
395
|
+
* @author sonion
|
|
396
|
+
* @description 错误边界组件,用于捕获子组件的错误并显示自定义的回退内容
|
|
397
|
+
* @param {ErrorBoundaryProps} props - 组件的属性
|
|
398
|
+
* @param {ReactNode} props.fallback - 错误发生时显示的回退内容
|
|
399
|
+
* @param {string} [props.msg='组件加载失败'] - 错误信息的提示文本
|
|
400
|
+
* @param {ReactNode} props.children - 子组件,可能会触发错误
|
|
401
|
+
* @returns {import('react').JSX.Element} - 组件节点
|
|
402
|
+
*/
|
|
403
|
+
var ErrorBoundary = class extends Component {
|
|
404
|
+
constructor(..._args) {
|
|
405
|
+
super(..._args);
|
|
406
|
+
_defineProperty(this, "state", { hasError: false });
|
|
407
|
+
_defineProperty(this, "msg", this.props.msg ?? "组件加载失败");
|
|
408
|
+
}
|
|
409
|
+
static getDerivedStateFromError() {
|
|
410
|
+
return { hasError: true };
|
|
411
|
+
}
|
|
412
|
+
componentDidCatch(error, errorInfo) {
|
|
413
|
+
console.error(`${this.msg}:`, error, errorInfo);
|
|
414
|
+
}
|
|
415
|
+
render() {
|
|
416
|
+
if (this.state.hasError) return this.props.fallback;
|
|
417
|
+
return this.props.children;
|
|
418
|
+
}
|
|
419
|
+
};
|
|
420
|
+
|
|
421
|
+
//#endregion
|
|
422
|
+
export { Carousel as i, AsyncCustomShow as n, CustomShow as r, ErrorBoundary as t };
|
|
@@ -1,14 +1,15 @@
|
|
|
1
|
-
import { FC,
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
import { FC, JSX, ReactNode } from "react";
|
|
2
|
+
//#region src/components/async-custom-show/index.d.ts
|
|
3
|
+
type AsyncCustomShowProps<T> = {
|
|
4
|
+
/** 异步判断 Promise */
|
|
5
|
+
when: Promise<T> | undefined | null | false;
|
|
6
|
+
/** 异步失败时的展示 */
|
|
7
|
+
fallback?: ReactNode;
|
|
8
|
+
/** 异步成功时的展示 */
|
|
9
|
+
children: (value: T | undefined | null | false) => ReactNode;
|
|
9
10
|
};
|
|
10
|
-
|
|
11
|
-
|
|
11
|
+
interface AsyncCustomShowType<T> extends FC<AsyncCustomShowProps<T>> {
|
|
12
|
+
(props: AsyncCustomShowProps<T>): JSX.Element;
|
|
12
13
|
}
|
|
13
14
|
/**
|
|
14
15
|
* @author sonion
|
|
@@ -18,5 +19,6 @@ export interface AsyncCustomShowType<T> extends FC<AsyncCustomShowProps<T>> {
|
|
|
18
19
|
* @param {ReactNode} [props.fallback] 异步失败时的展示
|
|
19
20
|
* @param {(value: T | undefined | null | false) => ReactNode} props.children 异步成功时的展示
|
|
20
21
|
*/
|
|
21
|
-
declare function AsyncCustomShow<T>({ when, fallback, children
|
|
22
|
-
|
|
22
|
+
declare function AsyncCustomShow<T>({ when, fallback, children }: AsyncCustomShowProps<T>): JSX.Element;
|
|
23
|
+
//#endregion
|
|
24
|
+
export { AsyncCustomShowProps, AsyncCustomShowType, AsyncCustomShow as default };
|
|
@@ -1,74 +1,20 @@
|
|
|
1
|
-
import { ReactElement } from
|
|
2
|
-
|
|
3
|
-
type
|
|
4
|
-
|
|
5
|
-
showArrow: ShowArrowType;
|
|
6
|
-
/** 轮播图数量 */
|
|
7
|
-
length: number;
|
|
8
|
-
/** 轮播图宽度 */
|
|
9
|
-
cardWidth?: number;
|
|
10
|
-
/** 轮播图容器 */
|
|
11
|
-
wrapperRef?: HTMLElement;
|
|
12
|
-
};
|
|
13
|
-
/**
|
|
14
|
-
* @author sonion
|
|
15
|
-
* @description 计算是否显示左右箭头
|
|
16
|
-
* @param {ShowArrowHookParams} params - hook参数
|
|
17
|
-
* @param {ShowArrowType} params.showArrow - 箭头类型
|
|
18
|
-
* @param {number} params.length - 轮播图数量
|
|
19
|
-
* @param {number} params.cardWidth - 轮播图宽度
|
|
20
|
-
* @param {HTMLElement} params.wrapperRef - 轮播图容器
|
|
21
|
-
* @returns {(index: number)=>[boolean, boolean]} - 计算是否显示左右箭头的函数
|
|
22
|
-
*/
|
|
23
|
-
export declare const useShowArrow: ({ showArrow, length, cardWidth, wrapperRef, }: ShowArrowHookParams) => (index: number) => [boolean, boolean];
|
|
24
|
-
export type NonLoopEndFuncType = (
|
|
1
|
+
import { ReactElement } from "react";
|
|
2
|
+
//#region src/components/carousel/hooks/index.d.ts
|
|
3
|
+
type ShowArrowType = 'always' | 'auto' | 'hover' | 'none';
|
|
4
|
+
type NonLoopEndFuncType = (
|
|
25
5
|
/** 切换方向 */
|
|
26
|
-
direction: 'prev' | 'next',
|
|
6
|
+
direction: 'prev' | 'next',
|
|
27
7
|
/** 当前索引 */
|
|
28
|
-
current: number,
|
|
8
|
+
current: number,
|
|
29
9
|
/** 轮播图数量 */
|
|
30
|
-
total: number,
|
|
10
|
+
total: number,
|
|
31
11
|
/** 非循环轮播结束时的偏移量 */
|
|
32
12
|
offset: number) => void;
|
|
33
|
-
type PlayControlHookParams = {
|
|
34
|
-
/** 轮播图数量 */
|
|
35
|
-
length: number;
|
|
36
|
-
/** 是否无缝轮播 */
|
|
37
|
-
loop: boolean;
|
|
38
|
-
/** 箭头类型 */
|
|
39
|
-
showArrow: ShowArrowType;
|
|
40
|
-
/** 轮播图宽度 */
|
|
41
|
-
cardWidth?: number;
|
|
42
|
-
/** 非循环轮播结束时的回调 */
|
|
43
|
-
onNonLoopEnd?: NonLoopEndFuncType;
|
|
44
|
-
/** 非循环轮播结束时的偏移量 */
|
|
45
|
-
offsetOnEnd?: number;
|
|
46
|
-
};
|
|
47
|
-
/**
|
|
48
|
-
* @author sonion
|
|
49
|
-
* @description 播放轮播图控制
|
|
50
|
-
* @param {PlayControlHookParams} params - hook参数
|
|
51
|
-
* @param {number} params.length - 轮播图数量
|
|
52
|
-
* @param {boolean} params.loop - 是否无缝轮播
|
|
53
|
-
* @param {ShowArrowType} params.showArrow - 箭头类型
|
|
54
|
-
* @param {number} params.cardWidth - 轮播图宽度
|
|
55
|
-
* @param {NonLoopEndFuncType} params.onNonLoopEnd - 非循环轮播结束回调
|
|
56
|
-
* @param {number} params.offsetOnEnd - 非循环轮播结束时的偏移量
|
|
57
|
-
*/
|
|
58
|
-
export declare const usePlayControl: ({ length, loop, showArrow, cardWidth, onNonLoopEnd, offsetOnEnd, }: PlayControlHookParams) => readonly [(node: HTMLElement | null) => void, (direction?: "prev" | "next") => void, (index: number) => void, () => number, import('react').MutableRefObject<boolean>];
|
|
59
13
|
/**
|
|
60
14
|
* 子元素重渲染的依赖的类型
|
|
61
15
|
* length 依赖 children的数量
|
|
62
16
|
* child 依赖 children本身的变化
|
|
63
17
|
*/
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
* @description 创建无缝轮播结构
|
|
68
|
-
* @param {ReactElement} children - 轮播列表
|
|
69
|
-
* @param {boolean} loop - 是否无缝轮播
|
|
70
|
-
* @param {RerenderType} rerender - 子元素重渲染的依赖类型
|
|
71
|
-
* @returns {ReactElement[]} - 无缝轮播结构
|
|
72
|
-
*/
|
|
73
|
-
export declare const useLoopChildren: (children: ReactElement[], loop: boolean, rerender: RerenderType) => ReactElement<any, string | import('react').JSXElementConstructor<any>>[];
|
|
74
|
-
export {};
|
|
18
|
+
type RerenderType = 'length' | 'child';
|
|
19
|
+
//#endregion
|
|
20
|
+
export { NonLoopEndFuncType, RerenderType, ShowArrowType };
|
|
@@ -1,64 +1,64 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { NonLoopEndFuncType, RerenderType, ShowArrowType } from "./hooks/index.js";
|
|
2
|
+
import { CSSProperties, FC, ReactElement } from "react";
|
|
3
|
+
//#region src/components/carousel/index.d.ts
|
|
3
4
|
type IndicatorType = 'line' | 'dot' | 'none';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
5
|
+
type CarouselProps = {
|
|
6
|
+
/** 单个轮播项的宽度 */
|
|
7
|
+
cardWidth?: number;
|
|
8
|
+
/** 单个轮播项的高度 */
|
|
9
|
+
cardHeight?: number;
|
|
10
|
+
/** 轮播容器的宽度 */
|
|
11
|
+
wrapperWidth?: number;
|
|
12
|
+
/** 轮播容器的高度 */
|
|
13
|
+
wrapperHeight?: number;
|
|
14
|
+
/** 轮播项之间的间隔 */
|
|
15
|
+
gapSize?: number;
|
|
16
|
+
/** 是否无缝轮播 */
|
|
17
|
+
loop?: boolean;
|
|
18
|
+
/** 切换时间(毫秒) */
|
|
19
|
+
duration?: number;
|
|
20
|
+
/** 是否自动播放 */
|
|
21
|
+
autoPlay?: boolean;
|
|
22
|
+
/** 箭头类型 */
|
|
23
|
+
showArrow?: ShowArrowType;
|
|
24
|
+
/** 指示器类型 */
|
|
25
|
+
indicatorType?: IndicatorType;
|
|
26
|
+
/** 主轮播类名,不包括指示器 */
|
|
27
|
+
className?: string;
|
|
28
|
+
/** 主轮播样式,不包括指示器 */
|
|
29
|
+
style?: CSSProperties;
|
|
30
|
+
/** 轮播项 */
|
|
31
|
+
children: ReactElement[];
|
|
32
|
+
/** 轮播项更新依赖方式 */
|
|
33
|
+
rerender?: RerenderType;
|
|
34
|
+
/** 非循环模式下,轮播结束回调 */
|
|
35
|
+
onNonLoopEnd?: NonLoopEndFuncType;
|
|
36
|
+
/** 非循环模式下,轮播结束时的偏移量。就是提前第几张就算结束。暂不支持延后。 */
|
|
37
|
+
offsetOnEnd?: number;
|
|
38
|
+
/** 自定义渲染箭头 */
|
|
39
|
+
arrows?: [
|
|
40
|
+
/** 自定义渲染上一个箭头 */
|
|
41
|
+
FC<{
|
|
42
|
+
onClick: () => void;
|
|
26
43
|
className?: string;
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
/** 非循环模式下,轮播结束回调 */
|
|
34
|
-
onNonLoopEnd?: NonLoopEndFuncType;
|
|
35
|
-
/** 非循环模式下,轮播结束时的偏移量。就是提前第几张就算结束。暂不支持延后。 */
|
|
36
|
-
offsetOnEnd?: number;
|
|
37
|
-
/** 自定义渲染箭头 */
|
|
38
|
-
arrows?: [
|
|
39
|
-
/** 自定义渲染上一个箭头 */
|
|
40
|
-
FC<{
|
|
41
|
-
onClick: () => void;
|
|
42
|
-
className?: string;
|
|
43
|
-
}>,
|
|
44
|
-
/** 自定义渲染下一个箭头 */
|
|
45
|
-
FC<{
|
|
46
|
-
onClick: () => void;
|
|
47
|
-
className?: string;
|
|
48
|
-
}>
|
|
49
|
-
];
|
|
44
|
+
}>,
|
|
45
|
+
/** 自定义渲染下一个箭头 */
|
|
46
|
+
FC<{
|
|
47
|
+
onClick: () => void;
|
|
48
|
+
className?: string;
|
|
49
|
+
}>];
|
|
50
50
|
};
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
51
|
+
type CarouselRef = {
|
|
52
|
+
/** 开始播放 */
|
|
53
|
+
run: () => void;
|
|
54
|
+
/** 停止播放 */
|
|
55
|
+
stop: () => void;
|
|
56
|
+
/** 切换到上一个或下一个 */
|
|
57
|
+
stepChange: (arg: 'prev' | 'next') => void;
|
|
58
|
+
/** 跳转到指定索引 */
|
|
59
|
+
jumpChange: (arg: number) => void;
|
|
60
|
+
/** 获取当前索引 */
|
|
61
|
+
getCurrentIndex: () => number;
|
|
62
62
|
};
|
|
63
63
|
/**
|
|
64
64
|
* @author sonion
|
|
@@ -82,7 +82,7 @@ export type CarouselRef = {
|
|
|
82
82
|
* @param {number} [props.offsetOnEnd=0] - 非循环模式下,轮播结束时的偏移量。就是提前第几张就算结束。暂不支持延后。
|
|
83
83
|
* @param {[FC<{ onClick: () => void; className?: string }>, FC<{ onClick: () => void; className?: string }>]} [props.arrows] - 自定义渲染箭头
|
|
84
84
|
*/
|
|
85
|
-
declare const Carousel: import(
|
|
85
|
+
declare const Carousel: import("react").ForwardRefExoticComponent<CarouselProps & import("react").RefAttributes<CarouselRef>>;
|
|
86
86
|
type CarouselType = typeof Carousel;
|
|
87
|
-
|
|
88
|
-
export default
|
|
87
|
+
//#endregion
|
|
88
|
+
export { CarouselProps, CarouselRef, type CarouselType, type RerenderType, type ShowArrowType, Carousel as default };
|