@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/README.md CHANGED
@@ -1,11 +1,11 @@
1
- # @guiwz/small-design
1
+ # @guiwzh/small-design
2
2
 
3
3
  一套轻量级 React 组件库,基于 React 18 + TypeScript + SCSS 构建。
4
4
 
5
5
  ## 安装
6
6
 
7
7
  ```bash
8
- npm install @guiwz/small-design
8
+ npm install @guiwzh/small-design
9
9
  ```
10
10
 
11
11
  ### Peer Dependencies
@@ -29,8 +29,8 @@ npm install react-router-dom
29
29
  ## 使用
30
30
 
31
31
  ```tsx
32
- import { Button, Menu, AutoComplete } from '@guiwz/small-design'
33
- import '@guiwz/small-design/dist/index.css'
32
+ import { Button, Menu, AutoComplete } from '@guiwzh/small-design'
33
+ import '@guiwzh/small-design/dist/index.css'
34
34
  ```
35
35
 
36
36
  ## 组件列表
package/dist/index.d.mts CHANGED
@@ -1,8 +1,7 @@
1
- import React$1, { ReactNode, FC, InputHTMLAttributes, ReactElement, CSSProperties, PropsWithChildren, RefObject } from 'react';
1
+ import React$1, { ReactNode, FC, InputHTMLAttributes, ReactElement, CSSProperties, RefObject } from 'react';
2
2
  import { IconProp } from '@fortawesome/fontawesome-svg-core';
3
3
  import { FontAwesomeIconProps } from '@fortawesome/react-fontawesome';
4
4
  import { CSSTransitionProps } from 'react-transition-group/CSSTransition';
5
- import * as react_jsx_runtime from 'react/jsx-runtime';
6
5
 
7
6
  type ButtonSize = 'lg' | 'sm';
8
7
  type ButtonType = 'primary' | 'default' | 'danger' | 'link';
@@ -126,9 +125,24 @@ interface UploadFile {
126
125
  percent?: number;
127
126
  error?: any;
128
127
  }
