@hsu-react/ui 0.0.17 → 0.0.19
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/es/components/Chart/Bar/index.js +21 -6
- package/es/components/Chart/Line/index.js +21 -6
- package/es/components/Chart/_utils/autoScrollLegend.d.ts +5 -0
- package/es/components/Chart/_utils/autoScrollLegend.js +16 -6
- package/es/components/FilePreview/_components/XlsxPreviewPane/index.d.ts +16 -0
- package/es/components/FilePreview/_components/XlsxPreviewPane/index.js +27 -0
- package/es/components/FilePreview/index.js +52 -23
- package/es/components/FormItem/FormInput/FormPasswordStrength/index.js +43 -4
- package/es/components/FormItem/index.d.ts +2 -2
- package/es/components/FormItem/index.js +46 -5
- package/es/components/SecondConf/index.d.ts +13 -2
- package/es/components/SecondConf/index.js +66 -26
- package/lib/components/Chart/Bar/index.js +18 -3
- package/lib/components/Chart/Line/index.js +18 -3
- package/lib/components/Chart/_utils/autoScrollLegend.d.ts +5 -0
- package/lib/components/Chart/_utils/autoScrollLegend.js +14 -6
- package/lib/components/FilePreview/_components/XlsxPreviewPane/index.d.ts +16 -0
- package/lib/components/FilePreview/_components/XlsxPreviewPane/index.js +35 -0
- package/lib/components/FilePreview/index.js +47 -23
- package/lib/components/FormItem/FormInput/FormPasswordStrength/index.js +37 -5
- package/lib/components/FormItem/index.d.ts +2 -2
- package/lib/components/FormItem/index.js +43 -7
- package/lib/components/SecondConf/index.d.ts +13 -2
- package/lib/components/SecondConf/index.js +55 -24
- package/package.json +4 -3
- package/src/components/Chart/Bar/index.tsx +26 -6
- package/src/components/Chart/Line/index.tsx +26 -6
- package/src/components/Chart/_utils/autoScrollLegend.ts +17 -3
- package/src/components/FilePreview/_components/XlsxPreviewPane/index.tsx +33 -0
- package/src/components/FilePreview/index.md +11 -0
- package/src/components/FilePreview/index.tsx +46 -19
- package/src/components/FormItem/FormInput/FormPasswordStrength/index.tsx +43 -4
- package/src/components/FormItem/index.md +19 -0
- package/src/components/FormItem/index.tsx +44 -5
- package/src/components/SecondConf/index.md +42 -0
- package/src/components/SecondConf/index.tsx +72 -22
|
@@ -280,14 +280,28 @@ const ChartBar: React.FC<ChartBarProps> = (props) => {
|
|
|
280
280
|
// Set the option: notMerge resets the dataZoom window to its initial position,
|
|
281
281
|
// so when x-axis data is unchanged (e.g. re-render triggered by axis recalculation / legend selection), record the current window first and restore it afterwards
|
|
282
282
|
const xAxisKey = xAxisData?.join("\u0001") ?? "";
|
|
283
|
-
const
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
283
|
+
const sameXAxis = prevXAxisKeyRef.current === xAxisKey;
|
|
284
|
+
const prevZoomRanges = sameXAxis
|
|
285
|
+
? ((chart.getOption() as ChartsOption | undefined)?.dataZoom as
|
|
286
|
+
| Array<{ start?: number; end?: number }>
|
|
287
|
+
| undefined)
|
|
288
|
+
: undefined;
|
|
289
|
+
// 滚动图例的翻页位置同样会被 notMerge 重置,x 轴未变时恢复,
|
|
290
|
+
// 否则点一次图例、或坐标轴重算触发一次重渲染,图例就跳回第一页
|
|
291
|
+
const prevLegendScrollIndex = sameXAxis
|
|
292
|
+
? (
|
|
293
|
+
(chart.getOption() as ChartsOption | undefined)?.legend as
|
|
294
|
+
| Array<{ scrollDataIndex?: number }>
|
|
295
|
+
| undefined
|
|
296
|
+
)?.[0]?.scrollDataIndex
|
|
297
|
+
: undefined;
|
|
289
298
|
prevXAxisKeyRef.current = xAxisKey;
|
|
290
299
|
chart.setOption(chartOption as ChartOptionType, true);
|
|
300
|
+
if (prevLegendScrollIndex) {
|
|
301
|
+
chart.setOption({
|
|
302
|
+
legend: { scrollDataIndex: prevLegendScrollIndex },
|
|
303
|
+
} as unknown as ChartOptionType);
|
|
304
|
+
}
|
|
291
305
|
const nextZooms = (chartOption as ChartsOption).dataZoom;
|
|
292
306
|
if (
|
|
293
307
|
prevZoomRanges?.length &&
|
|
@@ -406,6 +420,10 @@ const ChartBar: React.FC<ChartBarProps> = (props) => {
|
|
|
406
420
|
visibleCount: legendVisibleCount,
|
|
407
421
|
interval: legendScrollInterval,
|
|
408
422
|
autoStart: true,
|
|
423
|
+
// 滚轮已被 x 轴平移占用时不再让图例抢,否则一次滚动两处都动
|
|
424
|
+
enableWheel:
|
|
425
|
+
!normalizedScroll.zoomEnabled ||
|
|
426
|
+
normalizedScroll.wheelModeWhenSliderHidden !== "scroll",
|
|
409
427
|
});
|
|
410
428
|
}
|
|
411
429
|
|
|
@@ -443,6 +461,8 @@ const ChartBar: React.FC<ChartBarProps> = (props) => {
|
|
|
443
461
|
enableLegendAutoScroll,
|
|
444
462
|
legendVisibleCount,
|
|
445
463
|
legendScrollInterval,
|
|
464
|
+
// 图例是否接管滚轮由它推导,漏了会拿到过期的滚动配置
|
|
465
|
+
normalizedScroll,
|
|
446
466
|
]);
|
|
447
467
|
|
|
448
468
|
// Handle auto play
|
|
@@ -277,14 +277,28 @@ const ChartLine: React.FC<ChartLineProps> = (props) => {
|
|
|
277
277
|
// Set the option: notMerge resets the dataZoom window to its initial position,
|
|
278
278
|
// so when x-axis data is unchanged (e.g. re-render triggered by axis recalculation / legend selection), record the current window first and restore it afterwards
|
|
279
279
|
const xAxisKey = xAxisData?.join("\u0001") ?? "";
|
|
280
|
-
const
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
280
|
+
const sameXAxis = prevXAxisKeyRef.current === xAxisKey;
|
|
281
|
+
const prevZoomRanges = sameXAxis
|
|
282
|
+
? ((chart.getOption() as ChartsOption | undefined)?.dataZoom as
|
|
283
|
+
| Array<{ start?: number; end?: number }>
|
|
284
|
+
| undefined)
|
|
285
|
+
: undefined;
|
|
286
|
+
// 滚动图例的翻页位置同样会被 notMerge 重置,x 轴未变时恢复,
|
|
287
|
+
// 否则点一次图例、或坐标轴重算触发一次重渲染,图例就跳回第一页
|
|
288
|
+
const prevLegendScrollIndex = sameXAxis
|
|
289
|
+
? (
|
|
290
|
+
(chart.getOption() as ChartsOption | undefined)?.legend as
|
|
291
|
+
| Array<{ scrollDataIndex?: number }>
|
|
292
|
+
| undefined
|
|
293
|
+
)?.[0]?.scrollDataIndex
|
|
294
|
+
: undefined;
|
|
286
295
|
prevXAxisKeyRef.current = xAxisKey;
|
|
287
296
|
chart.setOption(chartOption as ChartOptionType, true);
|
|
297
|
+
if (prevLegendScrollIndex) {
|
|
298
|
+
chart.setOption({
|
|
299
|
+
legend: { scrollDataIndex: prevLegendScrollIndex },
|
|
300
|
+
} as unknown as ChartOptionType);
|
|
301
|
+
}
|
|
288
302
|
const nextZooms = (chartOption as ChartsOption).dataZoom;
|
|
289
303
|
if (
|
|
290
304
|
prevZoomRanges?.length &&
|
|
@@ -403,6 +417,10 @@ const ChartLine: React.FC<ChartLineProps> = (props) => {
|
|
|
403
417
|
visibleCount: legendVisibleCount,
|
|
404
418
|
interval: legendScrollInterval,
|
|
405
419
|
autoStart: true,
|
|
420
|
+
// 滚轮已被 x 轴平移占用时不再让图例抢,否则一次滚动两处都动
|
|
421
|
+
enableWheel:
|
|
422
|
+
!normalizedScroll.zoomEnabled ||
|
|
423
|
+
normalizedScroll.wheelModeWhenSliderHidden !== "scroll",
|
|
406
424
|
});
|
|
407
425
|
}
|
|
408
426
|
|
|
@@ -440,6 +458,8 @@ const ChartLine: React.FC<ChartLineProps> = (props) => {
|
|
|
440
458
|
enableLegendAutoScroll,
|
|
441
459
|
legendVisibleCount,
|
|
442
460
|
legendScrollInterval,
|
|
461
|
+
// 图例是否接管滚轮由它推导,漏了会拿到过期的滚动配置
|
|
462
|
+
normalizedScroll,
|
|
443
463
|
]);
|
|
444
464
|
|
|
445
465
|
// Handle auto play
|
|
@@ -7,6 +7,11 @@ export interface AutoScrollLegendProps {
|
|
|
7
7
|
interval?: number;
|
|
8
8
|
autoStart?: boolean;
|
|
9
9
|
stopOnWheel?: boolean;
|
|
10
|
+
/**
|
|
11
|
+
* 是否接管滚轮翻图例,默认 true。
|
|
12
|
+
* 图表自身已用滚轮平移 x 轴时传 false,避免一次滚动既翻图例又移坐标轴。
|
|
13
|
+
*/
|
|
14
|
+
enableWheel?: boolean;
|
|
10
15
|
}
|
|
11
16
|
|
|
12
17
|
export function autoScrollLegend(props: AutoScrollLegendProps) {
|
|
@@ -17,9 +22,14 @@ export function autoScrollLegend(props: AutoScrollLegendProps) {
|
|
|
17
22
|
interval = 1500,
|
|
18
23
|
autoStart = true,
|
|
19
24
|
stopOnWheel = true,
|
|
25
|
+
enableWheel = true,
|
|
20
26
|
} = props;
|
|
21
27
|
|
|
22
|
-
|
|
28
|
+
// 组件重渲染会重建滚动器,从图例当前位置接着轮播,避免每次都跳回第一项
|
|
29
|
+
const legendOption = (
|
|
30
|
+
chart.getOption() as { legend?: Array<{ scrollDataIndex?: number }> }
|
|
31
|
+
)?.legend?.[0];
|
|
32
|
+
let currentIndex = legendOption?.scrollDataIndex ?? 0;
|
|
23
33
|
let scrollInterval: NodeJS.Timeout | null = null;
|
|
24
34
|
let isManualScrolling = false;
|
|
25
35
|
|
|
@@ -90,13 +100,17 @@ export function autoScrollLegend(props: AutoScrollLegendProps) {
|
|
|
90
100
|
const chartDom = chart.getDom();
|
|
91
101
|
chartDom.addEventListener("mouseover", handleMouseOver);
|
|
92
102
|
chartDom.addEventListener("mouseout", handleMouseOut);
|
|
93
|
-
|
|
103
|
+
if (enableWheel) {
|
|
104
|
+
chartDom.addEventListener("wheel", handleWheel, { passive: false });
|
|
105
|
+
}
|
|
94
106
|
|
|
95
107
|
function dispose() {
|
|
96
108
|
stop();
|
|
97
109
|
chartDom.removeEventListener("mouseover", handleMouseOver);
|
|
98
110
|
chartDom.removeEventListener("mouseout", handleMouseOut);
|
|
99
|
-
|
|
111
|
+
if (enableWheel) {
|
|
112
|
+
chartDom.removeEventListener("wheel", handleWheel);
|
|
113
|
+
}
|
|
100
114
|
}
|
|
101
115
|
|
|
102
116
|
if (autoStart) {
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
|
|
3
|
+
import XlsxPreview from "../XlsxPreview";
|
|
4
|
+
import { useXlsxData } from "../../_hooks";
|
|
5
|
+
|
|
6
|
+
export interface XlsxPreviewPaneProps {
|
|
7
|
+
fileUrl?: string;
|
|
8
|
+
fileType?: string;
|
|
9
|
+
onClose?: () => void;
|
|
10
|
+
className?: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* xlsx 预览的完整单元:取数(useXlsxData → xlsx)+ 渲染(XlsxPreview → x-data-spreadsheet)。
|
|
15
|
+
*
|
|
16
|
+
* 单独成文件是为了让 FilePreview 能整体 lazy 掉它。useXlsxData 是 hook,不能写在
|
|
17
|
+
* switch 的某个分支里,若留在 FilePreview 顶层就会把 xlsx 静态拖进依赖图——那正是
|
|
18
|
+
* 要避免的。把「取数 + 渲染」收进同一个组件,整块按需加载。
|
|
19
|
+
*/
|
|
20
|
+
const XlsxPreviewPane: React.FC<XlsxPreviewPaneProps> = ({
|
|
21
|
+
fileUrl,
|
|
22
|
+
fileType,
|
|
23
|
+
onClose,
|
|
24
|
+
className,
|
|
25
|
+
}) => {
|
|
26
|
+
const xlsxData = useXlsxData({ fileType, fileUrl });
|
|
27
|
+
|
|
28
|
+
return (
|
|
29
|
+
<XlsxPreview xlsxData={xlsxData} onClose={onClose} className={className} />
|
|
30
|
+
);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export default XlsxPreviewPane;
|
|
@@ -59,3 +59,14 @@ export default () => {
|
|
|
59
59
|
| onClose | 关闭回调 | `() => void` | - |
|
|
60
60
|
|
|
61
61
|
> 另导出类型 `FilePreviewType` 与常量 `FilePreviewTypeArr`(支持的文件类型数组)。
|
|
62
|
+
|
|
63
|
+
### 重型格式按需加载
|
|
64
|
+
|
|
65
|
+
`pdf`、`xlsx`、`md` 三种预览器依赖体积很大的三方库(pdfjs、xlsx + x-data-spreadsheet、
|
|
66
|
+
katex 等),已改为按需加载:只有 `fileType` 真的命中时才去拉对应 chunk。
|
|
67
|
+
`mp4` / 图片 / `txt` 是轻量实现,保持同步渲染。
|
|
68
|
+
|
|
69
|
+
这一点对使用方很重要:`FilePreview` 在 `Upload`、`FormItem`(`FILE` / `IMAGEFILE`)的
|
|
70
|
+
静态依赖图上,若不这么做,**业务侧只要用到任何一种表单项,上述库就会被打进首屏**。
|
|
71
|
+
|
|
72
|
+
用法不变,组件内部已包 `Suspense`。首次预览某种重型格式时弹窗内容会晚一拍出现。
|
|
@@ -1,16 +1,33 @@
|
|
|
1
|
-
import React from "react";
|
|
2
|
-
import PdfPreview from "./_components/PdfPreview";
|
|
1
|
+
import React, { Suspense, lazy } from "react";
|
|
3
2
|
import VideoPreview from "./_components/VideoPreview";
|
|
4
3
|
import TextPreview from "./_components/TextPreview";
|
|
5
|
-
import MarkdownPreview from "./_components/MarkdownPreview";
|
|
6
4
|
import ImagePreview from "./_components/ImagePreview";
|
|
7
|
-
import XlsxPreview from "./_components/XlsxPreview";
|
|
8
|
-
import { useXlsxData } from "./_hooks";
|
|
9
5
|
import { FilePreviewType, FilePreviewTypeArr } from "./_utils";
|
|
10
6
|
|
|
11
7
|
export type { FilePreviewType };
|
|
12
8
|
export { FilePreviewTypeArr };
|
|
13
9
|
|
|
10
|
+
/**
|
|
11
|
+
* 重型格式按需加载。
|
|
12
|
+
*
|
|
13
|
+
* FilePreview 是个按 fileType 分发的 dispatcher,而它处在 Upload / FormItem 的静态
|
|
14
|
+
* 依赖图上(FormItem → FormUpload → Upload → FilePreview),业务侧只要用到任何一种
|
|
15
|
+
* 表单项,下面这些只服务于特定格式的库就会被打进首屏:
|
|
16
|
+
*
|
|
17
|
+
* pdf → PdfPreview → hsu-utils 的 RenderPDF → pdfjs-dist 约 374 KB
|
|
18
|
+
* xlsx → XlsxPreviewPane → xlsx + x-data-spreadsheet
|
|
19
|
+
* md → MarkdownPreview → Markdown → katex 等
|
|
20
|
+
*
|
|
21
|
+
* 修 dispatcher 而不是逐个改它的引用方:这里改一次,所有静态 import FilePreview 的
|
|
22
|
+
* 地方(Upload、UploadedItem、FormImage…)都受益,也不会因为将来多一个引用方而回归。
|
|
23
|
+
*
|
|
24
|
+
* 图片/文本/视频三种是轻量实现,保持静态——它们也是最常命中的格式,不值得为其
|
|
25
|
+
* 付一次异步往返。
|
|
26
|
+
*/
|
|
27
|
+
const PdfPreview = lazy(() => import("./_components/PdfPreview"));
|
|
28
|
+
const MarkdownPreview = lazy(() => import("./_components/MarkdownPreview"));
|
|
29
|
+
const XlsxPreviewPane = lazy(() => import("./_components/XlsxPreviewPane"));
|
|
30
|
+
|
|
14
31
|
interface FilePreviewProps {
|
|
15
32
|
fileUrl?: string;
|
|
16
33
|
fileType?: FilePreviewType;
|
|
@@ -25,7 +42,6 @@ interface FilePreviewProps {
|
|
|
25
42
|
const FilePreview: React.FC<FilePreviewProps> = (props) => {
|
|
26
43
|
const { fileUrl, fileType, open, onClose, text, className, pagination } =
|
|
27
44
|
props;
|
|
28
|
-
const xlsxData = useXlsxData({ fileType, fileUrl });
|
|
29
45
|
|
|
30
46
|
if (!open) {
|
|
31
47
|
return null;
|
|
@@ -43,13 +59,15 @@ const FilePreview: React.FC<FilePreviewProps> = (props) => {
|
|
|
43
59
|
}
|
|
44
60
|
case "pdf": {
|
|
45
61
|
return (
|
|
46
|
-
<
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
62
|
+
<Suspense fallback={null}>
|
|
63
|
+
<PdfPreview
|
|
64
|
+
fileUrl={fileUrl}
|
|
65
|
+
open={open}
|
|
66
|
+
onClose={onClose}
|
|
67
|
+
className={className}
|
|
68
|
+
pagination={pagination}
|
|
69
|
+
/>
|
|
70
|
+
</Suspense>
|
|
53
71
|
);
|
|
54
72
|
}
|
|
55
73
|
case "jpg":
|
|
@@ -71,16 +89,25 @@ const FilePreview: React.FC<FilePreviewProps> = (props) => {
|
|
|
71
89
|
}
|
|
72
90
|
case "md": {
|
|
73
91
|
return (
|
|
74
|
-
<
|
|
92
|
+
<Suspense fallback={null}>
|
|
93
|
+
<MarkdownPreview
|
|
94
|
+
text={text}
|
|
95
|
+
onClose={onClose}
|
|
96
|
+
className={className}
|
|
97
|
+
/>
|
|
98
|
+
</Suspense>
|
|
75
99
|
);
|
|
76
100
|
}
|
|
77
101
|
case "xlsx": {
|
|
78
102
|
return (
|
|
79
|
-
<
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
103
|
+
<Suspense fallback={null}>
|
|
104
|
+
<XlsxPreviewPane
|
|
105
|
+
fileUrl={fileUrl}
|
|
106
|
+
fileType={fileType}
|
|
107
|
+
onClose={onClose}
|
|
108
|
+
className={className}
|
|
109
|
+
/>
|
|
110
|
+
</Suspense>
|
|
84
111
|
);
|
|
85
112
|
}
|
|
86
113
|
default: {
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import Input, { InputProps as BasicInputProps } from "../../../Input";
|
|
2
2
|
import ItemContainer, { ItemContainerProps } from "../../ItemContainer";
|
|
3
|
-
import React, { useState } from "react";
|
|
3
|
+
import React, { useEffect, useRef, useState } from "react";
|
|
4
4
|
|
|
5
5
|
import styles from "./index.module.scss";
|
|
6
|
-
import zxcvbn from "zxcvbn";
|
|
7
6
|
|
|
8
7
|
interface InputProps extends BasicInputProps {}
|
|
9
8
|
|
|
@@ -11,9 +10,43 @@ export interface FormPasswordStrengthProps extends ItemContainerProps {
|
|
|
11
10
|
componentProps?: InputProps;
|
|
12
11
|
}
|
|
13
12
|
|
|
13
|
+
/**
|
|
14
|
+
* zxcvbn 约 793 KB(未压缩),却只在这一个字段的 onChange 里用来算强度分。
|
|
15
|
+
* 静态 import 会让它跟着 FormInput → FormItem 进入每个引入过表单项的页面的
|
|
16
|
+
* 首屏 chunk。改成动态 import:只有真的渲染了 PASSWORDSTRENGTH 字段才去拉。
|
|
17
|
+
*
|
|
18
|
+
* 模块级缓存 Promise,多个密码框共用一次加载;失败时清掉缓存,下次重试。
|
|
19
|
+
*/
|
|
20
|
+
type Zxcvbn = typeof import("zxcvbn");
|
|
21
|
+
|
|
22
|
+
let zxcvbnPromise: Promise<Zxcvbn> | undefined;
|
|
23
|
+
const loadZxcvbn = (): Promise<Zxcvbn> => {
|
|
24
|
+
if (!zxcvbnPromise) {
|
|
25
|
+
zxcvbnPromise = import("zxcvbn")
|
|
26
|
+
.then((mod) => {
|
|
27
|
+
// zxcvbn 是 `export =` 的 CJS 模块:不同打包器/interop 下动态 import 拿到的
|
|
28
|
+
// 可能是函数本身,也可能是 { default: 函数 },两种都要兼容
|
|
29
|
+
const m = mod as Zxcvbn & { default?: Zxcvbn };
|
|
30
|
+
return m.default ?? m;
|
|
31
|
+
})
|
|
32
|
+
.catch((err) => {
|
|
33
|
+
zxcvbnPromise = undefined;
|
|
34
|
+
throw err;
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
return zxcvbnPromise;
|
|
38
|
+
};
|
|
39
|
+
|
|
14
40
|
const PasswordStrength: React.FC<InputProps> = (props) => {
|
|
15
41
|
const { placeholder, onChange, ...inputConfig } = props;
|
|
16
42
|
const [fraction, setFraction] = useState<number>(0);
|
|
43
|
+
// 挂载即预取:chunk 仍是独立的,但用户敲第一个字符时通常已经就绪
|
|
44
|
+
useEffect(() => {
|
|
45
|
+
loadZxcvbn().catch(() => undefined);
|
|
46
|
+
}, []);
|
|
47
|
+
|
|
48
|
+
// 打字比加载快时会有多次并发计算,迟到的结果不能盖掉最新一次
|
|
49
|
+
const seqRef = useRef(0);
|
|
17
50
|
|
|
18
51
|
return (
|
|
19
52
|
<div className={styles.passwordStrength}>
|
|
@@ -23,8 +56,14 @@ const PasswordStrength: React.FC<InputProps> = (props) => {
|
|
|
23
56
|
onChange={(value) => {
|
|
24
57
|
onChange && onChange(value);
|
|
25
58
|
|
|
26
|
-
const
|
|
27
|
-
|
|
59
|
+
const seq = ++seqRef.current;
|
|
60
|
+
loadZxcvbn()
|
|
61
|
+
.then((zxcvbn) => {
|
|
62
|
+
if (seq !== seqRef.current) return;
|
|
63
|
+
setFraction(zxcvbn(value).guesses_log10);
|
|
64
|
+
})
|
|
65
|
+
// 强度条只是辅助提示,拉不到就不显示,不打断用户输入
|
|
66
|
+
.catch(() => undefined);
|
|
28
67
|
}}
|
|
29
68
|
allowClear
|
|
30
69
|
autoComplete="new-password"
|
|
@@ -528,3 +528,22 @@ export default () => (
|
|
|
528
528
|
- 上传类:`FILE`、`IMAGEFILE`
|
|
529
529
|
|
|
530
530
|
> 同时导出 `FormItemContainer`(即 `ItemContainer`)及类型 `FormItemType`、`FormItemProps`、`PlaceholderDict` / `PlaceholderDictEn` 占位文案字典。
|
|
531
|
+
|
|
532
|
+
### 重型字段按需加载
|
|
533
|
+
|
|
534
|
+
`EDITOR`、`CODEMIRROR`、`PASSWORDSTRENGTH` 三类字段的实现依赖体积很大的三方库
|
|
535
|
+
(`@wangeditor/editor` 约 814 KB、`@codemirror/*` 约 380 KB、`zxcvbn` 约 793 KB,均为未压缩)。
|
|
536
|
+
`FormItem` 几乎会被每个业务页面引入,若静态引入这些渲染器,那么**只要用到任何一种表单项**,
|
|
537
|
+
上述库就会被一并打进消费方的首屏产物。
|
|
538
|
+
|
|
539
|
+
因此这三类字段改为按需加载:只有 `type` 真的命中时才去拉对应 chunk。对使用方而言:
|
|
540
|
+
|
|
541
|
+
- **用法不变**,仍然是 `<FormItem type="EDITOR" ... />`,无需自行包 `Suspense`。
|
|
542
|
+
- 首次渲染这类字段时,该字段会晚一拍出现(组件内部已用 `Suspense fallback={null}` 兜住,
|
|
543
|
+
不会让表单布局跳动)。antd `Form` 的值存在 store 里,字段挂载晚于 `setFieldsValue`
|
|
544
|
+
也能正确回填,**不影响编辑态回显**。
|
|
545
|
+
- `PASSWORDSTRENGTH` 在挂载时即预取 `zxcvbn`,正常输入速度下感知不到延迟;强度条在
|
|
546
|
+
加载完成前保持 0,加载失败则不显示,不阻断输入。
|
|
547
|
+
|
|
548
|
+
若消费方希望这些库进首屏(例如整站以富文本编辑为主),照常在自己的入口静态
|
|
549
|
+
`import Editor from "@hsu-react/ui/es/components/Editor"` 即可,两者不冲突。
|
|
@@ -8,7 +8,7 @@ import FormDatePicker, {
|
|
|
8
8
|
FormRangePickerProps,
|
|
9
9
|
FormStepPickerProps,
|
|
10
10
|
} from "./FormDatePicker";
|
|
11
|
-
import
|
|
11
|
+
import type { FormEditorProps } from "./FormEditor";
|
|
12
12
|
import FormInput, {
|
|
13
13
|
FormInputNumberProps,
|
|
14
14
|
FormInputProps,
|
|
@@ -31,9 +31,40 @@ import FormTree, { FormTreeProps } from "./FormTree";
|
|
|
31
31
|
import FormUpload, { FormImageProps, FormUploadProps } from "./FormUpload";
|
|
32
32
|
import ItemContainer, { ItemContainerProps } from "./ItemContainer";
|
|
33
33
|
import FormText, { FormTextProps } from "./FormText";
|
|
34
|
-
import
|
|
34
|
+
import type { FormCodeMirrorProps } from "./FormCodeMirror";
|
|
35
35
|
|
|
36
|
-
import React from "react";
|
|
36
|
+
import React, { Suspense, lazy } from "react";
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* 重型字段按需加载。
|
|
40
|
+
*
|
|
41
|
+
* FormItem 是个静态分发器,几乎每个业务页面都会引入它。若在这里静态 import
|
|
42
|
+
* 全部渲染器,那么**只要用到任何一种表单项**,富文本、代码编辑器这些巨型依赖
|
|
43
|
+
* 就会被一并拉进消费方的首屏 chunk:
|
|
44
|
+
*
|
|
45
|
+
* FormItem → FormEditor → Editor → @wangeditor/editor (约 814 KB)
|
|
46
|
+
* FormItem → FormCodeMirror → CodeMirror → @codemirror/* (约 380 KB)
|
|
47
|
+
*
|
|
48
|
+
* 消费方实测:某后台项目仅因入口图里存在 FormItem,首屏就多背了约 2.5 MB
|
|
49
|
+
* 未压缩的三方库;改为按需加载后首屏从 3.72 MB 降到 965 KB(gzip)。
|
|
50
|
+
*
|
|
51
|
+
* 只有 type 真的命中 EDITOR / CODEMIRROR 时才会去拉对应 chunk。类型仍走静态
|
|
52
|
+
* `import type`(编译期擦除,不产生运行时依赖),所以 FormItemMap 不受影响。
|
|
53
|
+
* 同一目录下的 CodeMirror 早就用 `import()` 按需加载语言包/校验器,这里是同一思路。
|
|
54
|
+
*/
|
|
55
|
+
const FormEditor = lazy(() => import("./FormEditor"));
|
|
56
|
+
const FormCodeMirror = lazy(() => import("./FormCodeMirror"));
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* 重型字段的 Suspense 包装。
|
|
60
|
+
*
|
|
61
|
+
* fallback 用 null 而不是骨架:这些字段都由 ItemContainer 渲染 antd 的
|
|
62
|
+
* Form.Item,加载期间不占位反而不会让表单布局跳动;antd Form 的值存在 store 里,
|
|
63
|
+
* 字段挂载晚于 setFieldsValue 也能正确回填,不影响编辑态回显。
|
|
64
|
+
*/
|
|
65
|
+
const LazyField: React.FC<{ children: React.ReactNode }> = ({ children }) => (
|
|
66
|
+
<Suspense fallback={null}>{children}</Suspense>
|
|
67
|
+
);
|
|
37
68
|
|
|
38
69
|
export { ItemContainer as FormItemContainer };
|
|
39
70
|
export type { ItemContainerProps as FormItemContainerProps };
|
|
@@ -374,9 +405,17 @@ const FormItem: React.FC<FormItemProps> = (props) => {
|
|
|
374
405
|
case "SLIDER":
|
|
375
406
|
return <FormSlider {...props} />;
|
|
376
407
|
case "EDITOR":
|
|
377
|
-
return
|
|
408
|
+
return (
|
|
409
|
+
<LazyField>
|
|
410
|
+
<FormEditor {...props} />
|
|
411
|
+
</LazyField>
|
|
412
|
+
);
|
|
378
413
|
case "CODEMIRROR":
|
|
379
|
-
return
|
|
414
|
+
return (
|
|
415
|
+
<LazyField>
|
|
416
|
+
<FormCodeMirror {...props} />
|
|
417
|
+
</LazyField>
|
|
418
|
+
);
|
|
380
419
|
case "TEXT":
|
|
381
420
|
return <FormText {...props} />;
|
|
382
421
|
default:
|
|
@@ -43,6 +43,45 @@ export default () => {
|
|
|
43
43
|
};
|
|
44
44
|
```
|
|
45
45
|
|
|
46
|
+
## 触发器模式(推荐)
|
|
47
|
+
|
|
48
|
+
传入 `children` 作为触发元素,并且**不传 `open`**,弹窗自持开关状态,调用处原地包一层即可,不必为每个可确认的操作各存一份 `open`。
|
|
49
|
+
|
|
50
|
+
列表里尤其明显:`map()` 内部无法为每一行单独 `useState`,受控写法只能把「当前待确认的是哪一行」提到循环外,触发点和弹窗被拆到两个作用域;触发器模式没有这个问题。
|
|
51
|
+
|
|
52
|
+
```tsx
|
|
53
|
+
import React from "react";
|
|
54
|
+
import { SecondConf } from "@hsu-react/ui";
|
|
55
|
+
import { Button } from "antd";
|
|
56
|
+
|
|
57
|
+
const list = [
|
|
58
|
+
{ id: "1", name: "西游记" },
|
|
59
|
+
{ id: "2", name: "水浒传" },
|
|
60
|
+
];
|
|
61
|
+
|
|
62
|
+
export default () => (
|
|
63
|
+
<>
|
|
64
|
+
{list.map((it) => (
|
|
65
|
+
<SecondConf
|
|
66
|
+
key={it.id}
|
|
67
|
+
contentTitle={`删除《${it.name}》`}
|
|
68
|
+
contentText="删除后将无法恢复,请谨慎操作。"
|
|
69
|
+
okButtonProps={{ danger: true }}
|
|
70
|
+
onOk={() => console.log("deleted", it.id)}
|
|
71
|
+
>
|
|
72
|
+
<Button danger size="small" style={{ marginRight: 8 }}>
|
|
73
|
+
删除 {it.name}
|
|
74
|
+
</Button>
|
|
75
|
+
</SecondConf>
|
|
76
|
+
))}
|
|
77
|
+
</>
|
|
78
|
+
);
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
触发元素自身的 `onClick` 会照常先执行;点击会 `stopPropagation`,避免嵌在可点击的行/卡片里时连带触发整行的点击。
|
|
82
|
+
|
|
83
|
+
同时传 `open` 和 `children` 时以受控为准,此时 `children` 只作为触发元素渲染,开关由外部决定。
|
|
84
|
+
|
|
46
85
|
## API
|
|
47
86
|
|
|
48
87
|
在 `Modal`(即 antd `ModalProps`)基础上扩展(`title` 已被内部接管):
|
|
@@ -51,5 +90,8 @@ export default () => {
|
|
|
51
90
|
| --- | --- | --- | --- |
|
|
52
91
|
| contentTitle | 标题中「确认 … 吗?」的中间内容 | `ReactNode` | - |
|
|
53
92
|
| contentText | 标题下方的补充说明文案 | `ReactNode` | - |
|
|
93
|
+
| children | 触发元素;不传 `open` 时弹窗自持开关状态 | `ReactElement` | - |
|
|
54
94
|
|
|
55
95
|
> 其余属性(`open`、`onOk`、`onCancel`、`width`、`confirmLoading` 等)与 `Modal` 一致;组件内部默认 `width={800}`、`centered`、`maskClosable={false}`、`mask={false}`。
|
|
96
|
+
>
|
|
97
|
+
> `open` 传了就是受控(含 `undefined` 以外的任何值),`onOk` / `onCancel` 里需自行关闭;不传则由组件自行开关,`onOk` 只写业务动作即可。
|
|
@@ -1,36 +1,86 @@
|
|
|
1
|
-
import React, { ReactNode } from "react";
|
|
1
|
+
import React, { ReactElement, ReactNode, cloneElement, useState } from "react";
|
|
2
2
|
import styles from "./index.module.scss";
|
|
3
3
|
import Icon from "../Icon";
|
|
4
4
|
import Modal, { ModalProps } from "../Modal";
|
|
5
5
|
|
|
6
|
-
interface SecondConfProps
|
|
6
|
+
export interface SecondConfProps
|
|
7
|
+
extends Omit<ModalProps, "title" | "children"> {
|
|
7
8
|
contentTitle?: ReactNode;
|
|
8
9
|
contentText?: ReactNode;
|
|
10
|
+
/**
|
|
11
|
+
* Trigger element (button/icon). When provided and `open` is omitted, the
|
|
12
|
+
* dialog owns its open state, so the caller does not have to keep one flag
|
|
13
|
+
* per confirmable action — which is otherwise impossible inside a list
|
|
14
|
+
* `map()`. The child's own `onClick` still fires first, and the click stops
|
|
15
|
+
* propagating so a trigger nested in a clickable row or card does not also
|
|
16
|
+
* activate that row.
|
|
17
|
+
*/
|
|
18
|
+
children?: ReactElement<{ onClick?: (e: React.MouseEvent) => void }>;
|
|
9
19
|
}
|
|
10
20
|
|
|
11
21
|
const SecondConf: React.FC<SecondConfProps> = (props) => {
|
|
12
|
-
const {
|
|
22
|
+
const {
|
|
23
|
+
classNames,
|
|
24
|
+
contentTitle,
|
|
25
|
+
contentText,
|
|
26
|
+
children,
|
|
27
|
+
open,
|
|
28
|
+
onOk,
|
|
29
|
+
onCancel,
|
|
30
|
+
...modalConfig
|
|
31
|
+
} = props;
|
|
32
|
+
const [selfOpen, setSelfOpen] = useState<boolean>(false);
|
|
33
|
+
|
|
34
|
+
// Controlled whenever `open` is supplied; self-managed only in trigger mode.
|
|
35
|
+
const isControlled = open !== undefined;
|
|
36
|
+
const visible = isControlled ? open : selfOpen;
|
|
37
|
+
|
|
38
|
+
const trigger =
|
|
39
|
+
children &&
|
|
40
|
+
cloneElement(children, {
|
|
41
|
+
onClick: (e: React.MouseEvent) => {
|
|
42
|
+
e.stopPropagation();
|
|
43
|
+
children.props.onClick?.(e);
|
|
44
|
+
if (!isControlled) setSelfOpen(true);
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const handleOk: ModalProps["onOk"] = (e) => {
|
|
49
|
+
if (!isControlled) setSelfOpen(false);
|
|
50
|
+
onOk?.(e);
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const handleCancel: ModalProps["onCancel"] = (e) => {
|
|
54
|
+
if (!isControlled) setSelfOpen(false);
|
|
55
|
+
onCancel?.(e);
|
|
56
|
+
};
|
|
13
57
|
|
|
14
58
|
return (
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
59
|
+
<>
|
|
60
|
+
{trigger}
|
|
61
|
+
<Modal
|
|
62
|
+
width={800}
|
|
63
|
+
{...modalConfig}
|
|
64
|
+
open={visible}
|
|
65
|
+
onOk={handleOk}
|
|
66
|
+
onCancel={handleCancel}
|
|
67
|
+
centered
|
|
68
|
+
className={styles.SecondConf}
|
|
69
|
+
classNames={{
|
|
70
|
+
body: `${styles.body} ${classNames?.body || ""}`,
|
|
71
|
+
...classNames,
|
|
72
|
+
}}
|
|
73
|
+
maskClosable={false}
|
|
74
|
+
mask={false}
|
|
75
|
+
title=" "
|
|
76
|
+
>
|
|
77
|
+
<Icon icon="mingcute:question-line" className={styles.icon} />
|
|
78
|
+
<div className={styles.content}>
|
|
79
|
+
<div className={styles.title}>确认{contentTitle}吗?</div>
|
|
80
|
+
<div className={styles.text}>{contentText}</div>
|
|
81
|
+
</div>
|
|
82
|
+
</Modal>
|
|
83
|
+
</>
|
|
34
84
|
);
|
|
35
85
|
};
|
|
36
86
|
|