@guiwzh/small-design 0.1.0 → 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/dist/index.mjs CHANGED
@@ -492,7 +492,6 @@ var autoComplete_default = AutoComplete;
492
492
 
493
493
  // src/components/Upload/upload.tsx
494
494
  import { useRef as useRef4, useState as useState6 } from "react";
495
- import axios from "axios";
496
495
 
497
496
  // src/components/Progress/progress.tsx
498
497
  import { jsx as jsx9 } from "react/jsx-runtime";
@@ -615,10 +614,57 @@ var dragger_default = Dragger;
615
614
 
616
615
  // src/components/Upload/upload.tsx
617
616
  import { jsx as jsx12, jsxs as jsxs6 } from "react/jsx-runtime";
617
+ function defaultRequest(options) {
618
+ const { action, file, filename, headers, data, withCredentials, onProgress, onSuccess, onError } = options;
619
+ const formData = new FormData();
620
+ formData.append(filename, file);
621
+ if (data) {
622
+ Object.keys(data).forEach((key) => formData.append(key, data[key]));
623
+ }
624
+ const xhr = new XMLHttpRequest();
625
+ xhr.upload.addEventListener("progress", (e) => {
626
+ if (e.lengthComputable) {
627
+ const percent = Math.floor(e.loaded * 100 / e.total);
628
+ onProgress(percent);
629
+ }
630
+ });
631
+ xhr.addEventListener("load", () => {
632
+ if (xhr.status >= 200 && xhr.status < 300) {
633
+ let response;
634
+ try {
635
+ response = JSON.parse(xhr.responseText);
636
+ } catch (e) {
637
+ response = xhr.responseText;
638
+ }
639
+ onSuccess(response);
640
+ } else {
641
+ onError(new Error(`Upload failed with status ${xhr.status}`));
642
+ }
643
+ });
644
+ xhr.addEventListener("error", () => {
645
+ onError(new Error("Upload failed due to network error"));
646
+ });
647
+ xhr.addEventListener("abort", () => {
648
+ });
649
+ xhr.open("POST", action, true);
650
+ if (headers) {
651
+ Object.keys(headers).forEach((key) => {
652
+ xhr.setRequestHeader(key, headers[key]);
653
+ });
654
+ }
655
+ if (withCredentials) {
656
+ xhr.withCredentials = true;
657
+ }
658
+ xhr.send(formData);
659
+ return {
660
+ abort: () => xhr.abort()
661
+ };
662
+ }
618
663
  var Upload = (props) => {
619
664
  const {
620
665
  action,
621
666
  beforeUpload,
667
+ customRequest,
622
668
  onprogress,
623
669
  onSuccess,
624
670
  onError,
@@ -645,6 +691,7 @@ var Upload = (props) => {
645
691
  const abortRecord = useRef4({});
646
692
  const FileRecord = useRef4(/* @__PURE__ */ new Set());
647
693
  const [fileList, setFileList] = useState6([]);
694
+ const request = customRequest || defaultRequest;
648
695
  const handleClick = () => {
649
696
  var _a;
650
697
  (_a = fileInput.current) == null ? void 0 : _a.click();
@@ -656,21 +703,19 @@ var Upload = (props) => {
656
703
  }
657
704
  let concurrency = 0;
658
705
  const queue = [];
659
- function AskTask(task) {
660
- return new Promise(() => {
661
- const executeRequest = () => {
662
- concurrency++;
663
- task[0]().then(() => {
664
- concurrency--;
665
- nextTask();
666
- });
667
- };
668
- if (concurrency < limit) {
669
- executeRequest();
670
- } else {
671
- queue.push([executeRequest, task[1]]);
672
- }
673
- });
706
+ function askTask(task) {
707
+ const executeRequest = () => {
708
+ concurrency++;
709
+ task[0]().then(() => {
710
+ concurrency--;
711
+ nextTask();
712
+ });
713
+ };
714
+ if (concurrency < limit) {
715
+ executeRequest();
716
+ } else {
717
+ queue.push([executeRequest, task[1]]);
718
+ }
674
719
  }
675
720
  function nextTask() {
676
721
  if (concurrency < limit && queue.length > 0) {
@@ -685,7 +730,7 @@ var Upload = (props) => {
685
730
  }
686
731
  let uidCounter = 0;
687
732
  function dealFile(file) {
688
- let _file = {
733
+ const _file = {
689
734
  uid: `upload-file-${Date.now()}-${uidCounter++}`,
690
735
  size: file.size,
691
736
  name: file.name,
@@ -701,54 +746,40 @@ var Upload = (props) => {
701
746
  isSame = true;
702
747
  }
703
748
  }
704
- if (isSame) return prevList;
749
+ if (isSame) return [...prevList];
705
750
  return [_file, ...prevList];
706
751
  });
707
- const formData = new FormData();
708
- formData.append(name || "file", file);
709
- data && Object.keys(data).forEach((key) => formData.append(key, data[key]));
710
- const controller = new AbortController();
711
- abortRecord.current = { ...abortRecord.current, [_file.uid]: controller };
712
- return [_file, formData, controller];
752
+ return _file;
713
753
  }
714
- const post = (_file, formData, controller, try_nums) => {
754
+ const post = (_file, tryNums) => {
715
755
  return new Promise((resolve) => {
716
- axios.post(action, formData, {
717
- headers: {
718
- ...headers,
719
- "Content-Type": "multipart/form-data"
720
- },
756
+ const requestReturn = request({
757
+ action,
758
+ file: _file.raw,
759
+ filename: name || "file",
760
+ headers,
761
+ data,
721
762
  withCredentials,
722
- signal: controller.signal,
723
- onUploadProgress: (e) => {
724
- let percentage = Math.floor(e.loaded * 100 / e.total || 0);
725
- if (percentage < 100) {
726
- updateFileList(_file, {
727
- percent: percentage,
728
- status: "uploading"
729
- });
763
+ onProgress: (percent) => {
764
+ if (percent < 100) {
765
+ updateFileList(_file, { percent, status: "uploading" });
730
766
  _file.status = "uploading";
731
- _file.percent = percentage;
732
- onprogress == null ? void 0 : onprogress(percentage, _file);
767
+ _file.percent = percent;
768
+ onprogress == null ? void 0 : onprogress(percent, _file);
733
769
  }
734
- }
735
- }).then((res) => {
736
- updateFileList(_file, { status: "success", response: res.data });
737
- _file.status = "success";
738
- _file.response = res.data;
739
- onSuccess == null ? void 0 : onSuccess(res.data, _file);
740
- onChange == null ? void 0 : onChange(_file);
741
- resolve("success");
742
- }).catch((err) => {
743
- if (axios.isCancel(err)) {
744
- resolve("cancel");
745
- } else {
746
- if (try_nums < failednums) {
747
- const [_file_new, formData2, controller2] = dealFile(_file.raw);
748
- AskTask([
749
- () => post(_file_new, formData2, controller2, try_nums + 1),
750
- _file.name
751
- ]);
770
+ },
771
+ onSuccess: (response) => {
772
+ updateFileList(_file, { status: "success", response });
773
+ _file.status = "success";
774
+ _file.response = response;
775
+ onSuccess == null ? void 0 : onSuccess(response, _file);
776
+ onChange == null ? void 0 : onChange(_file);
777
+ resolve("success");
778
+ },
779
+ onError: (err) => {
780
+ if (tryNums < failednums) {
781
+ const newFile = dealFile(_file.raw);
782
+ askTask([() => post(newFile, tryNums + 1), _file.name]);
752
783
  }
753
784
  updateFileList(_file, { status: "error", error: err });
754
785
  _file.status = "error";
@@ -758,9 +789,10 @@ var Upload = (props) => {
758
789
  resolve("failed");
759
790
  }
760
791
  });
792
+ abortRecord.current[_file.uid] = requestReturn;
761
793
  });
762
794
  };
763
- let postFiles = Array.from(files);
795
+ const postFiles = Array.from(files);
764
796
  postFiles.forEach((file) => {
765
797
  if (maxsize && file.size > maxsize * 1024 * 1024) {
766
798
  onExceed == null ? void 0 : onExceed(`\u6587\u4EF6\u5927\u5C0F\u4E0D\u80FD\u8D85\u8FC7${maxsize}Mb`);
@@ -768,18 +800,18 @@ var Upload = (props) => {
768
800
  }
769
801
  FileRecord.current.add(file.name);
770
802
  if (!beforeUpload) {
771
- const [_file, formData, source] = dealFile(file);
772
- AskTask([() => post(_file, formData, source, 0), file.name]);
803
+ const _file = dealFile(file);
804
+ askTask([() => post(_file, 0), file.name]);
773
805
  } else {
774
806
  const result = beforeUpload(file);
775
807
  if (result && result instanceof Promise) {
776
- result.then((processedFile) => {
777
- const [_file, formData, source] = dealFile(file);
778
- AskTask([() => post(_file, formData, source, 0), file.name]);
808
+ result.then(() => {
809
+ const _file = dealFile(file);
810
+ askTask([() => post(_file, 0), file.name]);
779
811
  });
780
812
  } else if (result) {
781
- const [_file, formData, source] = dealFile(file);
782
- AskTask([() => post(_file, formData, source, 0), file.name]);
813
+ const _file = dealFile(file);
814
+ askTask([() => post(_file, 0), file.name]);
783
815
  }
784
816
  }
785
817
  });
@@ -1060,73 +1092,11 @@ var Lazyload = (props) => {
1060
1092
  return /* @__PURE__ */ jsx15("div", { ref: containerRef, className, style: styles, children: visible ? children : placeholder });
1061
1093
  };
1062
1094
  var lazyLoad_default = Lazyload;
1063
-
1064
- // src/components/Keepalive/Keepalive.tsx
1065
- import { createContext as createContext2, useContext as useContext3, useEffect as useEffect6, useRef as useRef7, useState as useState10 } from "react";
1066
- import { useOutlet, useLocation, matchPath } from "react-router-dom";
1067
- import { Fragment as Fragment2, jsx as jsx16, jsxs as jsxs8 } from "react/jsx-runtime";
1068
- var KeepAliveContext = createContext2({
1069
- keepPaths: [],
1070
- keepElements: {},
1071
- dropByPath() {
1072
- }
1073
- });
1074
- var isKeepPath = (keepPaths, path) => {
1075
- for (let i = 0; i < keepPaths.length; i++) {
1076
- let item = keepPaths[i];
1077
- if (item === path) return true;
1078
- if (item instanceof RegExp && item.test(path)) return true;
1079
- if (typeof item === "string" && item.toLowerCase() === path) return true;
1080
- }
1081
- return false;
1082
- };
1083
- function useKeepOutlet() {
1084
- const location = useLocation();
1085
- const element = useOutlet();
1086
- const { keepElements, keepPaths } = useContext3(KeepAliveContext);
1087
- const isKeep = isKeepPath(keepPaths, location.pathname);
1088
- const keepElementsRef = useRef7(keepElements);
1089
- keepElementsRef.current = keepElements;
1090
- useEffect6(() => {
1091
- if (isKeep && element) {
1092
- keepElementsRef.current[location.pathname] = element;
1093
- }
1094
- });
1095
- return /* @__PURE__ */ jsxs8(Fragment2, { children: [
1096
- Object.entries(keepElements).map(([pathname, el]) => /* @__PURE__ */ jsx16(
1097
- "div",
1098
- {
1099
- style: {
1100
- height: "100%",
1101
- width: "100%",
1102
- position: "relative",
1103
- overflow: "hidden auto"
1104
- },
1105
- className: "keep-alive-page",
1106
- hidden: !matchPath(location.pathname, pathname),
1107
- children: el
1108
- },
1109
- pathname
1110
- )),
1111
- !isKeep && element
1112
- ] });
1113
- }
1114
- var KeepAliveLayout = (props) => {
1115
- const { keepPaths, children } = props;
1116
- const [keepElements] = useState10({});
1117
- const dropByPath = (path) => {
1118
- delete keepElements[path];
1119
- };
1120
- return /* @__PURE__ */ jsx16(KeepAliveContext.Provider, { value: { keepPaths, keepElements, dropByPath }, children });
1121
- };
1122
- var Keepalive_default = KeepAliveLayout;
1123
1095
  export {
1124
1096
  autoComplete_default as AutoComplete,
1125
1097
  button_default as Button,
1126
1098
  icon_default as Icon,
1127
1099
  input_default as Input,
1128
- KeepAliveContext,
1129
- Keepalive_default as KeepAliveLayout,
1130
1100
  lazyLoad_default as LazyLoad,
1131
1101
  Menu_default as Menu,
1132
1102
  menuItem_default as MenuItem,
@@ -1137,7 +1107,6 @@ export {
1137
1107
  upload_default as Upload,
1138
1108
  virtualList_default as VirtualList,
1139
1109
  useClickOutside_default as useClickOutside,
1140
- useDebounce_default as useDebounce,
1141
- useKeepOutlet
1110
+ useDebounce_default as useDebounce
1142
1111
  };
1143
1112
  //# sourceMappingURL=index.mjs.map