iglooform 2.4.16 → 2.4.20

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.
@@ -94,6 +94,52 @@ function _asyncToGenerator(fn) { return function () { var self = this, args = ar
94
94
  var useBreakpoint = _grid.default.useBreakpoint;
95
95
  var prefix = 'igloo-upload-photo';
96
96
 
97
+ function ClassifyType(type) {
98
+ if (type.includes('image')) {
99
+ return 'image';
100
+ }
101
+
102
+ if (type.includes('video')) {
103
+ return 'video';
104
+ }
105
+
106
+ if (type.includes('pdf')) {
107
+ return 'pdf';
108
+ }
109
+
110
+ return 'unknown';
111
+ }
112
+
113
+ var getFileType = /*#__PURE__*/function () {
114
+ var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(src) {
115
+ var r, contentType;
116
+ return regeneratorRuntime.wrap(function _callee$(_context) {
117
+ while (1) {
118
+ switch (_context.prev = _context.next) {
119
+ case 0:
120
+ _context.next = 2;
121
+ return fetch(src, {
122
+ cache: 'force-cache'
123
+ });
124
+
125
+ case 2:
126
+ r = _context.sent;
127
+ contentType = r.headers.get('content-type');
128
+ return _context.abrupt("return", ClassifyType(contentType));
129
+
130
+ case 5:
131
+ case "end":
132
+ return _context.stop();
133
+ }
134
+ }
135
+ }, _callee);
136
+ }));
137
+
138
+ return function getFileType(_x) {
139
+ return _ref.apply(this, arguments);
140
+ };
141
+ }();
142
+
97
143
  var UploadPhoto = function UploadPhoto(props) {
98
144
  var _classnames3, _classnames4;
99
145
 
@@ -117,46 +163,48 @@ var UploadPhoto = function UploadPhoto(props) {
117
163
  bizKey = props.bizKey,
118
164
  _props$handleUpload = props.handleUpload,
119
165
  handleUpload = _props$handleUpload === void 0 ? /*#__PURE__*/function () {
120
- var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(file) {
166
+ var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(file) {
121
167
  var formData, response, rst;
122
- return regeneratorRuntime.wrap(function _callee$(_context) {
168
+ return regeneratorRuntime.wrap(function _callee2$(_context2) {
123
169
  while (1) {
124
- switch (_context.prev = _context.next) {
170
+ switch (_context2.prev = _context2.next) {
125
171
  case 0:
126
172
  formData = new FormData();
127
173
  formData.append('file', file);
128
174
  formData.append('biz_key', bizKey);
129
- _context.next = 5;
175
+ _context2.next = 5;
130
176
  return fetch(uploadApi, {
131
177
  method: 'post',
132
178
  body: formData
133
179
  });
134
180
 
135
181
  case 5:
136
- response = _context.sent;
137
- _context.next = 8;
182
+ response = _context2.sent;
183
+ _context2.next = 8;
138
184
  return response.json();
139
185
 
140
186
  case 8:
141
- rst = _context.sent;
142
- return _context.abrupt("return", rst.url);
187
+ rst = _context2.sent;
188
+ return _context2.abrupt("return", rst.url);
143
189
 
144
190
  case 10:
145
191
  case "end":
146
- return _context.stop();
192
+ return _context2.stop();
147
193
  }
148
194
  }
149
- }, _callee);
195
+ }, _callee2);
150
196
  }));
151
197
 
152
- return function (_x) {
153
- return _ref.apply(this, arguments);
198
+ return function (_x2) {
199
+ return _ref2.apply(this, arguments);
154
200
  };
155
201
  }() : _props$handleUpload,
156
202
  setFieldError = props.setFieldError,
157
203
  className = props.className,
158
204
  id = props.id,
159
- validateField = props.validateField;
205
+ validateField = props.validateField,
206
+ _props$capture = props.capture,
207
+ capture = _props$capture === void 0 ? false : _props$capture;
160
208
 
161
209
  var _useContext2 = (0, _react.useContext)(_locale.LocaleContext),
162
210
  formatMessage = _useContext2.formatMessage;
@@ -165,17 +213,7 @@ var UploadPhoto = function UploadPhoto(props) {
165
213
  _useState2 = _slicedToArray(_useState, 1),
166
214
  isMobile = _useState2[0];
167
215
 
168
- var _useState3 = (0, _react.useState)(value.map(function (url, index) {
169
- return {
170
- url: url,
171
- dataUrl: url,
172
- status: 'success',
173
- type: '',
174
- name: "File".concat(index + 1),
175
- size: 0,
176
- uid: index
177
- };
178
- })),
216
+ var _useState3 = (0, _react.useState)([]),
179
217
  _useState4 = _slicedToArray(_useState3, 2),
180
218
  files = _useState4[0],
181
219
  setFiles = _useState4[1];
@@ -188,14 +226,64 @@ var UploadPhoto = function UploadPhoto(props) {
188
226
  var _useBreakpoint = useBreakpoint(),
189
227
  md = _useBreakpoint.md;
190
228
 
229
+ var mapFilesFromValue = function mapFilesFromValue(urls) {
230
+ var _files = [];
231
+ Promise.all(urls.map( /*#__PURE__*/function () {
232
+ var _ref3 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3(url, index) {
233
+ var type;
234
+ return regeneratorRuntime.wrap(function _callee3$(_context3) {
235
+ while (1) {
236
+ switch (_context3.prev = _context3.next) {
237
+ case 0:
238
+ _context3.next = 2;
239
+ return getFileType(url);
240
+
241
+ case 2:
242
+ type = _context3.sent;
243
+
244
+ _files.push({
245
+ url: url,
246
+ dataUrl: url,
247
+ status: 'success',
248
+ type: type,
249
+ name: "File".concat(index + 1),
250
+ size: 0,
251
+ uid: index
252
+ });
253
+
254
+ case 4:
255
+ case "end":
256
+ return _context3.stop();
257
+ }
258
+ }
259
+ }, _callee3);
260
+ }));
261
+
262
+ return function (_x3, _x4) {
263
+ return _ref3.apply(this, arguments);
264
+ };
265
+ }())).then(function () {
266
+ return setFiles(_files);
267
+ });
268
+ };
269
+
270
+ (0, _react.useEffect)(function () {
271
+ if (Array.isArray(value) && value.some(function (url) {
272
+ return typeof url === 'string' && !files.find(function (file) {
273
+ return file.url === url;
274
+ });
275
+ })) {
276
+ mapFilesFromValue(value);
277
+ }
278
+ }, [value, files]);
191
279
  var uploadId = (0, _react.useMemo)(function () {
192
280
  return "igloo-upload-photo-input-".concat(Math.random());
193
281
  }, []);
194
282
  var uploadIndex = (0, _react.useRef)((value === null || value === void 0 ? void 0 : value.length) ? value.length : 0);
195
283
  (0, _react.useEffect)(function () {
196
284
  return function () {
197
- files.forEach(function (_ref2) {
198
- var dataUrl = _ref2.dataUrl;
285
+ files.forEach(function (_ref4) {
286
+ var dataUrl = _ref4.dataUrl;
199
287
  URL.revokeObjectURL(dataUrl);
200
288
  });
201
289
  };
@@ -265,12 +353,12 @@ var UploadPhoto = function UploadPhoto(props) {
265
353
  };
266
354
 
267
355
  var uploadFile = /*#__PURE__*/function () {
268
- var _ref3 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(file, currentIndex) {
356
+ var _ref5 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4(file, currentIndex) {
269
357
  var uid, dataUrl, name, size, mimeType, type, errorMsg, _errorMsg;
270
358
 
271
- return regeneratorRuntime.wrap(function _callee2$(_context2) {
359
+ return regeneratorRuntime.wrap(function _callee4$(_context4) {
272
360
  while (1) {
273
- switch (_context2.prev = _context2.next) {
361
+ switch (_context4.prev = _context4.next) {
274
362
  case 0:
275
363
  uid = ++uploadIndex.current;
276
364
  dataUrl = URL.createObjectURL(file);
@@ -278,7 +366,7 @@ var UploadPhoto = function UploadPhoto(props) {
278
366
  type = (0, _tools.classifyType)(mimeType);
279
367
 
280
368
  if (!(type === 'unknown')) {
281
- _context2.next = 9;
369
+ _context4.next = 9;
282
370
  break;
283
371
  }
284
372
 
@@ -287,11 +375,11 @@ var UploadPhoto = function UploadPhoto(props) {
287
375
  });
288
376
  limitNumError.current = errorMsg;
289
377
  setFieldError && setFieldError(errorMsg);
290
- return _context2.abrupt("return", Promise.reject(errorMsg));
378
+ return _context4.abrupt("return", Promise.reject(errorMsg));
291
379
 
292
380
  case 9:
293
381
  if (!(limit && currentIndex > limit)) {
294
- _context2.next = 14;
382
+ _context4.next = 14;
295
383
  break;
296
384
  }
297
385
 
@@ -303,7 +391,7 @@ var UploadPhoto = function UploadPhoto(props) {
303
391
  });
304
392
  limitNumError.current = _errorMsg;
305
393
  setFieldError && setFieldError(_errorMsg);
306
- return _context2.abrupt("return", Promise.reject(_errorMsg));
394
+ return _context4.abrupt("return", Promise.reject(_errorMsg));
307
395
 
308
396
  case 14:
309
397
  limitNumError.current = undefined;
@@ -318,7 +406,7 @@ var UploadPhoto = function UploadPhoto(props) {
318
406
  uid: uid
319
407
  }]);
320
408
  });
321
- return _context2.abrupt("return", handleUploadPromise(file, function (url) {
409
+ return _context4.abrupt("return", handleUploadPromise(file, function (url) {
322
410
  var urls = [];
323
411
  setFiles(function (files) {
324
412
  return files.map(function (f) {
@@ -381,63 +469,63 @@ var UploadPhoto = function UploadPhoto(props) {
381
469
 
382
470
  case 17:
383
471
  case "end":
384
- return _context2.stop();
472
+ return _context4.stop();
385
473
  }
386
474
  }
387
- }, _callee2);
475
+ }, _callee4);
388
476
  }));
389
477
 
390
- return function uploadFile(_x2, _x3) {
391
- return _ref3.apply(this, arguments);
478
+ return function uploadFile(_x5, _x6) {
479
+ return _ref5.apply(this, arguments);
392
480
  };
393
481
  }();
394
482
 
395
483
  var handleUploadPromise = /*#__PURE__*/function () {
396
- var _ref4 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3(file, onSuccess, onFailed) {
484
+ var _ref6 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee5(file, onSuccess, onFailed) {
397
485
  var rst, errorMsg;
398
- return regeneratorRuntime.wrap(function _callee3$(_context3) {
486
+ return regeneratorRuntime.wrap(function _callee5$(_context5) {
399
487
  while (1) {
400
- switch (_context3.prev = _context3.next) {
488
+ switch (_context5.prev = _context5.next) {
401
489
  case 0:
402
- _context3.prev = 0;
403
- _context3.next = 3;
490
+ _context5.prev = 0;
491
+ _context5.next = 3;
404
492
  return handleUpload(file);
405
493
 
406
494
  case 3:
407
- rst = _context3.sent;
495
+ rst = _context5.sent;
408
496
  onSuccess(rst);
409
- return _context3.abrupt("return", Promise.resolve(rst));
497
+ return _context5.abrupt("return", Promise.resolve(rst));
410
498
 
411
499
  case 8:
412
- _context3.prev = 8;
413
- _context3.t0 = _context3["catch"](0);
414
- errorMsg = _typeof(_context3.t0) === 'object' ? _context3.t0.message : _context3.t0;
500
+ _context5.prev = 8;
501
+ _context5.t0 = _context5["catch"](0);
502
+ errorMsg = _typeof(_context5.t0) === 'object' ? _context5.t0.message : _context5.t0;
415
503
  onFailed(errorMsg);
416
- return _context3.abrupt("return", Promise.reject(errorMsg));
504
+ return _context5.abrupt("return", Promise.reject(errorMsg));
417
505
 
418
506
  case 13:
419
507
  case "end":
420
- return _context3.stop();
508
+ return _context5.stop();
421
509
  }
422
510
  }
423
- }, _callee3, null, [[0, 8]]);
511
+ }, _callee5, null, [[0, 8]]);
424
512
  }));
425
513
 
426
- return function handleUploadPromise(_x4, _x5, _x6) {
427
- return _ref4.apply(this, arguments);
514
+ return function handleUploadPromise(_x7, _x8, _x9) {
515
+ return _ref6.apply(this, arguments);
428
516
  };
429
517
  }();
430
518
 
431
519
  var handleDelete = /*#__PURE__*/function () {
432
- var _ref5 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4(index) {
520
+ var _ref7 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee6(index) {
433
521
  var arr, hasError, rst;
434
- return regeneratorRuntime.wrap(function _callee4$(_context4) {
522
+ return regeneratorRuntime.wrap(function _callee6$(_context6) {
435
523
  while (1) {
436
- switch (_context4.prev = _context4.next) {
524
+ switch (_context6.prev = _context6.next) {
437
525
  case 0:
438
526
  limitNumError.current = null;
439
527
  arr = files;
440
- _context4.next = 4;
528
+ _context6.next = 4;
441
529
  return setFiles(function (files) {
442
530
  var newArr = files.filter(function (_, i) {
443
531
  return i !== index;
@@ -447,7 +535,7 @@ var UploadPhoto = function UploadPhoto(props) {
447
535
  });
448
536
 
449
537
  case 4:
450
- _context4.next = 6;
538
+ _context6.next = 6;
451
539
  return setMasks(function (masks) {
452
540
  return masks.filter(function (_, i) {
453
541
  return i !== index;
@@ -461,7 +549,7 @@ var UploadPhoto = function UploadPhoto(props) {
461
549
  });
462
550
 
463
551
  if (!hasError) {
464
- _context4.next = 10;
552
+ _context6.next = 10;
465
553
  break;
466
554
  }
467
555
 
@@ -474,46 +562,46 @@ var UploadPhoto = function UploadPhoto(props) {
474
562
 
475
563
  return f.url;
476
564
  });
477
- return _context4.abrupt("return", typeof onChange === 'function' && onChange(rst));
565
+ return _context6.abrupt("return", typeof onChange === 'function' && onChange(rst));
478
566
 
479
567
  case 10:
480
- typeof onChange === 'function' && onChange(arr.map(function (_ref6) {
481
- var url = _ref6.url;
568
+ typeof onChange === 'function' && onChange(arr.map(function (_ref8) {
569
+ var url = _ref8.url;
482
570
  return url;
483
571
  }));
484
572
 
485
573
  case 11:
486
574
  case "end":
487
- return _context4.stop();
575
+ return _context6.stop();
488
576
  }
489
577
  }
490
- }, _callee4);
578
+ }, _callee6);
491
579
  }));
492
580
 
493
- return function handleDelete(_x7) {
494
- return _ref5.apply(this, arguments);
581
+ return function handleDelete(_x10) {
582
+ return _ref7.apply(this, arguments);
495
583
  };
496
584
  }();
497
585
 
498
586
  var handleReUpload = /*#__PURE__*/function () {
499
- var _ref7 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee5(index) {
587
+ var _ref9 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee7(index) {
500
588
  var _files$index, originFile, reUpUid, rst, latestFiles, fileListWithError, newList, _rst;
501
589
 
502
- return regeneratorRuntime.wrap(function _callee5$(_context5) {
590
+ return regeneratorRuntime.wrap(function _callee7$(_context7) {
503
591
  while (1) {
504
- switch (_context5.prev = _context5.next) {
592
+ switch (_context7.prev = _context7.next) {
505
593
  case 0:
506
594
  _files$index = files[index], originFile = _files$index.originFile, reUpUid = _files$index.uid;
507
595
 
508
596
  if (originFile) {
509
- _context5.next = 3;
597
+ _context7.next = 3;
510
598
  break;
511
599
  }
512
600
 
513
- return _context5.abrupt("return");
601
+ return _context7.abrupt("return");
514
602
 
515
603
  case 3:
516
- _context5.next = 5;
604
+ _context7.next = 5;
517
605
  return setFiles(function (files) {
518
606
  var newFiles = _toConsumableArray(files);
519
607
 
@@ -522,14 +610,14 @@ var UploadPhoto = function UploadPhoto(props) {
522
610
  });
523
611
 
524
612
  case 5:
525
- _context5.prev = 5;
526
- _context5.next = 8;
613
+ _context7.prev = 5;
614
+ _context7.next = 8;
527
615
  return handleUpload(originFile);
528
616
 
529
617
  case 8:
530
- rst = _context5.sent;
618
+ rst = _context7.sent;
531
619
  latestFiles = [];
532
- _context5.next = 12;
620
+ _context7.next = 12;
533
621
  return setFiles(function (files) {
534
622
  var newFiles = _toConsumableArray(files);
535
623
 
@@ -557,19 +645,19 @@ var UploadPhoto = function UploadPhoto(props) {
557
645
  return url;
558
646
  });
559
647
  onChange && onChange(fileListWithError);
560
- _context5.next = 23;
648
+ _context7.next = 23;
561
649
  break;
562
650
 
563
651
  case 16:
564
- _context5.prev = 16;
565
- _context5.t0 = _context5["catch"](5);
652
+ _context7.prev = 16;
653
+ _context7.t0 = _context7["catch"](5);
566
654
  newList = [];
567
- _context5.next = 21;
655
+ _context7.next = 21;
568
656
  return setFiles(function (files) {
569
657
  var newFiles = _toConsumableArray(files);
570
658
 
571
659
  newFiles[index].status = 'failed';
572
- newFiles[index].errorMsg = _context5.t0;
660
+ newFiles[index].errorMsg = _context7.t0;
573
661
  newList = newFiles;
574
662
  return newFiles;
575
663
  });
@@ -600,14 +688,14 @@ var UploadPhoto = function UploadPhoto(props) {
600
688
 
601
689
  case 24:
602
690
  case "end":
603
- return _context5.stop();
691
+ return _context7.stop();
604
692
  }
605
693
  }
606
- }, _callee5, null, [[5, 16]]);
694
+ }, _callee7, null, [[5, 16]]);
607
695
  }));
608
696
 
609
- return function handleReUpload(_x8) {
610
- return _ref7.apply(this, arguments);
697
+ return function handleReUpload(_x11) {
698
+ return _ref9.apply(this, arguments);
611
699
  };
612
700
  }();
613
701
 
@@ -701,12 +789,12 @@ var UploadPhoto = function UploadPhoto(props) {
701
789
  children: (0, _jsxRuntime.jsxs)(_row.default, {
702
790
  gutter: [isMobile ? 16 : 32, 16],
703
791
  wrap: true,
704
- children: [Boolean(samples === null || samples === void 0 ? void 0 : samples.length) && samples.map(function (_ref8, index) {
792
+ children: [Boolean(samples === null || samples === void 0 ? void 0 : samples.length) && samples.map(function (_ref10, index) {
705
793
  var _classnames, _classnames2;
706
794
 
707
- var src = _ref8.src,
708
- label = _ref8.label,
709
- type = _ref8.type;
795
+ var src = _ref10.src,
796
+ label = _ref10.label,
797
+ type = _ref10.type;
710
798
  return (0, _jsxRuntime.jsxs)(_col.default, {
711
799
  span: sampleSpan,
712
800
  children: [(0, _jsxRuntime.jsx)("img", {
@@ -742,7 +830,7 @@ var UploadPhoto = function UploadPhoto(props) {
742
830
  accept: "image/*,video/*,application/pdf",
743
831
  value: [],
744
832
  multiple: true,
745
- capture: true
833
+ capture: capture
746
834
  }, void 0), (0, _jsxRuntime.jsxs)(_space.default, {
747
835
  className: "".concat(prefix, "-content"),
748
836
  size: [16, 16],
@@ -766,13 +854,13 @@ var UploadPhoto = function UploadPhoto(props) {
766
854
  children: buttonText || 'Upload'
767
855
  }, void 0)
768
856
  }, void 0)]
769
- }, void 0), files.map(function (_ref9, index) {
857
+ }, void 0), files.map(function (_ref11, index) {
770
858
  var _classnames5;
771
859
 
772
- var dataUrl = _ref9.dataUrl,
773
- status = _ref9.status,
774
- type = _ref9.type,
775
- uid = _ref9.uid;
860
+ var dataUrl = _ref11.dataUrl,
861
+ status = _ref11.status,
862
+ type = _ref11.type,
863
+ uid = _ref11.uid;
776
864
  return (0, _jsxRuntime.jsx)(_badge.default, {
777
865
  count: getBadge(status, index, limit),
778
866
  children: (0, _jsxRuntime.jsxs)("div", {
@@ -840,10 +928,10 @@ function checkFileList(value) {
840
928
  return Promise.resolve();
841
929
  }
842
930
 
843
- UploadPhoto.formItemPropsHandler = function (_ref10) {
844
- var limit = _ref10.limit,
845
- previewFormater = _ref10.previewFormater,
846
- label = _ref10.label;
931
+ UploadPhoto.formItemPropsHandler = function (_ref12) {
932
+ var limit = _ref12.limit,
933
+ previewFormater = _ref12.previewFormater,
934
+ label = _ref12.label;
847
935
  return {
848
936
  previewFormater: previewFormater || function (value) {
849
937
  return (0, _jsxRuntime.jsx)(_uploadPreview.default, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "iglooform",
3
- "version": "2.4.16",
3
+ "version": "2.4.20",
4
4
  "scripts": {
5
5
  "start": "dumi dev",
6
6
  "build-dev": "dumi build",