@lemon-fe/hooks 0.0.23 → 0.0.24
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/es/config.d.ts +12 -0
- package/es/config.js +29 -0
- package/es/index.d.ts +2 -0
- package/es/index.js +3 -1
- package/es/usePreload/index.d.ts +9 -0
- package/es/usePreload/index.js +74 -0
- package/es/usePrimaryColor/index.js +14 -13
- package/es/useRequest/index.d.ts +0 -9
- package/es/useRequest/index.js +5 -76
- package/package.json +3 -5
package/es/config.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ReactElement } from 'react';
|
|
2
|
+
interface Opts<ToastReturnType = any> {
|
|
3
|
+
showToast(): ToastReturnType;
|
|
4
|
+
cancelToast(toastReturn: ToastReturnType): void;
|
|
5
|
+
showError(err: Error): void;
|
|
6
|
+
showLoading(): ReactElement | null;
|
|
7
|
+
errorResult(err: Error, refresh: () => void): ReactElement | null;
|
|
8
|
+
setPrimaryColor(val: string): void;
|
|
9
|
+
}
|
|
10
|
+
export declare const hooksOpts: Opts;
|
|
11
|
+
export declare function config<ToastReturnType = any>(opts: Partial<Opts<ToastReturnType>>): void;
|
|
12
|
+
export {};
|
package/es/config.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
var warning = function warning(name) {
|
|
2
|
+
console.warn("implement hooks ".concat(name));
|
|
3
|
+
};
|
|
4
|
+
|
|
5
|
+
export var hooksOpts = {
|
|
6
|
+
showToast: function showToast() {
|
|
7
|
+
warning('showToast');
|
|
8
|
+
},
|
|
9
|
+
cancelToast: function cancelToast() {
|
|
10
|
+
warning('cancelToast');
|
|
11
|
+
},
|
|
12
|
+
showError: function showError() {
|
|
13
|
+
warning('showError');
|
|
14
|
+
},
|
|
15
|
+
errorResult: function errorResult() {
|
|
16
|
+
warning('errorResult');
|
|
17
|
+
return null;
|
|
18
|
+
},
|
|
19
|
+
setPrimaryColor: function setPrimaryColor() {
|
|
20
|
+
warning('setPrimaryColor');
|
|
21
|
+
},
|
|
22
|
+
showLoading: function showLoading() {
|
|
23
|
+
warning('showLoading');
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
export function config(opts) {
|
|
28
|
+
Object.assign(hooksOpts, opts);
|
|
29
|
+
}
|
package/es/index.d.ts
CHANGED
|
@@ -4,3 +4,5 @@ export { default as useInterval } from './useInterval';
|
|
|
4
4
|
export { default as usePrimaryColor } from './usePrimaryColor';
|
|
5
5
|
export { default as useRequest } from './useRequest';
|
|
6
6
|
export { default as useAsyncEffect } from './useAsyncEffect';
|
|
7
|
+
export { default as usePreload } from './usePreload';
|
|
8
|
+
export { config } from './config';
|
package/es/index.js
CHANGED
|
@@ -3,4 +3,6 @@ export { default as useThrottle } from './useThrottle';
|
|
|
3
3
|
export { default as useInterval } from './useInterval';
|
|
4
4
|
export { default as usePrimaryColor } from './usePrimaryColor';
|
|
5
5
|
export { default as useRequest } from './useRequest';
|
|
6
|
-
export { default as useAsyncEffect } from './useAsyncEffect';
|
|
6
|
+
export { default as useAsyncEffect } from './useAsyncEffect';
|
|
7
|
+
export { default as usePreload } from './usePreload';
|
|
8
|
+
export { config } from './config';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ReactElement } from 'react';
|
|
2
|
+
export default function usePreload<TargetProps>(opts: {
|
|
3
|
+
request: () => Promise<TargetProps>;
|
|
4
|
+
onSuccess?: (data: TargetProps) => void;
|
|
5
|
+
} | (() => Promise<TargetProps>), deps?: ReadonlyArray<any>): {
|
|
6
|
+
refresh: () => void;
|
|
7
|
+
data: TargetProps | null;
|
|
8
|
+
mask: ReactElement<any, string | import("react").JSXElementConstructor<any>> | null;
|
|
9
|
+
};
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
2
|
+
|
|
3
|
+
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
4
|
+
|
|
5
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
6
|
+
|
|
7
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
8
|
+
|
|
9
|
+
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
|
|
10
|
+
|
|
11
|
+
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
12
|
+
|
|
13
|
+
import { useEffect, useState } from 'react';
|
|
14
|
+
import useRequest from '../useRequest';
|
|
15
|
+
import { hooksOpts } from '../config';
|
|
16
|
+
export default function usePreload(opts, deps) {
|
|
17
|
+
var _useState = useState(null),
|
|
18
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
19
|
+
data = _useState2[0],
|
|
20
|
+
setData = _useState2[1];
|
|
21
|
+
|
|
22
|
+
var _useState3 = useState(null),
|
|
23
|
+
_useState4 = _slicedToArray(_useState3, 2),
|
|
24
|
+
error = _useState4[0],
|
|
25
|
+
setError = _useState4[1];
|
|
26
|
+
|
|
27
|
+
var request;
|
|
28
|
+
|
|
29
|
+
var _onSuccess;
|
|
30
|
+
|
|
31
|
+
if (typeof opts === 'function') {
|
|
32
|
+
request = opts;
|
|
33
|
+
} else {
|
|
34
|
+
request = opts.request;
|
|
35
|
+
_onSuccess = opts.onSuccess;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
var _useRequest = useRequest(request, {
|
|
39
|
+
onSuccess: function onSuccess(res) {
|
|
40
|
+
if (_onSuccess) {
|
|
41
|
+
_onSuccess(res);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
setData(res);
|
|
45
|
+
},
|
|
46
|
+
onError: function onError(err) {
|
|
47
|
+
setError(err);
|
|
48
|
+
}
|
|
49
|
+
}),
|
|
50
|
+
run = _useRequest.run;
|
|
51
|
+
|
|
52
|
+
useEffect(function () {
|
|
53
|
+
run();
|
|
54
|
+
}, deps ? deps : []);
|
|
55
|
+
|
|
56
|
+
var handleRefresh = function handleRefresh() {
|
|
57
|
+
setError(null);
|
|
58
|
+
run();
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
var mask = null;
|
|
62
|
+
|
|
63
|
+
if (error) {
|
|
64
|
+
mask = hooksOpts.errorResult(error, handleRefresh);
|
|
65
|
+
} else if (data === null) {
|
|
66
|
+
mask = hooksOpts.showLoading();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return {
|
|
70
|
+
refresh: handleRefresh,
|
|
71
|
+
data: data,
|
|
72
|
+
mask: mask
|
|
73
|
+
};
|
|
74
|
+
}
|
|
@@ -11,28 +11,29 @@ function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Sy
|
|
|
11
11
|
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
12
12
|
|
|
13
13
|
import { useState, useEffect } from 'react';
|
|
14
|
-
import { ConfigProvider } from 'antd';
|
|
15
14
|
import isEqual from 'lodash/isEqual';
|
|
16
15
|
import colorString from 'color-string';
|
|
17
16
|
import Subject from '../subject';
|
|
18
|
-
|
|
17
|
+
import { hooksOpts } from '../config';
|
|
19
18
|
var subject = Subject();
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
});
|
|
19
|
+
var primaryColor = '#fff';
|
|
20
|
+
|
|
21
|
+
if (document) {
|
|
22
|
+
primaryColor = getComputedStyle(document.documentElement).getPropertyValue('--ant-primary-color').trim();
|
|
23
|
+
subject.subscribe({
|
|
24
|
+
next: function next(color) {
|
|
25
|
+
primaryColor = color;
|
|
26
|
+
document.documentElement.style.setProperty('--lemon-primary-color', "".concat(colorString.get.rgb(color).slice(0, 3).join(',')));
|
|
27
|
+
hooksOpts.setPrimaryColor(color);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
31
|
/**
|
|
32
32
|
* 设置主题色
|
|
33
33
|
* @param color 主色调 必须是hex或者rgb
|
|
34
34
|
*/
|
|
35
35
|
|
|
36
|
+
|
|
36
37
|
export default function usePrimaryColor() {
|
|
37
38
|
var _useState = useState(primaryColor),
|
|
38
39
|
_useState2 = _slicedToArray(_useState, 2),
|
package/es/useRequest/index.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
1
|
/**
|
|
3
2
|
* 发起网络请求, 主要面向场景: 提交单据、点击列表打开详情页前请求数据,自动捕获异常
|
|
4
3
|
* @param request 网络请求方法
|
|
@@ -20,11 +19,3 @@ export default function useRequest<T extends (...args: any[]) => PromiseLike<any
|
|
|
20
19
|
run: ((...args: Parameters<T>) => Promise<void>) | import("lodash").DebouncedFunc<(...args: Parameters<T>) => Promise<void>>;
|
|
21
20
|
loading: boolean;
|
|
22
21
|
};
|
|
23
|
-
export declare function usePreload<TargetProps>(opts: {
|
|
24
|
-
request: () => Promise<TargetProps>;
|
|
25
|
-
onSuccess?: (data: TargetProps) => void;
|
|
26
|
-
} | (() => Promise<TargetProps>), deps?: ReadonlyArray<any>): {
|
|
27
|
-
refresh: () => void;
|
|
28
|
-
data: TargetProps | null;
|
|
29
|
-
mask: React.ReactElement<any, string | React.JSXElementConstructor<any>> | null;
|
|
30
|
-
};
|
package/es/useRequest/index.js
CHANGED
|
@@ -14,10 +14,10 @@ function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Sy
|
|
|
14
14
|
|
|
15
15
|
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
16
16
|
|
|
17
|
-
import
|
|
18
|
-
import { message, Result, Button, Spin } from 'antd';
|
|
17
|
+
import { useRef, useEffect, useMemo, useState } from 'react';
|
|
19
18
|
import debounce from 'lodash/debounce';
|
|
20
19
|
import throttle from 'lodash/debounce';
|
|
20
|
+
import { hooksOpts } from '../config';
|
|
21
21
|
/**
|
|
22
22
|
* 发起网络请求, 主要面向场景: 提交单据、点击列表打开详情页前请求数据,自动捕获异常
|
|
23
23
|
* @param request 网络请求方法
|
|
@@ -50,7 +50,7 @@ export default function useRequest(request, options) {
|
|
|
50
50
|
|
|
51
51
|
var start = function start() {
|
|
52
52
|
if (toast && !toastRef.current) {
|
|
53
|
-
toastRef.current =
|
|
53
|
+
toastRef.current = hooksOpts.showToast() || true;
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
if (loadingState) {
|
|
@@ -60,7 +60,7 @@ export default function useRequest(request, options) {
|
|
|
60
60
|
|
|
61
61
|
var end = function end() {
|
|
62
62
|
if (toastRef.current) {
|
|
63
|
-
toastRef.current
|
|
63
|
+
hooksOpts.cancelToast(toastRef.current);
|
|
64
64
|
toastRef.current = null;
|
|
65
65
|
}
|
|
66
66
|
|
|
@@ -114,7 +114,7 @@ export default function useRequest(request, options) {
|
|
|
114
114
|
if (onError) {
|
|
115
115
|
onError(_context.t0);
|
|
116
116
|
} else {
|
|
117
|
-
|
|
117
|
+
hooksOpts.showError(_context.t0);
|
|
118
118
|
}
|
|
119
119
|
|
|
120
120
|
case 13:
|
|
@@ -156,75 +156,4 @@ export default function useRequest(request, options) {
|
|
|
156
156
|
run: formatedRun,
|
|
157
157
|
loading: loading
|
|
158
158
|
};
|
|
159
|
-
}
|
|
160
|
-
export function usePreload(opts, deps) {
|
|
161
|
-
var _useState3 = useState(null),
|
|
162
|
-
_useState4 = _slicedToArray(_useState3, 2),
|
|
163
|
-
data = _useState4[0],
|
|
164
|
-
setData = _useState4[1];
|
|
165
|
-
|
|
166
|
-
var _useState5 = useState(null),
|
|
167
|
-
_useState6 = _slicedToArray(_useState5, 2),
|
|
168
|
-
error = _useState6[0],
|
|
169
|
-
setError = _useState6[1];
|
|
170
|
-
|
|
171
|
-
var request;
|
|
172
|
-
|
|
173
|
-
var _onSuccess;
|
|
174
|
-
|
|
175
|
-
if (typeof opts === 'function') {
|
|
176
|
-
request = opts;
|
|
177
|
-
} else {
|
|
178
|
-
request = opts.request;
|
|
179
|
-
_onSuccess = opts.onSuccess;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
var _useRequest = useRequest(request, {
|
|
183
|
-
onSuccess: function onSuccess(res) {
|
|
184
|
-
if (_onSuccess) {
|
|
185
|
-
_onSuccess(res);
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
setData(res);
|
|
189
|
-
},
|
|
190
|
-
onError: function onError(err) {
|
|
191
|
-
setError(err);
|
|
192
|
-
}
|
|
193
|
-
}),
|
|
194
|
-
run = _useRequest.run;
|
|
195
|
-
|
|
196
|
-
useEffect(function () {
|
|
197
|
-
run();
|
|
198
|
-
}, deps ? deps : []);
|
|
199
|
-
|
|
200
|
-
var handleRefresh = function handleRefresh() {
|
|
201
|
-
setError(null);
|
|
202
|
-
run();
|
|
203
|
-
};
|
|
204
|
-
|
|
205
|
-
var mask = null;
|
|
206
|
-
|
|
207
|
-
if (error) {
|
|
208
|
-
mask = /*#__PURE__*/React.createElement(Result, {
|
|
209
|
-
status: "500",
|
|
210
|
-
title: "500",
|
|
211
|
-
subTitle: error.message,
|
|
212
|
-
extra: /*#__PURE__*/React.createElement(Button, {
|
|
213
|
-
type: "primary",
|
|
214
|
-
onClick: handleRefresh
|
|
215
|
-
}, "\u91CD\u65B0\u52A0\u8F7D")
|
|
216
|
-
});
|
|
217
|
-
} else if (data === null) {
|
|
218
|
-
mask = /*#__PURE__*/React.createElement("div", {
|
|
219
|
-
className: "lemon-page-loading"
|
|
220
|
-
}, /*#__PURE__*/React.createElement(Spin, {
|
|
221
|
-
size: "large"
|
|
222
|
-
}));
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
return {
|
|
226
|
-
refresh: handleRefresh,
|
|
227
|
-
data: data,
|
|
228
|
-
mask: mask
|
|
229
|
-
};
|
|
230
159
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lemon-fe/hooks",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.24",
|
|
4
4
|
"description": "> TODO: description",
|
|
5
5
|
"author": "鲁盛杰 <lusj@cnlemon.net>",
|
|
6
6
|
"homepage": "",
|
|
@@ -16,14 +16,12 @@
|
|
|
16
16
|
"registry": "https://registry.npmjs.org"
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"antd": "^4.17.0",
|
|
20
19
|
"color-string": "^1.9.0",
|
|
21
20
|
"lodash": "^4.17.21",
|
|
22
|
-
"react": "^17.0.2"
|
|
23
|
-
"react-dom": "^17.0.2"
|
|
21
|
+
"react": "^17.0.2"
|
|
24
22
|
},
|
|
25
23
|
"devDependencies": {
|
|
26
24
|
"@types/color-string": "^1.5.2"
|
|
27
25
|
},
|
|
28
|
-
"gitHead": "
|
|
26
|
+
"gitHead": "85f8df424cf24a01973162690b65a2747ee67af5"
|
|
29
27
|
}
|