@aura-react-comp/ui 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.md +67 -2
- package/esm/alert/index.d.ts +28 -0
- package/esm/alert/index.js +76 -0
- package/esm/alert/index.less +172 -0
- package/esm/avatar/index.d.ts +41 -0
- package/esm/avatar/index.js +117 -0
- package/esm/avatar/index.less +163 -0
- package/esm/badge/index.d.ts +29 -0
- package/esm/badge/index.js +84 -0
- package/esm/badge/index.less +215 -0
- package/esm/breadcrumb/index.d.ts +31 -0
- package/esm/breadcrumb/index.js +91 -0
- package/esm/breadcrumb/index.less +107 -0
- package/esm/button/index.d.ts +28 -0
- package/esm/button/index.js +59 -0
- package/esm/button/index.less +191 -0
- package/esm/card/index.d.ts +67 -0
- package/esm/card/index.js +156 -0
- package/esm/card/index.less +231 -0
- package/esm/checkbox/index.d.ts +35 -0
- package/esm/checkbox/index.js +118 -0
- package/esm/checkbox/index.less +175 -0
- package/esm/collapse/index.d.ts +45 -0
- package/esm/collapse/index.js +157 -0
- package/esm/collapse/index.less +144 -0
- package/esm/divider/index.d.ts +16 -0
- package/esm/divider/index.js +37 -0
- package/esm/divider/index.less +115 -0
- package/esm/dropdown/index.d.ts +38 -0
- package/esm/dropdown/index.js +108 -0
- package/esm/dropdown/index.less +247 -0
- package/esm/empty/index.d.ts +35 -0
- package/esm/empty/index.js +87 -0
- package/esm/empty/index.less +67 -0
- package/esm/flex/index.d.ts +24 -0
- package/esm/flex/index.js +64 -0
- package/esm/flex/index.less +9 -0
- package/esm/form/index.d.ts +121 -0
- package/esm/form/index.js +443 -0
- package/esm/form/index.less +258 -0
- package/esm/global.d.ts +6 -0
- package/esm/index.d.ts +78 -0
- package/esm/index.js +41 -0
- package/esm/input/index.d.ts +51 -0
- package/esm/input/index.js +217 -0
- package/esm/input/index.less +369 -0
- package/esm/layout/index.d.ts +46 -0
- package/esm/layout/index.js +135 -0
- package/esm/layout/index.less +132 -0
- package/esm/menu/index.d.ts +91 -0
- package/esm/menu/index.js +294 -0
- package/esm/menu/index.less +340 -0
- package/esm/message/index.d.ts +33 -0
- package/esm/message/index.js +199 -0
- package/esm/message/index.less +118 -0
- package/esm/notification/index.d.ts +33 -0
- package/esm/notification/index.js +229 -0
- package/esm/notification/index.less +175 -0
- package/esm/pagination/index.d.ts +38 -0
- package/esm/pagination/index.js +182 -0
- package/esm/pagination/index.less +230 -0
- package/esm/popconfirm/index.d.ts +37 -0
- package/esm/popconfirm/index.js +115 -0
- package/esm/popconfirm/index.less +229 -0
- package/esm/radio/index.d.ts +33 -0
- package/esm/radio/index.js +122 -0
- package/esm/radio/index.less +155 -0
- package/esm/rate/index.d.ts +30 -0
- package/esm/rate/index.js +143 -0
- package/esm/rate/index.less +87 -0
- package/esm/result/index.d.ts +19 -0
- package/esm/result/index.js +69 -0
- package/esm/result/index.less +104 -0
- package/esm/scrollbar/index.d.ts +18 -0
- package/esm/scrollbar/index.js +35 -0
- package/esm/scrollbar/index.less +67 -0
- package/esm/select/index.d.ts +63 -0
- package/esm/select/index.js +358 -0
- package/esm/select/index.less +471 -0
- package/esm/slider/index.d.ts +36 -0
- package/esm/slider/index.js +255 -0
- package/esm/slider/index.less +154 -0
- package/esm/space/index.d.ts +16 -0
- package/esm/space/index.js +34 -0
- package/esm/space/index.less +34 -0
- package/esm/spin/index.d.ts +44 -0
- package/esm/spin/index.js +152 -0
- package/esm/spin/index.less +179 -0
- package/esm/steps/index.d.ts +55 -0
- package/esm/steps/index.js +165 -0
- package/esm/steps/index.less +394 -0
- package/esm/switch/index.d.ts +30 -0
- package/esm/switch/index.js +56 -0
- package/esm/switch/index.less +160 -0
- package/esm/tabs/index.d.ts +45 -0
- package/esm/tabs/index.js +170 -0
- package/esm/tabs/index.less +228 -0
- package/esm/tag/index.d.ts +52 -0
- package/esm/tag/index.js +120 -0
- package/esm/tag/index.less +227 -0
- package/esm/textarea/index.d.ts +27 -0
- package/esm/textarea/index.js +84 -0
- package/esm/textarea/index.less +226 -0
- package/esm/theme/ThemeProvider.d.ts +15 -0
- package/esm/theme/ThemeProvider.js +32 -0
- package/esm/theme/index.d.ts +2 -0
- package/esm/theme/index.js +1 -0
- package/esm/theme/tokens.css +183 -0
- package/esm/theme/useTheme.d.ts +1 -0
- package/esm/theme/useTheme.js +1 -0
- package/esm/tooltip/index.d.ts +38 -0
- package/esm/tooltip/index.js +149 -0
- package/esm/tooltip/index.less +244 -0
- package/esm/typography/index.d.ts +26 -0
- package/esm/typography/index.js +92 -0
- package/esm/typography/index.less +107 -0
- package/esm/upload/index.d.ts +68 -0
- package/esm/upload/index.js +447 -0
- package/esm/upload/index.less +376 -0
- package/esm/upload/request.d.ts +26 -0
- package/esm/upload/request.js +38 -0
- package/package.json +56 -3
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import React, { forwardRef, createElement } from 'react';
|
|
2
|
+
import { classNames, prefixCls } from '@aura-react-comp/shared';
|
|
3
|
+
import "./index.less";
|
|
4
|
+
|
|
5
|
+
/* ===== Title ===== */
|
|
6
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
7
|
+
const levelTagMap = {
|
|
8
|
+
1: 'h1',
|
|
9
|
+
2: 'h2',
|
|
10
|
+
3: 'h3',
|
|
11
|
+
4: 'h4',
|
|
12
|
+
5: 'h5'
|
|
13
|
+
};
|
|
14
|
+
export const Title = /*#__PURE__*/forwardRef(({
|
|
15
|
+
level = 1,
|
|
16
|
+
className,
|
|
17
|
+
children,
|
|
18
|
+
...rest
|
|
19
|
+
}, ref) => {
|
|
20
|
+
const cls = classNames(prefixCls('typography-title'), prefixCls(`typography-title-${level}`), className);
|
|
21
|
+
return /*#__PURE__*/createElement(levelTagMap[level], {
|
|
22
|
+
ref,
|
|
23
|
+
className: cls,
|
|
24
|
+
...rest
|
|
25
|
+
}, children);
|
|
26
|
+
});
|
|
27
|
+
Title.displayName = 'Title';
|
|
28
|
+
|
|
29
|
+
/* ===== Text ===== */
|
|
30
|
+
|
|
31
|
+
export const Text = /*#__PURE__*/forwardRef(({
|
|
32
|
+
variant = 'default',
|
|
33
|
+
strong,
|
|
34
|
+
underline,
|
|
35
|
+
delete: del,
|
|
36
|
+
code,
|
|
37
|
+
mark,
|
|
38
|
+
className,
|
|
39
|
+
children,
|
|
40
|
+
...rest
|
|
41
|
+
}, ref) => {
|
|
42
|
+
const cls = classNames(prefixCls('typography-text'), variant !== 'default' && prefixCls(`typography-text-${variant}`), strong && prefixCls('typography-text-strong'), underline && prefixCls('typography-text-underline'), code && prefixCls('typography-text-code'), mark && prefixCls('typography-text-mark'), className);
|
|
43
|
+
const content = del ? /*#__PURE__*/_jsx("del", {
|
|
44
|
+
children: children
|
|
45
|
+
}) : children;
|
|
46
|
+
return /*#__PURE__*/_jsx("span", {
|
|
47
|
+
ref: ref,
|
|
48
|
+
className: cls,
|
|
49
|
+
...rest,
|
|
50
|
+
children: content
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
Text.displayName = 'Text';
|
|
54
|
+
|
|
55
|
+
/* ===== Paragraph ===== */
|
|
56
|
+
|
|
57
|
+
export const Paragraph = /*#__PURE__*/forwardRef(({
|
|
58
|
+
ellipsis = false,
|
|
59
|
+
className,
|
|
60
|
+
children,
|
|
61
|
+
...rest
|
|
62
|
+
}, ref) => {
|
|
63
|
+
const cls = classNames(prefixCls('typography-paragraph'), ellipsis && prefixCls('typography-paragraph-ellipsis'), className);
|
|
64
|
+
return /*#__PURE__*/_jsx("p", {
|
|
65
|
+
ref: ref,
|
|
66
|
+
className: cls,
|
|
67
|
+
...rest,
|
|
68
|
+
children: children
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
Paragraph.displayName = 'Paragraph';
|
|
72
|
+
|
|
73
|
+
/* ===== Typography 复合组件 ===== */
|
|
74
|
+
|
|
75
|
+
const TypographyBase = /*#__PURE__*/forwardRef(({
|
|
76
|
+
className,
|
|
77
|
+
children,
|
|
78
|
+
...rest
|
|
79
|
+
}, ref) => {
|
|
80
|
+
const cls = classNames(prefixCls('typography'), className);
|
|
81
|
+
return /*#__PURE__*/_jsx("span", {
|
|
82
|
+
ref: ref,
|
|
83
|
+
className: cls,
|
|
84
|
+
...rest,
|
|
85
|
+
children: children
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
TypographyBase.displayName = 'Typography';
|
|
89
|
+
export const Typography = TypographyBase;
|
|
90
|
+
Typography.Title = Title;
|
|
91
|
+
Typography.Text = Text;
|
|
92
|
+
Typography.Paragraph = Paragraph;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/* ===== Typography 基础 ===== */
|
|
2
|
+
.aura-typography {
|
|
3
|
+
color: var(--aura-text);
|
|
4
|
+
font-size: var(--aura-font-size-md);
|
|
5
|
+
line-height: 1.6;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/* ===== Title ===== */
|
|
9
|
+
.aura-typography-title {
|
|
10
|
+
margin: 0;
|
|
11
|
+
font-weight: 600;
|
|
12
|
+
line-height: 1.35;
|
|
13
|
+
color: var(--aura-text);
|
|
14
|
+
|
|
15
|
+
&&-1 {
|
|
16
|
+
font-size: var(--aura-font-size-3xl);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
&&-2 {
|
|
20
|
+
font-size: var(--aura-font-size-2xl);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
&&-3 {
|
|
24
|
+
font-size: var(--aura-font-size-xl);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
&&-4 {
|
|
28
|
+
font-size: var(--aura-font-size-lg);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
&&-5 {
|
|
32
|
+
font-size: var(--aura-font-size-md);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/* ===== Text ===== */
|
|
37
|
+
.aura-typography-text {
|
|
38
|
+
font-size: var(--aura-font-size-md);
|
|
39
|
+
color: var(--aura-text);
|
|
40
|
+
line-height: 1.6;
|
|
41
|
+
|
|
42
|
+
/* 变体颜色 */
|
|
43
|
+
&&-secondary {
|
|
44
|
+
color: var(--aura-text-secondary);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
&&-success {
|
|
48
|
+
color: var(--aura-success);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
&&-warning {
|
|
52
|
+
color: var(--aura-warning);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
&&-danger {
|
|
56
|
+
color: var(--aura-error);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/* 修饰符 */
|
|
60
|
+
&&-strong {
|
|
61
|
+
font-weight: 600;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
&&-underline {
|
|
65
|
+
text-decoration: underline;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
&&-code {
|
|
69
|
+
padding: 2px 6px;
|
|
70
|
+
font-family:
|
|
71
|
+
'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
|
|
72
|
+
font-size: 85%;
|
|
73
|
+
background: var(--aura-bg-secondary);
|
|
74
|
+
border: 1px solid var(--aura-border);
|
|
75
|
+
border-radius: var(--aura-radius-sm);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
&&-mark {
|
|
79
|
+
padding: 0 4px;
|
|
80
|
+
background: var(--aura-warning-light);
|
|
81
|
+
border-radius: var(--aura-radius-sm);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/* ===== Paragraph ===== */
|
|
86
|
+
.aura-typography-paragraph {
|
|
87
|
+
margin: 0;
|
|
88
|
+
font-size: var(--aura-font-size-md);
|
|
89
|
+
color: var(--aura-text);
|
|
90
|
+
line-height: 1.8;
|
|
91
|
+
|
|
92
|
+
&&-ellipsis {
|
|
93
|
+
display: -webkit-box;
|
|
94
|
+
overflow: hidden;
|
|
95
|
+
text-overflow: ellipsis;
|
|
96
|
+
white-space: normal;
|
|
97
|
+
-webkit-line-clamp: 1;
|
|
98
|
+
-webkit-box-orient: vertical;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/* ===== Dark mode ===== */
|
|
103
|
+
[data-theme='dark'] {
|
|
104
|
+
.aura-typography-text-code {
|
|
105
|
+
background: var(--aura-bg-tertiary);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
export interface UploadFile {
|
|
3
|
+
uid: string;
|
|
4
|
+
name: string;
|
|
5
|
+
status: 'uploading' | 'done' | 'error';
|
|
6
|
+
url?: string;
|
|
7
|
+
file?: File;
|
|
8
|
+
}
|
|
9
|
+
/** 自定义上传的参数:通过 onSuccess / onError 回报上传结果 */
|
|
10
|
+
export interface CustomRequestOptions {
|
|
11
|
+
file: File;
|
|
12
|
+
action?: string;
|
|
13
|
+
headers?: Record<string, string>;
|
|
14
|
+
onSuccess: (body?: unknown) => void;
|
|
15
|
+
onError: (error: Error) => void;
|
|
16
|
+
}
|
|
17
|
+
export interface UploadProps {
|
|
18
|
+
/** 接受的文件类型 */
|
|
19
|
+
accept?: string;
|
|
20
|
+
/** 是否支持多选 */
|
|
21
|
+
multiple?: boolean;
|
|
22
|
+
/** 是否禁用 */
|
|
23
|
+
disabled?: boolean;
|
|
24
|
+
/** 文件大小上限(bytes) */
|
|
25
|
+
maxSize?: number;
|
|
26
|
+
/** 最多可上传的文件数量;为 1 时新选择的文件直接替换 */
|
|
27
|
+
maxCount?: number;
|
|
28
|
+
/** 文件列表展示风格
|
|
29
|
+
* @default 'text'
|
|
30
|
+
*/
|
|
31
|
+
listType?: 'text' | 'picture' | 'picture-card';
|
|
32
|
+
/** 受控文件列表(编辑页回显);传入后组件不自改状态,更新请以 onChange 返回的列表为准 */
|
|
33
|
+
fileList?: UploadFile[];
|
|
34
|
+
/** 默认文件列表(非受控) */
|
|
35
|
+
defaultFileList?: UploadFile[];
|
|
36
|
+
/**
|
|
37
|
+
* 上传接口地址。
|
|
38
|
+
* 配置后选择文件即发起真实 POST(multipart/form-data,字段名 `file`),
|
|
39
|
+
* 成功置为 `done`、失败置为 `error`;不配置则保持本地模拟流程。
|
|
40
|
+
*/
|
|
41
|
+
action?: string;
|
|
42
|
+
/** 随上传请求附加的请求头(仅在配置了 `action` 时生效) */
|
|
43
|
+
headers?: Record<string, string>;
|
|
44
|
+
/** 文件列表变化回调 */
|
|
45
|
+
onChange?: (fileList: UploadFile[]) => void;
|
|
46
|
+
/** 上传前钩子,返回 false 阻止上传 */
|
|
47
|
+
beforeUpload?: (file: File) => boolean | Promise<File>;
|
|
48
|
+
/** 点击移除前的钩子,返回 false 阻止移除 */
|
|
49
|
+
onRemove?: (file: UploadFile) => boolean | void | Promise<boolean | void>;
|
|
50
|
+
/** 点击文件名(预览)回调 */
|
|
51
|
+
onPreview?: (file: UploadFile) => void;
|
|
52
|
+
/** 自定义上传实现;配置后忽略 action 的内置请求,通过 onSuccess / onError 回报状态 */
|
|
53
|
+
customRequest?: (options: CustomRequestOptions) => void;
|
|
54
|
+
/** 自定义类名 */
|
|
55
|
+
className?: string;
|
|
56
|
+
/** 自定义样式 */
|
|
57
|
+
style?: React.CSSProperties;
|
|
58
|
+
}
|
|
59
|
+
export interface DraggerProps extends UploadProps {
|
|
60
|
+
children?: React.ReactNode;
|
|
61
|
+
}
|
|
62
|
+
declare const Dragger: React.ForwardRefExoticComponent<DraggerProps & React.RefAttributes<HTMLDivElement>>;
|
|
63
|
+
interface UploadCompound extends React.ForwardRefExoticComponent<UploadProps & React.RefAttributes<HTMLDivElement>> {
|
|
64
|
+
Dragger: typeof Dragger;
|
|
65
|
+
}
|
|
66
|
+
declare const Upload: UploadCompound;
|
|
67
|
+
export { Upload, Dragger };
|
|
68
|
+
export default Upload;
|
|
@@ -0,0 +1,447 @@
|
|
|
1
|
+
import React, { forwardRef, useState, useRef, useCallback } from 'react';
|
|
2
|
+
import { classNames, prefixCls } from '@aura-react-comp/shared';
|
|
3
|
+
import { Uploading, CheckCircleFilled, CloseCircleFilled, Close, Upload as UploadIcon, CloudUpload, PicturePlaceholder } from '@aura-react-comp/icons';
|
|
4
|
+
import { requestUpload } from "./request";
|
|
5
|
+
import "./index.less";
|
|
6
|
+
|
|
7
|
+
/* ===== 类型定义 ===== */
|
|
8
|
+
|
|
9
|
+
/** 自定义上传的参数:通过 onSuccess / onError 回报上传结果 */
|
|
10
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
11
|
+
import { jsxs as _jsxs } from "react/jsx-runtime";
|
|
12
|
+
import { Fragment as _Fragment } from "react/jsx-runtime";
|
|
13
|
+
/* ===== 工具函数 ===== */
|
|
14
|
+
|
|
15
|
+
let uidCounter = 0;
|
|
16
|
+
function generateUid() {
|
|
17
|
+
return `aura-upload-${Date.now()}-${++uidCounter}`;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** 状态图标 */
|
|
21
|
+
const StatusIcon = ({
|
|
22
|
+
status
|
|
23
|
+
}) => {
|
|
24
|
+
if (status === 'uploading') {
|
|
25
|
+
return /*#__PURE__*/_jsx(Uploading, {
|
|
26
|
+
size: 16,
|
|
27
|
+
className: prefixCls('upload-status-icon')
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
if (status === 'done') {
|
|
31
|
+
return /*#__PURE__*/_jsx(CheckCircleFilled, {
|
|
32
|
+
size: 16,
|
|
33
|
+
className: classNames(prefixCls('upload-status-icon'), prefixCls('upload-status-done'))
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
return /*#__PURE__*/_jsx(CloseCircleFilled, {
|
|
37
|
+
size: 16,
|
|
38
|
+
className: classNames(prefixCls('upload-status-icon'), prefixCls('upload-status-error'))
|
|
39
|
+
});
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/* ===== 内部共享逻辑(Upload 与 Dragger 共用) ===== */
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* 文件上传核心流:列表受控/非受控、maxCount 收敛、上传编排(customRequest / action / 模拟)。
|
|
46
|
+
* 列表写入统一走 commit:非受控写内部状态,受控只回调 onChange,由调用方决定是否更新。
|
|
47
|
+
*/
|
|
48
|
+
function useUploadCore(options) {
|
|
49
|
+
const {
|
|
50
|
+
fileList: controlledList,
|
|
51
|
+
defaultFileList,
|
|
52
|
+
maxCount,
|
|
53
|
+
maxSize,
|
|
54
|
+
action,
|
|
55
|
+
headers,
|
|
56
|
+
onChange,
|
|
57
|
+
onRemove,
|
|
58
|
+
beforeUpload,
|
|
59
|
+
customRequest
|
|
60
|
+
} = options;
|
|
61
|
+
const isControlled = controlledList !== undefined;
|
|
62
|
+
// 状态值本身不直接读取(渲染读 listRef 即时快照),仅用于触发重渲染
|
|
63
|
+
const [, setInternalList] = useState(defaultFileList ?? []);
|
|
64
|
+
// 非受控模式的即时快照:异步补丁(patchFile)从这里读最新列表,避免 setState 闭包过期
|
|
65
|
+
const listRef = useRef(defaultFileList ?? []);
|
|
66
|
+
const currentList = isControlled ? controlledList : listRef.current;
|
|
67
|
+
|
|
68
|
+
/** 唯一的列表写入口 */
|
|
69
|
+
const commit = useCallback(next => {
|
|
70
|
+
listRef.current = next;
|
|
71
|
+
if (!isControlled) setInternalList(next);
|
|
72
|
+
onChange?.(next);
|
|
73
|
+
}, [isControlled, onChange]);
|
|
74
|
+
|
|
75
|
+
/** 按 uid 局部更新单个文件 */
|
|
76
|
+
const patchFile = useCallback((uid, patch) => {
|
|
77
|
+
const base = isControlled ? controlledList : listRef.current;
|
|
78
|
+
commit(base.map(f => f.uid === uid ? {
|
|
79
|
+
...f,
|
|
80
|
+
...patch
|
|
81
|
+
} : f));
|
|
82
|
+
}, [commit, isControlled, controlledList]);
|
|
83
|
+
|
|
84
|
+
/** 模拟上传过程(未配置 action / customRequest 时保持原行为,便于演示与测试) */
|
|
85
|
+
const simulateUpload = useCallback(uploadFile => {
|
|
86
|
+
setTimeout(() => {
|
|
87
|
+
patchFile(uploadFile.uid, {
|
|
88
|
+
status: 'done'
|
|
89
|
+
});
|
|
90
|
+
}, 1500);
|
|
91
|
+
}, [patchFile]);
|
|
92
|
+
|
|
93
|
+
/** 发起上传:customRequest 优先,其次 action 真实请求,否则定时模拟 */
|
|
94
|
+
const startUpload = useCallback(uploadFile => {
|
|
95
|
+
if (customRequest && uploadFile.file) {
|
|
96
|
+
customRequest({
|
|
97
|
+
file: uploadFile.file,
|
|
98
|
+
action,
|
|
99
|
+
headers,
|
|
100
|
+
onSuccess: () => patchFile(uploadFile.uid, {
|
|
101
|
+
status: 'done'
|
|
102
|
+
}),
|
|
103
|
+
onError: () => patchFile(uploadFile.uid, {
|
|
104
|
+
status: 'error'
|
|
105
|
+
})
|
|
106
|
+
});
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
if (!action || !uploadFile.file) {
|
|
110
|
+
simulateUpload(uploadFile);
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
requestUpload({
|
|
114
|
+
file: uploadFile.file,
|
|
115
|
+
action,
|
|
116
|
+
headers
|
|
117
|
+
}).then(() => patchFile(uploadFile.uid, {
|
|
118
|
+
status: 'done'
|
|
119
|
+
})).catch(() => patchFile(uploadFile.uid, {
|
|
120
|
+
status: 'error'
|
|
121
|
+
}));
|
|
122
|
+
}, [customRequest, action, headers, patchFile, simulateUpload]);
|
|
123
|
+
|
|
124
|
+
/** 处理选择的文件: maxSize / beforeUpload 过滤 → maxCount 收敛 → 追加并上传 */
|
|
125
|
+
const processFiles = useCallback(async files => {
|
|
126
|
+
if (!files || files.length === 0) return;
|
|
127
|
+
const incoming = [];
|
|
128
|
+
for (let i = 0; i < files.length; i++) {
|
|
129
|
+
const file = files[i];
|
|
130
|
+
if (maxSize && file.size > maxSize) continue;
|
|
131
|
+
if (beforeUpload) {
|
|
132
|
+
try {
|
|
133
|
+
const result = await beforeUpload(file);
|
|
134
|
+
if (result === false) continue;
|
|
135
|
+
} catch {
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
incoming.push({
|
|
140
|
+
uid: generateUid(),
|
|
141
|
+
name: file.name,
|
|
142
|
+
status: 'uploading',
|
|
143
|
+
file,
|
|
144
|
+
url: URL.createObjectURL(file)
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
if (incoming.length === 0) return;
|
|
148
|
+
|
|
149
|
+
// maxCount 收敛:为 1 时新选择直接替换;否则只保留剩余名额
|
|
150
|
+
let accepted = incoming;
|
|
151
|
+
if (maxCount === 1) {
|
|
152
|
+
accepted = [incoming[0]];
|
|
153
|
+
} else if (maxCount !== undefined) {
|
|
154
|
+
const room = maxCount - currentList.length;
|
|
155
|
+
if (room <= 0) return;
|
|
156
|
+
accepted = incoming.slice(0, room);
|
|
157
|
+
}
|
|
158
|
+
const nextList = maxCount === 1 ? accepted : [...currentList, ...accepted];
|
|
159
|
+
commit(nextList);
|
|
160
|
+
accepted.forEach(f => startUpload(f));
|
|
161
|
+
}, [maxSize, beforeUpload, maxCount, currentList, commit, startUpload]);
|
|
162
|
+
|
|
163
|
+
/** 删除文件;onRemove 返回 false 时阻止 */
|
|
164
|
+
const handleRemove = useCallback(async uid => {
|
|
165
|
+
const file = currentList.find(f => f.uid === uid);
|
|
166
|
+
if (!file) return;
|
|
167
|
+
if (onRemove) {
|
|
168
|
+
const result = await onRemove(file);
|
|
169
|
+
if (result === false) return;
|
|
170
|
+
}
|
|
171
|
+
commit(currentList.filter(f => f.uid !== uid));
|
|
172
|
+
}, [currentList, onRemove, commit]);
|
|
173
|
+
return {
|
|
174
|
+
fileList: currentList,
|
|
175
|
+
processFiles,
|
|
176
|
+
handleRemove
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/* ===== Upload 主组件 ===== */
|
|
181
|
+
|
|
182
|
+
const UploadBase = /*#__PURE__*/forwardRef(({
|
|
183
|
+
accept,
|
|
184
|
+
multiple = false,
|
|
185
|
+
disabled = false,
|
|
186
|
+
maxSize,
|
|
187
|
+
maxCount,
|
|
188
|
+
listType = 'text',
|
|
189
|
+
fileList,
|
|
190
|
+
defaultFileList,
|
|
191
|
+
action,
|
|
192
|
+
headers,
|
|
193
|
+
onChange,
|
|
194
|
+
beforeUpload,
|
|
195
|
+
onRemove,
|
|
196
|
+
onPreview,
|
|
197
|
+
customRequest,
|
|
198
|
+
className,
|
|
199
|
+
style
|
|
200
|
+
}, ref) => {
|
|
201
|
+
const inputRef = useRef(null);
|
|
202
|
+
const {
|
|
203
|
+
fileList: currentList,
|
|
204
|
+
processFiles,
|
|
205
|
+
handleRemove
|
|
206
|
+
} = useUploadCore({
|
|
207
|
+
fileList,
|
|
208
|
+
defaultFileList,
|
|
209
|
+
maxCount,
|
|
210
|
+
maxSize,
|
|
211
|
+
action,
|
|
212
|
+
headers,
|
|
213
|
+
onChange,
|
|
214
|
+
onRemove,
|
|
215
|
+
beforeUpload,
|
|
216
|
+
customRequest
|
|
217
|
+
});
|
|
218
|
+
const handleChange = useCallback(e => {
|
|
219
|
+
processFiles(e.target.files);
|
|
220
|
+
// 重置 input 以便再次选择相同文件
|
|
221
|
+
if (inputRef.current) inputRef.current.value = '';
|
|
222
|
+
}, [processFiles]);
|
|
223
|
+
|
|
224
|
+
/** 点击触发文件选择 */
|
|
225
|
+
const handleClick = useCallback(() => {
|
|
226
|
+
if (disabled) return;
|
|
227
|
+
inputRef.current?.click();
|
|
228
|
+
}, [disabled]);
|
|
229
|
+
|
|
230
|
+
/* --- className --- */
|
|
231
|
+
const wrapperCls = classNames(prefixCls('upload'), prefixCls(`upload-${listType}`), disabled && prefixCls('upload-disabled'), className);
|
|
232
|
+
|
|
233
|
+
/* --- 渲染文件列表项 --- */
|
|
234
|
+
const renderFileItem = file => {
|
|
235
|
+
const itemCls = classNames(prefixCls('upload-file'), prefixCls(`upload-file-${file.status}`));
|
|
236
|
+
const nameNode = /*#__PURE__*/_jsx("span", {
|
|
237
|
+
className: classNames(prefixCls('upload-file-name'), onPreview && prefixCls('upload-file-name-link')),
|
|
238
|
+
title: file.name,
|
|
239
|
+
onClick: onPreview ? () => onPreview(file) : undefined,
|
|
240
|
+
role: onPreview ? 'button' : undefined,
|
|
241
|
+
tabIndex: onPreview ? 0 : undefined,
|
|
242
|
+
onKeyDown: onPreview ? e => {
|
|
243
|
+
if (e.key === 'Enter' || e.key === ' ') {
|
|
244
|
+
e.preventDefault();
|
|
245
|
+
onPreview(file);
|
|
246
|
+
}
|
|
247
|
+
} : undefined,
|
|
248
|
+
children: file.name
|
|
249
|
+
});
|
|
250
|
+
return /*#__PURE__*/_jsxs("div", {
|
|
251
|
+
className: itemCls,
|
|
252
|
+
children: [(listType === 'picture' || listType === 'picture-card') && /*#__PURE__*/_jsx("div", {
|
|
253
|
+
className: prefixCls('upload-file-thumbnail'),
|
|
254
|
+
children: file.url ? /*#__PURE__*/_jsx("img", {
|
|
255
|
+
src: file.url,
|
|
256
|
+
alt: file.name
|
|
257
|
+
}) : /*#__PURE__*/_jsx(PicturePlaceholder, {
|
|
258
|
+
size: 24
|
|
259
|
+
})
|
|
260
|
+
}), /*#__PURE__*/_jsxs("div", {
|
|
261
|
+
className: prefixCls('upload-file-info'),
|
|
262
|
+
children: [nameNode, /*#__PURE__*/_jsx(StatusIcon, {
|
|
263
|
+
status: file.status
|
|
264
|
+
})]
|
|
265
|
+
}), /*#__PURE__*/_jsx("button", {
|
|
266
|
+
type: "button",
|
|
267
|
+
className: prefixCls('upload-file-remove'),
|
|
268
|
+
onClick: () => handleRemove(file.uid),
|
|
269
|
+
"aria-label": `删除 ${file.name}`,
|
|
270
|
+
title: "\u5220\u9664\u6587\u4EF6",
|
|
271
|
+
children: /*#__PURE__*/_jsx(Close, {
|
|
272
|
+
size: 14
|
|
273
|
+
})
|
|
274
|
+
})]
|
|
275
|
+
}, file.uid);
|
|
276
|
+
};
|
|
277
|
+
return /*#__PURE__*/_jsxs("div", {
|
|
278
|
+
ref: ref,
|
|
279
|
+
className: wrapperCls,
|
|
280
|
+
style: style,
|
|
281
|
+
children: [/*#__PURE__*/_jsx("input", {
|
|
282
|
+
ref: inputRef,
|
|
283
|
+
type: "file",
|
|
284
|
+
className: prefixCls('upload-input'),
|
|
285
|
+
accept: accept,
|
|
286
|
+
multiple: multiple,
|
|
287
|
+
onChange: handleChange,
|
|
288
|
+
disabled: disabled,
|
|
289
|
+
"aria-hidden": "true",
|
|
290
|
+
tabIndex: -1
|
|
291
|
+
}), /*#__PURE__*/_jsxs("button", {
|
|
292
|
+
type: "button",
|
|
293
|
+
className: prefixCls('upload-trigger'),
|
|
294
|
+
onClick: handleClick,
|
|
295
|
+
disabled: disabled,
|
|
296
|
+
"aria-label": "\u9009\u62E9\u6587\u4EF6\u4E0A\u4F20",
|
|
297
|
+
children: [/*#__PURE__*/_jsx(UploadIcon, {
|
|
298
|
+
size: 16
|
|
299
|
+
}), /*#__PURE__*/_jsx("span", {
|
|
300
|
+
children: "\u70B9\u51FB\u4E0A\u4F20"
|
|
301
|
+
})]
|
|
302
|
+
}), currentList.length > 0 && /*#__PURE__*/_jsx("div", {
|
|
303
|
+
className: prefixCls('upload-list'),
|
|
304
|
+
role: "list",
|
|
305
|
+
children: currentList.map(renderFileItem)
|
|
306
|
+
})]
|
|
307
|
+
});
|
|
308
|
+
});
|
|
309
|
+
UploadBase.displayName = 'Upload';
|
|
310
|
+
|
|
311
|
+
/* ===== Dragger 子组件 ===== */
|
|
312
|
+
|
|
313
|
+
const Dragger = /*#__PURE__*/forwardRef(({
|
|
314
|
+
accept,
|
|
315
|
+
multiple = false,
|
|
316
|
+
disabled = false,
|
|
317
|
+
maxSize,
|
|
318
|
+
maxCount,
|
|
319
|
+
fileList,
|
|
320
|
+
defaultFileList,
|
|
321
|
+
action,
|
|
322
|
+
headers,
|
|
323
|
+
onChange,
|
|
324
|
+
beforeUpload,
|
|
325
|
+
onRemove,
|
|
326
|
+
onPreview,
|
|
327
|
+
customRequest,
|
|
328
|
+
className,
|
|
329
|
+
style,
|
|
330
|
+
children
|
|
331
|
+
}, ref) => {
|
|
332
|
+
const [isDragOver, setIsDragOver] = useState(false);
|
|
333
|
+
const inputRef = useRef(null);
|
|
334
|
+
const {
|
|
335
|
+
fileList: currentList,
|
|
336
|
+
processFiles,
|
|
337
|
+
handleRemove
|
|
338
|
+
} = useUploadCore({
|
|
339
|
+
fileList,
|
|
340
|
+
defaultFileList,
|
|
341
|
+
maxCount,
|
|
342
|
+
maxSize,
|
|
343
|
+
action,
|
|
344
|
+
headers,
|
|
345
|
+
onChange,
|
|
346
|
+
onRemove,
|
|
347
|
+
beforeUpload,
|
|
348
|
+
customRequest
|
|
349
|
+
});
|
|
350
|
+
const handleDragOver = useCallback(e => {
|
|
351
|
+
e.preventDefault();
|
|
352
|
+
if (!disabled) setIsDragOver(true);
|
|
353
|
+
}, [disabled]);
|
|
354
|
+
const handleDragLeave = useCallback(e => {
|
|
355
|
+
e.preventDefault();
|
|
356
|
+
setIsDragOver(false);
|
|
357
|
+
}, []);
|
|
358
|
+
const handleDrop = useCallback(e => {
|
|
359
|
+
e.preventDefault();
|
|
360
|
+
setIsDragOver(false);
|
|
361
|
+
if (disabled) return;
|
|
362
|
+
processFiles(e.dataTransfer.files);
|
|
363
|
+
}, [disabled, processFiles]);
|
|
364
|
+
const handleClick = useCallback(() => {
|
|
365
|
+
if (disabled) return;
|
|
366
|
+
inputRef.current?.click();
|
|
367
|
+
}, [disabled]);
|
|
368
|
+
const handleInputChange = useCallback(e => {
|
|
369
|
+
processFiles(e.target.files);
|
|
370
|
+
if (inputRef.current) inputRef.current.value = '';
|
|
371
|
+
}, [processFiles]);
|
|
372
|
+
const wrapperCls = classNames(prefixCls('upload-dragger'), isDragOver && prefixCls('upload-dragger-hover'), disabled && prefixCls('upload-dragger-disabled'), className);
|
|
373
|
+
return /*#__PURE__*/_jsxs("div", {
|
|
374
|
+
ref: ref,
|
|
375
|
+
className: wrapperCls,
|
|
376
|
+
style: style,
|
|
377
|
+
children: [/*#__PURE__*/_jsx("input", {
|
|
378
|
+
ref: inputRef,
|
|
379
|
+
type: "file",
|
|
380
|
+
className: prefixCls('upload-input'),
|
|
381
|
+
accept: accept,
|
|
382
|
+
multiple: multiple,
|
|
383
|
+
onChange: handleInputChange,
|
|
384
|
+
disabled: disabled,
|
|
385
|
+
"aria-hidden": "true",
|
|
386
|
+
tabIndex: -1
|
|
387
|
+
}), /*#__PURE__*/_jsx("div", {
|
|
388
|
+
className: prefixCls('upload-dragger-area'),
|
|
389
|
+
onClick: handleClick,
|
|
390
|
+
onDragOver: handleDragOver,
|
|
391
|
+
onDragLeave: handleDragLeave,
|
|
392
|
+
onDrop: handleDrop,
|
|
393
|
+
role: "button",
|
|
394
|
+
"aria-label": "\u62D6\u62FD\u6587\u4EF6\u5230\u6B64\u533A\u57DF\u4E0A\u4F20",
|
|
395
|
+
tabIndex: disabled ? -1 : 0,
|
|
396
|
+
children: children ?? /*#__PURE__*/_jsxs(_Fragment, {
|
|
397
|
+
children: [/*#__PURE__*/_jsx(CloudUpload, {
|
|
398
|
+
size: 48,
|
|
399
|
+
className: prefixCls('upload-dragger-icon')
|
|
400
|
+
}), /*#__PURE__*/_jsx("p", {
|
|
401
|
+
className: prefixCls('upload-dragger-text'),
|
|
402
|
+
children: "\u5C06\u6587\u4EF6\u62D6\u62FD\u5230\u6B64\u533A\u57DF\u4E0A\u4F20"
|
|
403
|
+
}), /*#__PURE__*/_jsx("p", {
|
|
404
|
+
className: prefixCls('upload-dragger-hint'),
|
|
405
|
+
children: "\u652F\u6301\u5355\u4E2A\u6216\u6279\u91CF\u4E0A\u4F20"
|
|
406
|
+
})]
|
|
407
|
+
})
|
|
408
|
+
}), currentList.length > 0 && /*#__PURE__*/_jsx("div", {
|
|
409
|
+
className: prefixCls('upload-list'),
|
|
410
|
+
role: "list",
|
|
411
|
+
children: currentList.map(file => {
|
|
412
|
+
const itemCls = classNames(prefixCls('upload-file'), prefixCls(`upload-file-${file.status}`));
|
|
413
|
+
return /*#__PURE__*/_jsxs("div", {
|
|
414
|
+
className: itemCls,
|
|
415
|
+
children: [/*#__PURE__*/_jsxs("div", {
|
|
416
|
+
className: prefixCls('upload-file-info'),
|
|
417
|
+
children: [/*#__PURE__*/_jsx("span", {
|
|
418
|
+
className: classNames(prefixCls('upload-file-name'), onPreview && prefixCls('upload-file-name-link')),
|
|
419
|
+
title: file.name,
|
|
420
|
+
onClick: onPreview ? () => onPreview(file) : undefined,
|
|
421
|
+
role: onPreview ? 'button' : undefined,
|
|
422
|
+
children: file.name
|
|
423
|
+
}), /*#__PURE__*/_jsx(StatusIcon, {
|
|
424
|
+
status: file.status
|
|
425
|
+
})]
|
|
426
|
+
}), /*#__PURE__*/_jsx("button", {
|
|
427
|
+
type: "button",
|
|
428
|
+
className: prefixCls('upload-file-remove'),
|
|
429
|
+
onClick: () => handleRemove(file.uid),
|
|
430
|
+
"aria-label": `删除 ${file.name}`,
|
|
431
|
+
children: /*#__PURE__*/_jsx(Close, {
|
|
432
|
+
size: 14
|
|
433
|
+
})
|
|
434
|
+
})]
|
|
435
|
+
}, file.uid);
|
|
436
|
+
})
|
|
437
|
+
})]
|
|
438
|
+
});
|
|
439
|
+
});
|
|
440
|
+
Dragger.displayName = 'Upload.Dragger';
|
|
441
|
+
|
|
442
|
+
/* ===== 复合组件 ===== */
|
|
443
|
+
|
|
444
|
+
const Upload = UploadBase;
|
|
445
|
+
Upload.Dragger = Dragger;
|
|
446
|
+
export { Upload, Dragger };
|
|
447
|
+
export default Upload;
|