128
+ interface UploadRequestOptions {
129
+ action: string;
130
+ file: File;
131
+ filename: string;
132
+ headers?: Record<string, string>;
133
+ data?: Record<string, string | Blob>;
134
+ withCredentials?: boolean;
135
+ onProgress: (percent: number) => void;
136
+ onSuccess: (response: any) => void;
137
+ onError: (error: any) => void;
138
+ }
139
+ interface UploadRequestReturn {
140
+ abort: () => void;
141
+ }
129
142
  interface UploadProps {
130
143
  action: string;
131
144
  beforeUpload?: (file: File) => boolean | Promise<File>;
145
+ customRequest?: (options: UploadRequestOptions) => UploadRequestReturn;
132
146
  onprogress?: (percentage: number, file: UploadFile) => void;
133
147
  onSuccess?: (data: any, file: UploadFile) => void;
134
148
  onError?: (err: any, file: UploadFile) => void;
@@ -198,18 +212,8 @@ interface LazyloadProps {
198
212
  }
199
213
  declare const Lazyload: FC<LazyloadProps>;
200
214
 
201
- interface KeepAliveLayoutProps extends PropsWithChildren {
202
- keepPaths: Array<string | RegExp>;
203
- keepElements?: Record<string, ReactNode>;
204
- dropByPath?: (path: string) => void;
205
- }
206
- type KeepAliveContextType = Omit<Required<KeepAliveLayoutProps>, "children">;
207
- declare const KeepAliveContext: React$1.Context<KeepAliveContextType>;
208
- declare function useKeepOutlet(): react_jsx_runtime.JSX.Element;
209
- declare const KeepAliveLayout: FC<KeepAliveLayoutProps>;
210
-
211
215
  declare function useDebounce<T>(value: T, delay?: number): T;
212
216
 
213
217
  declare function useClickOutside(ref: RefObject<HTMLElement>, handler: (event: MouseEvent) => void): void;
214
218
 
215
- export { type AutoCompletProps, AutoComplete, Button, type ButtonSize, type ButtonType, type DataSourceType, type IMenuComponent, Icon, type IconProps, Input, type InputProps, KeepAliveContext, KeepAliveLayout, Lazyload as LazyLoad, TransMenu as Menu, MenuItem, type MenuItemProps, type MenuProps, Progress, type ProgressProps, Signature, SubMenu, type SubMenuProps, type ThemeProps, Transition, Upload, type UploadFile, type UploadProps, VirtualList, useClickOutside, useDebounce, useKeepOutlet };
219
+ export { type AutoCompletProps, AutoComplete, Button, type ButtonSize, type ButtonType, type DataSourceType, type IMenuComponent, Icon, type IconProps, Input, type InputProps, Lazyload as LazyLoad, TransMenu as Menu, MenuItem, type MenuItemProps, type MenuProps, Progress, type ProgressProps, Signature, SubMenu, type SubMenuProps, type ThemeProps, Transition, Upload, type UploadFile, type UploadProps, VirtualList, useClickOutside, useDebounce };
package/dist/index.d.ts CHANGED
@@ -1,8 +1,7 @@
1
- import React$1, { ReactNode, FC, InputHTMLAttributes, ReactElement, CSSProperties, PropsWithChildren, RefObject } from 'react';
1
+ import React$1, { ReactNode, FC, InputHTMLAttributes, ReactElement, CSSProperties, RefObject } from 'react';
2
2
  import { IconProp } from '@fortawesome/fontawesome-svg-core';
3
3
  import { FontAwesomeIconProps } from '@fortawesome/react-fontawesome';
4
4
  import { CSSTransitionProps } from 'react-transition-group/CSSTransition';
5
- import * as react_jsx_runtime from 'react/jsx-runtime';
6
5
 
7
6
  type ButtonSize = 'lg' | 'sm';
8
7
  type ButtonType = 'primary' | 'default' | 'danger' | 'link';
@@ -126,9 +125,24 @@ interface UploadFile {
126
125
  percent?: number;
127
126
  error?: any;
128
127
  }
128
+ interface UploadRequestOptions {
129
+ action: string;
130
+ file: File;
131
+ filename: string;
132
+ headers?: Record<string, string>;
133
+ data?: Record<string, string | Blob>;
134
+ withCredentials?: boolean;
135
+ onProgress: (percent: number) => void;
136
+ onSuccess: (response: any) => void;
137
+ onError: (error: any) => void;
138
+ }
139
+ interface UploadRequestReturn {
140
+ abort: () => void;
141
+ }
129
142
  interface UploadProps {
130
143
  action: string;
131
144
  beforeUpload?: (file: File) => boolean | Promise<File>;
145
+ customRequest?: (options: UploadRequestOptions) => UploadRequestReturn;
132
146
  onprogress?: (percentage: number, file: UploadFile) => void;
133
147
  onSuccess?: (data: any, file: UploadFile) => void;
134
148
  onError?: (err: any, file: UploadFile) => void;
@@ -198,18 +212,8 @@ interface LazyloadProps {
198
212
  }
199
213
  declare const Lazyload: FC<LazyloadProps>;
200
214
 
201
- interface KeepAliveLayoutProps extends PropsWithChildren {
202
- keepPaths: Array<string | RegExp>;
203
- keepElements?: Record<string, ReactNode>;
204
- dropByPath?: (path: string) => void;
205
- }
206
- type KeepAliveContextType = Omit<Required<KeepAliveLayoutProps>, "children">;
207
- declare const KeepAliveContext: React$1.Context<KeepAliveContextType>;
208
- declare function useKeepOutlet(): react_jsx_runtime.JSX.Element;
209
- declare const KeepAliveLayout: FC<KeepAliveLayoutProps>;
210
-
211
215
  declare function useDebounce<T>(value: T, delay?: number): T;
212
216
 
213
217
  declare function useClickOutside(ref: RefObject<HTMLElement>, handler: (event: MouseEvent) => void): void;
214
218
 
215
- export { type AutoCompletProps, AutoComplete, Button, type ButtonSize, type ButtonType, type DataSourceType, type IMenuComponent, Icon, type IconProps, Input, type InputProps, KeepAliveContext, KeepAliveLayout, Lazyload as LazyLoad, TransMenu as Menu, MenuItem, type MenuItemProps, type MenuProps, Progress, type ProgressProps, Signature, SubMenu, type SubMenuProps, type ThemeProps, Transition, Upload, type UploadFile, type UploadProps, VirtualList, useClickOutside, useDebounce, useKeepOutlet };
219
+ export { type AutoCompletProps, AutoComplete, Button, type ButtonSize, type ButtonType, type DataSourceType, type IMenuComponent, Icon, type IconProps, Input, type InputProps, Lazyload as LazyLoad, TransMenu as Menu, MenuItem, type MenuItemProps, type MenuProps, Progress, type ProgressProps, Signature, SubMenu, type SubMenuProps, type ThemeProps, Transition, Upload, type UploadFile, type UploadProps, VirtualList, useClickOutside, useDebounce };
package/dist/index.js CHANGED
@@ -27,15 +27,13 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
27
27
  ));
