@ecoding/components.antd 0.3.59 → 0.3.61
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/lib/core/multiple-img-upload/index.d.ts +6 -1
- package/lib/core/multiple-img-upload/index.js +103 -50
- package/lib/core/multiple-single-img-upload/index.js +1 -1
- package/lib/core/multiple-upload/index.d.ts +2 -2
- package/lib/core/multiple-upload/index.js +4 -4
- package/lib/core/single-file-upload/index.js +1 -1
- package/lib/core/single-img-upload/index.js +1 -1
- package/package.json +2 -2
|
@@ -1,16 +1,21 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import type { UploadFile } from 'antd';
|
|
3
|
+
interface IOpts {
|
|
4
|
+
size?: number;
|
|
5
|
+
w?: number;
|
|
6
|
+
h?: number;
|
|
7
|
+
}
|
|
3
8
|
interface IProps {
|
|
4
9
|
message?: string;
|
|
5
10
|
showMessage?: boolean;
|
|
6
11
|
actionParams?: any;
|
|
7
12
|
i18n?: any;
|
|
13
|
+
opts?: IOpts;
|
|
8
14
|
value?: UploadFile[] | string[];
|
|
9
15
|
headers?: any;
|
|
10
16
|
data?: any;
|
|
11
17
|
gif?: boolean;
|
|
12
18
|
disabled?: boolean;
|
|
13
|
-
isTip?: boolean;
|
|
14
19
|
name?: string;
|
|
15
20
|
action: string | (() => string);
|
|
16
21
|
buttonText?: string;
|
|
@@ -7,11 +7,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
import React, { useState } from 'react';
|
|
11
|
-
import { Upload, Image } from 'antd';
|
|
10
|
+
import React, { useState, useCallback, useMemo } from 'react';
|
|
11
|
+
import { Upload, Image as Img, Alert, Flex, message } from 'antd';
|
|
12
12
|
import { PlusOutlined } from '@ant-design/icons';
|
|
13
|
-
import
|
|
14
|
-
import Loading from "../../core/loading";
|
|
13
|
+
import { buildURL } from "@ecoding/helper.url";
|
|
15
14
|
import { DndContext, PointerSensor, useSensor } from '@dnd-kit/core';
|
|
16
15
|
import { arrayMove, SortableContext, useSortable, horizontalListSortingStrategy, } from '@dnd-kit/sortable';
|
|
17
16
|
import { CSS } from '@dnd-kit/utilities';
|
|
@@ -34,7 +33,9 @@ const DraggableUploadListItem = ({ originNode, file }) => {
|
|
|
34
33
|
* @returns 返回地址的string
|
|
35
34
|
*/
|
|
36
35
|
const MultipleImgUpload = (props) => {
|
|
37
|
-
const {
|
|
36
|
+
const { name, buttonText, maxCount, disabled, value, onChange } = props;
|
|
37
|
+
const [previewOpen, setPreviewOpen] = useState(false);
|
|
38
|
+
const [previewImage, setPreviewImage] = useState('');
|
|
38
39
|
const [imgList, setImgList] = useState((value === null || value === void 0 ? void 0 : value.map((url, index) => {
|
|
39
40
|
if (typeof url === "string") {
|
|
40
41
|
return {
|
|
@@ -60,47 +61,28 @@ const MultipleImgUpload = (props) => {
|
|
|
60
61
|
});
|
|
61
62
|
onChange && (onChange === null || onChange === void 0 ? void 0 : onChange(!isEmpty ? urls : undefined));
|
|
62
63
|
};
|
|
63
|
-
const handleChange = (
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
break;
|
|
68
|
-
case 'done': {
|
|
69
|
-
Loading.hide();
|
|
70
|
-
const response = info.file.response;
|
|
71
|
-
if (response && response.code === 200) {
|
|
72
|
-
const data = response.data || {};
|
|
73
|
-
const temp = imgList.concat([Object.assign({}, data)]);
|
|
74
|
-
updateItems(temp);
|
|
75
|
-
}
|
|
76
|
-
else {
|
|
77
|
-
Toast.error({
|
|
78
|
-
mask: true,
|
|
79
|
-
title: response.message || '服务出错,图片上传失败'
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
break;
|
|
83
|
-
}
|
|
84
|
-
case 'error': {
|
|
85
|
-
const response = info.file.response;
|
|
86
|
-
console.log(response.data);
|
|
87
|
-
Toast.error({
|
|
88
|
-
mask: true,
|
|
89
|
-
title: response.message || '服务出错,图片上传失败'
|
|
90
|
-
});
|
|
91
|
-
Loading.hide();
|
|
92
|
-
break;
|
|
64
|
+
const handleChange = ({ fileList }) => {
|
|
65
|
+
fileList && fileList.forEach((item) => {
|
|
66
|
+
if (item.status == "done" && item.response.code == 200 && item.response.data) {
|
|
67
|
+
item.url = item.response.data.url;
|
|
93
68
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
69
|
+
});
|
|
70
|
+
updateItems(fileList);
|
|
71
|
+
// if (len) {
|
|
72
|
+
// return;
|
|
73
|
+
// }
|
|
74
|
+
// updateItems(fileList)
|
|
97
75
|
};
|
|
98
76
|
const handleRemove = (file) => {
|
|
99
77
|
const lefts = imgList.filter(item => item.uid !== file.uid);
|
|
100
78
|
updateItems(lefts);
|
|
101
79
|
};
|
|
102
|
-
const
|
|
103
|
-
|
|
80
|
+
const action = useMemo(() => {
|
|
81
|
+
if (typeof props.action === "string") {
|
|
82
|
+
return buildURL(props.action, props.actionParams);
|
|
83
|
+
}
|
|
84
|
+
return buildURL(props.action(), props.actionParams);
|
|
85
|
+
}, [props.actionParams, props.action]);
|
|
104
86
|
const getBase64 = (file) => new Promise((resolve, reject) => {
|
|
105
87
|
const reader = new FileReader();
|
|
106
88
|
reader.readAsDataURL(file);
|
|
@@ -133,22 +115,93 @@ const MultipleImgUpload = (props) => {
|
|
|
133
115
|
updateItems(newItems);
|
|
134
116
|
}
|
|
135
117
|
};
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
118
|
+
const getImgwh = useCallback((base64) => {
|
|
119
|
+
return new Promise((resolve, reject) => {
|
|
120
|
+
const img = new Image();
|
|
121
|
+
img.onload = () => {
|
|
122
|
+
// 宽度固定
|
|
123
|
+
resolve({
|
|
124
|
+
w: img.width,
|
|
125
|
+
h: img.height
|
|
126
|
+
});
|
|
127
|
+
};
|
|
128
|
+
img.onerror = () => reject();
|
|
129
|
+
img.src = base64;
|
|
130
|
+
});
|
|
131
|
+
}, []);
|
|
132
|
+
const beforeUpload = useCallback((file) => {
|
|
133
|
+
return new Promise((resolve, reject) => __awaiter(void 0, void 0, void 0, function* () {
|
|
134
|
+
const ary = ["jpg", "jpeg", "png"];
|
|
135
|
+
if (props.gif) {
|
|
136
|
+
ary.push("gif");
|
|
137
|
+
}
|
|
138
|
+
if (ary.indexOf(file.type.toLowerCase().replace(/image\//g, "")) === -1) {
|
|
139
|
+
message.error(`请上传 ${ary.join(",")} 格式的图片!`);
|
|
140
|
+
reject();
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
const isLt2M = file.size / 1024 / 1024 < (props.opts.size || 1);
|
|
144
|
+
if (!isLt2M) {
|
|
145
|
+
message.error(`图片大小必须小于${props.opts.size} MB!`);
|
|
146
|
+
reject();
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
const base64 = yield getBase64(file);
|
|
150
|
+
const wh = yield getImgwh(base64);
|
|
151
|
+
if (props.opts.w && props.opts.w !== wh.w) {
|
|
152
|
+
message.error(`图片宽度不符合 ${props.opts.w}px 约定!`);
|
|
153
|
+
reject();
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
if (props.opts.h && props.opts.h !== wh.h) {
|
|
157
|
+
message.error(`图片高度不符合 ${props.opts.h}px 约定!`);
|
|
158
|
+
reject();
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
resolve();
|
|
162
|
+
}));
|
|
163
|
+
}, []);
|
|
164
|
+
return (React.createElement(React.Fragment, null,
|
|
165
|
+
props.showMessage && (React.createElement(Alert, { className: 'mb20', message: props.message ? props.message : (React.createElement(Flex, { gap: 10 },
|
|
166
|
+
React.createElement("div", null,
|
|
167
|
+
props.i18n ? props.i18n.$t("global.size", "大小") : "大小",
|
|
168
|
+
"\uFF1A",
|
|
169
|
+
props.i18n ? props.i18n.$t("global.lt", "小于") : "小于",
|
|
170
|
+
" ",
|
|
171
|
+
props.opts.size || 1,
|
|
172
|
+
"MB;"),
|
|
173
|
+
React.createElement("div", null,
|
|
174
|
+
props.i18n ? props.i18n.$t("global.dimension", "尺寸") : "尺寸",
|
|
175
|
+
"\uFF1A",
|
|
176
|
+
props.opts.w ? `${props.opts.w}px` : "n",
|
|
177
|
+
" * ",
|
|
178
|
+
props.opts.h ? `${props.opts.h}px` : "n",
|
|
179
|
+
";"),
|
|
180
|
+
React.createElement("div", null,
|
|
181
|
+
props.i18n ? props.i18n.$t("global.support", "支持") : "支持",
|
|
182
|
+
"\uFF1Ajpg\u3001jpeg\u3001png",
|
|
183
|
+
props.gif ? "、gif" : "",
|
|
184
|
+
";"))), type: "warning", showIcon: true })),
|
|
185
|
+
React.createElement(DndContext, { sensors: [sensor], onDragEnd: onDragEnd },
|
|
186
|
+
React.createElement(SortableContext, { items: imgList === null || imgList === void 0 ? void 0 : imgList.map((i) => i.uid), strategy: horizontalListSortingStrategy },
|
|
187
|
+
React.createElement(Upload, { listType: "picture-card", fileList: imgList, action: action, beforeUpload: beforeUpload, onPreview: handlePreview, onChange: handleChange, onRemove: handleRemove, name: name, data: props.data, multiple: true, disabled: disabled, withCredentials: true, itemRender: (originNode, file) => (React.createElement(DraggableUploadListItem, { originNode: originNode, file: file })) }, maxCount && imgList.length >= maxCount ? null : uploadButton),
|
|
188
|
+
previewImage && (React.createElement(Img, { wrapperStyle: { display: 'none' }, preview: {
|
|
189
|
+
visible: previewOpen,
|
|
190
|
+
onVisibleChange: (visible) => setPreviewOpen(visible),
|
|
191
|
+
afterOpenChange: (visible) => !visible && setPreviewImage('')
|
|
192
|
+
}, src: previewImage }))))));
|
|
144
193
|
};
|
|
145
194
|
MultipleImgUpload.defaultProps = {
|
|
195
|
+
maxCount: 6,
|
|
196
|
+
showMessage: true,
|
|
146
197
|
action: "/api/upload/img",
|
|
147
198
|
name: "file",
|
|
148
199
|
gif: false,
|
|
149
|
-
buttonText: '
|
|
200
|
+
buttonText: 'Upload',
|
|
150
201
|
data: {},
|
|
151
|
-
isTip: true,
|
|
152
202
|
actionParams: {},
|
|
203
|
+
opts: {
|
|
204
|
+
size: 1
|
|
205
|
+
}
|
|
153
206
|
};
|
|
154
207
|
export default MultipleImgUpload;
|
|
@@ -26,5 +26,5 @@ interface IProps {
|
|
|
26
26
|
openFileDialogOnClick?: boolean;
|
|
27
27
|
style?: React.CSSProperties;
|
|
28
28
|
}
|
|
29
|
-
declare const
|
|
30
|
-
export default
|
|
29
|
+
declare const MultipleUpload: React.FC<IProps>;
|
|
30
|
+
export default MultipleUpload;
|
|
@@ -37,7 +37,7 @@ const InfosRender = ({ infos, setInfos, onChange }) => {
|
|
|
37
37
|
}
|
|
38
38
|
return React.createElement(Empty, { imageStyle: { height: 30 }, image: Empty.PRESENTED_IMAGE_SIMPLE });
|
|
39
39
|
};
|
|
40
|
-
const
|
|
40
|
+
const MultipleUpload = (props) => {
|
|
41
41
|
const [infos, setInfos] = useState([]);
|
|
42
42
|
const [open, setOpen] = useState(undefined);
|
|
43
43
|
const timeout = useRef();
|
|
@@ -128,7 +128,7 @@ const SingleFileUpload = (props) => {
|
|
|
128
128
|
React.createElement(Popover, { destroyTooltipOnHide: true, open: open, placement: "right", trigger: "hover", title: "Uploaded", content: React.createElement(InfosRender, { onChange: props.onChange, infos: infos, setInfos: setInfos }) },
|
|
129
129
|
React.createElement(Button, { icon: React.createElement(EllipsisOutlined, null) }))));
|
|
130
130
|
};
|
|
131
|
-
|
|
131
|
+
MultipleUpload.defaultProps = {
|
|
132
132
|
name: 'file',
|
|
133
133
|
openFileDialogOnClick: true,
|
|
134
134
|
icon: true,
|
|
@@ -136,8 +136,8 @@ SingleFileUpload.defaultProps = {
|
|
|
136
136
|
extraData: {},
|
|
137
137
|
maxCount: 10,
|
|
138
138
|
headers: undefined,
|
|
139
|
-
buttonText: '
|
|
139
|
+
buttonText: 'Upload',
|
|
140
140
|
type: 'jpg, jpeg, png, gif, bmp, wbmp, webp, tif, woff, woff2, ttf, otf, txt, psd, svg, js, jsx, json, css, less, html, htm, xml, pdf, zip, gz, tgz, gzip, mp3, mp4, avi, xlsx, xls, doc, docx, ppt, pptx, rar, 7z',
|
|
141
141
|
style: {}
|
|
142
142
|
};
|
|
143
|
-
export default
|
|
143
|
+
export default MultipleUpload;
|
|
@@ -101,7 +101,7 @@ SingleFileUpload.defaultProps = {
|
|
|
101
101
|
icon: true,
|
|
102
102
|
data: {},
|
|
103
103
|
actionParams: {},
|
|
104
|
-
buttonText: "
|
|
104
|
+
buttonText: "Upload",
|
|
105
105
|
type: "jpg, jpeg, png, gif, bmp, wbmp, webp, tif, woff, woff2, ttf, otf, txt, psd, svg, js, jsx, json, css, less, html, htm, xml, pdf, zip, gz, tgz, gzip, mp3, mp4, avi, xlsx, xls, doc, docx, ppt, pptx, rar, 7z",
|
|
106
106
|
style: {}
|
|
107
107
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ecoding/components.antd",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.61",
|
|
4
4
|
"author": "cxc",
|
|
5
5
|
"homepage": "",
|
|
6
6
|
"license": "MIT",
|
|
@@ -47,5 +47,5 @@
|
|
|
47
47
|
"antd": "5.27.0",
|
|
48
48
|
"axios": "^1.1.2"
|
|
49
49
|
},
|
|
50
|
-
"gitHead": "
|
|
50
|
+
"gitHead": "40631c611e4f76eb3f693ba01640b53fe97ba494"
|
|
51
51
|
}
|