@ecoding/components.antd 0.3.59 → 0.3.60
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.
|
@@ -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,9 +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 { buildURL } from "@ecoding/helper.url";
|
|
13
14
|
import Toast from "../../core/toast";
|
|
14
15
|
import Loading from "../../core/loading";
|
|
15
16
|
import { DndContext, PointerSensor, useSensor } from '@dnd-kit/core';
|
|
@@ -34,7 +35,9 @@ const DraggableUploadListItem = ({ originNode, file }) => {
|
|
|
34
35
|
* @returns 返回地址的string
|
|
35
36
|
*/
|
|
36
37
|
const MultipleImgUpload = (props) => {
|
|
37
|
-
const {
|
|
38
|
+
const { name, buttonText, maxCount, disabled, value, onChange } = props;
|
|
39
|
+
const [previewOpen, setPreviewOpen] = useState(false);
|
|
40
|
+
const [previewImage, setPreviewImage] = useState('');
|
|
38
41
|
const [imgList, setImgList] = useState((value === null || value === void 0 ? void 0 : value.map((url, index) => {
|
|
39
42
|
if (typeof url === "string") {
|
|
40
43
|
return {
|
|
@@ -83,7 +86,6 @@ const MultipleImgUpload = (props) => {
|
|
|
83
86
|
}
|
|
84
87
|
case 'error': {
|
|
85
88
|
const response = info.file.response;
|
|
86
|
-
console.log(response.data);
|
|
87
89
|
Toast.error({
|
|
88
90
|
mask: true,
|
|
89
91
|
title: response.message || '服务出错,图片上传失败'
|
|
@@ -99,8 +101,12 @@ const MultipleImgUpload = (props) => {
|
|
|
99
101
|
const lefts = imgList.filter(item => item.uid !== file.uid);
|
|
100
102
|
updateItems(lefts);
|
|
101
103
|
};
|
|
102
|
-
const
|
|
103
|
-
|
|
104
|
+
const action = useMemo(() => {
|
|
105
|
+
if (typeof props.action === "string") {
|
|
106
|
+
return buildURL(props.action, props.actionParams);
|
|
107
|
+
}
|
|
108
|
+
return buildURL(props.action(), props.actionParams);
|
|
109
|
+
}, [props.actionParams, props.action]);
|
|
104
110
|
const getBase64 = (file) => new Promise((resolve, reject) => {
|
|
105
111
|
const reader = new FileReader();
|
|
106
112
|
reader.readAsDataURL(file);
|
|
@@ -133,22 +139,93 @@ const MultipleImgUpload = (props) => {
|
|
|
133
139
|
updateItems(newItems);
|
|
134
140
|
}
|
|
135
141
|
};
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
142
|
+
const getImgwh = useCallback((base64) => {
|
|
143
|
+
return new Promise((resolve, reject) => {
|
|
144
|
+
const img = new Image();
|
|
145
|
+
img.onload = () => {
|
|
146
|
+
// 宽度固定
|
|
147
|
+
resolve({
|
|
148
|
+
w: img.width,
|
|
149
|
+
h: img.height
|
|
150
|
+
});
|
|
151
|
+
};
|
|
152
|
+
img.onerror = () => reject();
|
|
153
|
+
img.src = base64;
|
|
154
|
+
});
|
|
155
|
+
}, []);
|
|
156
|
+
const beforeUpload = useCallback((file) => {
|
|
157
|
+
return new Promise((resolve, reject) => __awaiter(void 0, void 0, void 0, function* () {
|
|
158
|
+
const ary = ["jpg", "jpeg", "png"];
|
|
159
|
+
if (props.gif) {
|
|
160
|
+
ary.push("gif");
|
|
161
|
+
}
|
|
162
|
+
if (ary.indexOf(file.type.toLowerCase().replace(/image\//g, "")) === -1) {
|
|
163
|
+
message.error(`请上传 ${ary.join(",")} 格式的图片!`);
|
|
164
|
+
reject();
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
const isLt2M = file.size / 1024 / 1024 < (props.opts.size || 1);
|
|
168
|
+
if (!isLt2M) {
|
|
169
|
+
message.error(`图片大小必须小于${props.opts.size} MB!`);
|
|
170
|
+
reject();
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
const base64 = yield getBase64(file);
|
|
174
|
+
const wh = yield getImgwh(base64);
|
|
175
|
+
if (props.opts.w && props.opts.w !== wh.w) {
|
|
176
|
+
message.error(`图片宽度不符合 ${props.opts.w}px 约定!`);
|
|
177
|
+
reject();
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
if (props.opts.h && props.opts.h !== wh.h) {
|
|
181
|
+
message.error(`图片高度不符合 ${props.opts.h}px 约定!`);
|
|
182
|
+
reject();
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
resolve();
|
|
186
|
+
}));
|
|
187
|
+
}, []);
|
|
188
|
+
return (React.createElement(React.Fragment, null,
|
|
189
|
+
props.showMessage && (React.createElement(Alert, { className: 'mb20', message: props.message ? props.message : (React.createElement(Flex, { gap: 10 },
|
|
190
|
+
React.createElement("div", null,
|
|
191
|
+
props.i18n ? props.i18n.$t("global.size", "大小") : "大小",
|
|
192
|
+
"\uFF1A",
|
|
193
|
+
props.i18n ? props.i18n.$t("global.lt", "小于") : "小于",
|
|
194
|
+
" ",
|
|
195
|
+
props.opts.size || 1,
|
|
196
|
+
"MB;"),
|
|
197
|
+
React.createElement("div", null,
|
|
198
|
+
props.i18n ? props.i18n.$t("global.dimension", "尺寸") : "尺寸",
|
|
199
|
+
"\uFF1A",
|
|
200
|
+
props.opts.w ? `${props.opts.w}px` : "n",
|
|
201
|
+
" * ",
|
|
202
|
+
props.opts.h ? `${props.opts.h}px` : "n",
|
|
203
|
+
";"),
|
|
204
|
+
React.createElement("div", null,
|
|
205
|
+
props.i18n ? props.i18n.$t("global.support", "支持") : "支持",
|
|
206
|
+
"\uFF1Ajpg\u3001jpeg\u3001png",
|
|
207
|
+
props.gif ? "、gif" : "",
|
|
208
|
+
";"))), type: "warning", showIcon: true })),
|
|
209
|
+
React.createElement(DndContext, { sensors: [sensor], onDragEnd: onDragEnd },
|
|
210
|
+
React.createElement(SortableContext, { items: imgList === null || imgList === void 0 ? void 0 : imgList.map((i) => i.uid), strategy: horizontalListSortingStrategy },
|
|
211
|
+
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),
|
|
212
|
+
previewImage && (React.createElement(Img, { wrapperStyle: { display: 'none' }, preview: {
|
|
213
|
+
visible: previewOpen,
|
|
214
|
+
onVisibleChange: (visible) => setPreviewOpen(visible),
|
|
215
|
+
afterOpenChange: (visible) => !visible && setPreviewImage('')
|
|
216
|
+
}, src: previewImage }))))));
|
|
144
217
|
};
|
|
145
218
|
MultipleImgUpload.defaultProps = {
|
|
219
|
+
maxCount: 6,
|
|
220
|
+
showMessage: true,
|
|
146
221
|
action: "/api/upload/img",
|
|
147
222
|
name: "file",
|
|
148
223
|
gif: false,
|
|
149
224
|
buttonText: '上传图片',
|
|
150
225
|
data: {},
|
|
151
|
-
isTip: true,
|
|
152
226
|
actionParams: {},
|
|
227
|
+
opts: {
|
|
228
|
+
size: 1
|
|
229
|
+
}
|
|
153
230
|
};
|
|
154
231
|
export default MultipleImgUpload;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ecoding/components.antd",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.60",
|
|
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": "ba3818c41417032a2fb1c71f400cf283940dd54f"
|
|
51
51
|
}
|