28
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
29
 
30
- // src/index.ts
31
- var index_exports = {};
32
- __export(index_exports, {
30
+ // src/lib.ts
31
+ var lib_exports = {};
32
+ __export(lib_exports, {
33
33
  AutoComplete: () => autoComplete_default,
34
34
  Button: () => button_default,
35
35
  Icon: () => icon_default,
36
36
  Input: () => input_default,
37
- KeepAliveContext: () => KeepAliveContext,
38
- KeepAliveLayout: () => Keepalive_default,
39
37
  LazyLoad: () => lazyLoad_default,
40
38
  Menu: () => Menu_default,
41
39
  MenuItem: () => menuItem_default,
@@ -46,10 +44,9 @@ __export(index_exports, {
46
44
  Upload: () => upload_default,
47
45
  VirtualList: () => virtualList_default,
48
46
  useClickOutside: () => useClickOutside_default,
49
- useDebounce: () => useDebounce_default,
50
- useKeepOutlet: () => useKeepOutlet
47
+ useDebounce: () => useDebounce_default
51
48
  });
52
- module.exports = __toCommonJS(index_exports);
49
+ module.exports = __toCommonJS(lib_exports);
53
50
 
54
51
  // src/components/Button/button.tsx
55
52
  var import_classnames = __toESM(require("classnames"));
@@ -545,7 +542,6 @@ var autoComplete_default = AutoComplete;
545
542
 
546
543
  // src/components/Upload/upload.tsx
547
544
  var import_react9 = require("react");
548
- var import_axios = __toESM(require("axios"));
549
545
 
550
546
  // src/components/Progress/progress.tsx
551
547
  var import_jsx_runtime9 = require("react/jsx-runtime");
@@ -668,10 +664,57 @@ var dragger_default = Dragger;
668
664
 
669
665
  // src/components/Upload/upload.tsx
670
666
  var import_jsx_runtime12 = require("react/jsx-runtime");
667
+ function defaultRequest(options) {
668
+ const { action, file, filename, headers, data, withCredentials, onProgress, onSuccess, onError } = options;
669
+ const formData = new FormData();
670
+ formData.append(filename, file);
671
+ if (data) {
672
+ Object.keys(data).forEach((key) => formData.append(key, data[key]));
673
+ }
674
+ const xhr = new XMLHttpRequest();
675
+ xhr.upload.addEventListener("progress", (e) => {
676
+ if (e.lengthComputable) {
677
+ const percent = Math.floor(e.loaded * 100 / e.total);
678
+ onProgress(percent);
679
+ }
680
+ });
681
+ xhr.addEventListener("load", () => {
682
+ if (xhr.status >= 200 && xhr.status < 300) {
683
+ let response;
684
+ try {
685
+ response = JSON.parse(xhr.responseText);
686
+ } catch (e) {
687
+ response = xhr.responseText;
688
+ }
689
+ onSuccess(response);
690
+ } else {
691
+ onError(new Error(`Upload failed with status ${xhr.status}`));
692
+ }
693
+ });
694
+ xhr.addEventListener("error", () => {
695
+ onError(new Error("Upload failed due to network error"));
696
+ });
697
+ xhr.addEventListener("abort", () => {
698
+ });
699
+ xhr.open("POST", action, true);
700
+ if (headers) {
701
+ Object.keys(headers).forEach((key) => {
702
+ xhr.setRequestHeader(key, headers[key]);
703
+ });
704
+ }
705
+ if (withCredentials) {
706
+ xhr.withCredentials = true;
707
+ }
708
+ xhr.send(formData);
709
+ return {
710
+ abort: () => xhr.abort()
711
+ };
712
+ }
671
713
  var Upload = (props) => {
672
714
  const {
673
715
  action,
674
716
  beforeUpload,
717
+ customRequest,
675
718
  onprogress,
676
719
  onSuccess,
677
720
  onError,
@@ -698,6 +741,7 @@ var Upload = (props) => {
698
741
  const abortRecord = (0, import_react9.useRef)({});
699
742
  const FileRecord = (0, import_react9.useRef)(/* @__PURE__ */ new Set());
700
743
  const [fileList, setFileList] = (0, import_react9.useState)([]);
744
+ const request = customRequest || defaultRequest;
701
745
  const handleClick = () => {
702
746
  var _a;
703
747
  (_a = fileInput.current) == null ? void 0 : _a.click();
@@ -709,21 +753,19 @@ var Upload = (props) => {
709
753
  }
710
754
  let concurrency = 0;
711
755
  const queue = [];
712
- function AskTask(task) {
713
- return new Promise(() => {
714
- const executeRequest = () => {
715
- concurrency++;
716
- task[0]().then(() => {
717
- concurrency--;
718
- nextTask();
719
- });
720
- };
721
- if (concurrency < limit) {
722
- executeRequest();
723
- } else {
724
- queue.push([executeRequest, task[1]]);
725
- }
726
- });
756
+ function askTask(task) {
757
+ const executeRequest = () => {
758
+ concurrency++;
759
+ task[0]().then(() => {
760
+ concurrency--;
761
+ nextTask();
762
+ });
763
+ };
764
+ if (concurrency < limit) {
765
+ executeRequest();
766
+ } else {
767
+ queue.push([executeRequest, task[1]]);
768
+ }
727
769
  }
728
770
  function nextTask() {
729
771
  if (concurrency < limit && queue.length > 0) {
@@ -738,7 +780,7 @@ var Upload = (props) => {
738
780
  }
739
781
  let uidCounter = 0;
740
782
  function dealFile(file) {
741
- let _file = {
783
+ const _file = {
742
784
  uid: `upload-file-${Date.now()}-${uidCounter++}`,
743
785
  size: file.size,
744
786
  name: file.name,
@@ -754,54 +796,40 @@ var Upload = (props) => {
754
796
  isSame = true;
755
797
  }
756
798
  }
757
- if (isSame) return prevList;
799
+ if (isSame) return [...prevList];
758
800
  return [_file, ...prevList];
759
801
  });
760
- const formData = new FormData();
761
- formData.append(name || "file", file);
762
- data && Object.keys(data).forEach((key) => formData.append(key, data[key]));
763
- const controller = new AbortController();
764
- abortRecord.current = { ...abortRecord.current, [_file.uid]: controller };
765
- return [_file, formData, controller];
802
+ return _file;
766
803
  }
767
- const post = (_file, formData, controller, try_nums) => {
804
+ const post = (_file, tryNums) => {
768
805
  return new Promise((resolve) => {
769
- import_axios.default.post(action, formData, {
770
- headers: {
771
- ...headers,
772
- "Content-Type": "multipart/form-data"
773
- },
806
+ const requestReturn = request({
807
+ action,
808
+ file: _file.raw,
809
+ filename: name || "file",
810
+ headers,
811
+ data,
774
812
  withCredentials,
775
- signal: controller.signal,
776
- onUploadProgress: (e) => {
777
- let percentage = Math.floor(e.loaded * 100 / e.total || 0);
778
- if (percentage < 100) {
779
- updateFileList(_file, {
780
- percent: percentage,
781
- status: "uploading"
782
- });
813
+ onProgress: (percent) => {
814
+ if (percent < 100) {
815
+ updateFileList(_file, { percent, status: "uploading" });
783
816
  _file.status = "uploading";
784
- _file.percent = percentage;
785
- onprogress == null ? void 0 : onprogress(percentage, _file);
817
+ _file.percent = percent;
818
+ onprogress == null ? void 0 : onprogress(percent, _file);
786
819
  }
787
- }
788
- }).then((res) => {
789
- updateFileList(_file, { status: "success", response: res.data });
790
- _file.status = "success";
791
- _file.response = res.data;
792
- onSuccess == null ? void 0 : onSuccess(res.data, _file);
793
- onChange == null ? void 0 : onChange(_file);
794
- resolve("success");
795
- }).catch((err) => {
796
- if (import_axios.default.isCancel(err)) {
797
- resolve("cancel");
798
- } else {
799
- if (try_nums < failednums) {
800
- const [_file_new, formData2, controller2] = dealFile(_file.raw);
801
- AskTask([
802
- () => post(_file_new, formData2, controller2, try_nums + 1),
803
- _file.name
804
- ]);
820
+ },
821
+ onSuccess: (response) => {
822
+ updateFileList(_file, { status: "success", response });
823
+ _file.status = "success";
824
+ _file.response = response;
825
+ onSuccess == null ? void 0 : onSuccess(response, _file);
826
+ onChange == null ? void 0 : onChange(_file);
827
+ resolve("success");
828
+ },
829
+ onError: (err) => {
830
+ if (tryNums < failednums) {
831
+ const newFile = dealFile(_file.raw);
832
+ askTask([() => post(newFile, tryNums + 1), _file.name]);
805
833
  }
806
834
  updateFileList(_file, { status: "error", error: err });
807
835
  _file.status = "error";
@@ -811,9 +839,10 @@ var Upload = (props) => {
811
839
  resolve("failed");
812
840
  }
813
841
  });
842
+ abortRecord.current[_file.uid] = requestReturn;
814
843
  });
815
844
  };
816
- let postFiles = Array.from(files);
845
+ const postFiles = Array.from(files);
817
846
  postFiles.forEach((file) => {
818
847
  if (maxsize && file.size > maxsize * 1024 * 1024) {
819
848
  onExceed == null ? void 0 : onExceed(`\u6587\u4EF6\u5927\u5C0F\u4E0D\u80FD\u8D85\u8FC7${maxsize}Mb`);
@@ -821,18 +850,18 @@ var Upload = (props) => {
821
850
  }
822
851
  FileRecord.current.add(file.name);
823
852
  if (!beforeUpload) {
824
- const [_file, formData, source] = dealFile(file);
825
- AskTask([() => post(_file, formData, source, 0), file.name]);
853
+ const _file = dealFile(file);
854
+ askTask([() => post(_file, 0), file.name]);
826
855
  } else {
827
856
  const result = beforeUpload(file);
828
857
  if (result && result instanceof Promise) {
829
- result.then((processedFile) => {
830
- const [_file, formData, source] = dealFile(file);
831
- AskTask([() => post(_file, formData, source, 0), file.name]);
858
+ result.then(() => {
859
+ const _file = dealFile(file);
860
+ askTask([() => post(_file, 0), file.name]);
832
861
  });
833
862
  } else if (result) {
834
- const [_file, formData, source] = dealFile(file);
835
- AskTask([() => post(_file, formData, source, 0), file.name]);
863
+ const _file = dealFile(file);
864
+ askTask([() => post(_file, 0), file.name]);
836
865
  }
837
866
  }
838
867
  });
@@ -1109,74 +1138,12 @@ var Lazyload = (props) => {
1109
1138
  return /* @__PURE__ */ (0, import_jsx_runtime15.jsx)("div", { ref: containerRef, className, style: styles, children: visible ? children : placeholder });
1110
1139
  };
1111
1140
  var lazyLoad_default = Lazyload;
1112
-
1113
- // src/components/Keepalive/Keepalive.tsx
1114
- var import_react13 = require("react");
1115
- var import_react_router_dom = require("react-router-dom");
1116
- var import_jsx_runtime16 = require("react/jsx-runtime");
1117
- var KeepAliveContext = (0, import_react13.createContext)({
1118
- keepPaths: [],
1119
- keepElements: {},
1120
- dropByPath() {
1121
- }
1122
- });
1123
- var isKeepPath = (keepPaths, path) => {
1124
- for (let i = 0; i < keepPaths.length; i++) {
1125
- let item = keepPaths[i];
1126
- if (item === path) return true;
1127
- if (item instanceof RegExp && item.test(path)) return true;
1128
- if (typeof item === "string" && item.toLowerCase() === path) return true;
1129
- }
1130
- return false;
1131
- };
1132
- function useKeepOutlet() {
1133
- const location = (0, import_react_router_dom.useLocation)();
1134
- const element = (0, import_react_router_dom.useOutlet)();
1135
- const { keepElements, keepPaths } = (0, import_react13.useContext)(KeepAliveContext);
1136
- const isKeep = isKeepPath(keepPaths, location.pathname);
1137
- const keepElementsRef = (0, import_react13.useRef)(keepElements);
1138
- keepElementsRef.current = keepElements;
1139
- (0, import_react13.useEffect)(() => {
1140
- if (isKeep && element) {
1141
- keepElementsRef.current[location.pathname] = element;
1142
- }
1143
- });
1144
- return /* @__PURE__ */ (0, import_jsx_runtime16.jsxs)(import_jsx_runtime16.Fragment, { children: [
1145
- Object.entries(keepElements).map(([pathname, el]) => /* @__PURE__ */ (0, import_jsx_runtime16.jsx)(
1146
- "div",
1147
- {
1148
- style: {
1149
- height: "100%",
1150
- width: "100%",
1151
- position: "relative",
1152
- overflow: "hidden auto"
1153
- },
1154
- className: "keep-alive-page",
1155
- hidden: !(0, import_react_router_dom.matchPath)(location.pathname, pathname),
1156
- children: el
1157
- },
1158
- pathname
1159
- )),
1160
- !isKeep && element
1161
- ] });
1162
- }
1163
- var KeepAliveLayout = (props) => {
1164
- const { keepPaths, children } = props;
1165
- const [keepElements] = (0, import_react13.useState)({});
1166
- const dropByPath = (path) => {
1167
- delete keepElements[path];
1168
- };
1169
- return /* @__PURE__ */ (0, import_jsx_runtime16.jsx)(KeepAliveContext.Provider, { value: { keepPaths, keepElements, dropByPath }, children });
1170
- };
1171
- var Keepalive_default = KeepAliveLayout;
1172
1141
  // Annotate the CommonJS export names for ESM import in node:
1173
1142
  0 && (module.exports = {
1174
1143
  AutoComplete,
1175
1144
  Button,
1176
1145
  Icon,
1177
1146
  Input,
1178
- KeepAliveContext,
1179
- KeepAliveLayout,
1180
1147
  LazyLoad,
1181
1148
  Menu,
1182
1149
  MenuItem,
@@ -1187,7 +1154,6 @@ var Keepalive_default = KeepAliveLayout;
1187
1154
  Upload,
1188
1155
  VirtualList,
1189
1156
  useClickOutside,
1190
- useDebounce,
1191
- useKeepOutlet
1157
+ useDebounce
1192
1158
  });
1193
1159
  //# sourceMappingURL=index.js.map