@wzyjs/hooks 0.0.31 → 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ export * from 'ahooks';
2
+ export { useCopyToClipboard, useCookie, useUpdate } from 'react-use';
3
+ export * from "./useRequestPro";
4
+ export * from "./useEffectValue";
5
+ export * from "./useElementScrollVisible";
6
+ export * from "./useHideFooter";
7
+ export * from "./useVisibleInfo";
@@ -0,0 +1,18 @@
1
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
2
+ 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."); }
3
+ 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); }
4
+ 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; }
5
+ function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
6
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
7
+ import { useEffect, useState } from 'react';
8
+ export var useEffectValue = function useEffectValue(fn) {
9
+ var deps = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
10
+ var _useState = useState(),
11
+ _useState2 = _slicedToArray(_useState, 2),
12
+ value = _useState2[0],
13
+ setValue = _useState2[1];
14
+ useEffect(function () {
15
+ return setValue(fn());
16
+ }, deps);
17
+ return [value, setValue];
18
+ };
@@ -0,0 +1,52 @@
1
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
2
+ 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."); }
3
+ 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); }
4
+ 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; }
5
+ function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
6
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
7
+ import { useEffect, useState } from 'react';
8
+
9
+ // 判断某元素滚动时,指定元素是否显示
10
+ export var useElementScrollVisible = function useElementScrollVisible(scroll, target) {
11
+ var $scroll = document.querySelector(scroll) || {};
12
+ var $scrollRect = $scroll.getBoundingClientRect();
13
+ var _useState = useState(false),
14
+ _useState2 = _slicedToArray(_useState, 2),
15
+ visible = _useState2[0],
16
+ setVisible = _useState2[1];
17
+ var _useState3 = useState(''),
18
+ _useState4 = _slicedToArray(_useState3, 2),
19
+ direction = _useState4[0],
20
+ setDirection = _useState4[1];
21
+ var _useState5 = useState(''),
22
+ _useState6 = _slicedToArray(_useState5, 2),
23
+ rect = _useState6[0],
24
+ setRect = _useState6[1];
25
+ useEffect(function () {
26
+ var fn = function fn() {
27
+ var $target = document.querySelector(target) || {};
28
+ var rect = $target.getBoundingClientRect();
29
+ setRect(rect);
30
+ if (rect.bottom < $scrollRect.top) {
31
+ setDirection('top');
32
+ setVisible(false);
33
+ } else if (rect.top > $scrollRect.bottom) {
34
+ setDirection('bottom');
35
+ setVisible(false);
36
+ } else {
37
+ setDirection('show');
38
+ setVisible(true);
39
+ }
40
+ };
41
+ fn();
42
+ $scroll.addEventListener('scroll', fn);
43
+ return function () {
44
+ return $scroll.removeEventListener('scroll', fn);
45
+ };
46
+ }, []);
47
+ return {
48
+ visible: visible,
49
+ direction: direction,
50
+ rect: rect
51
+ };
52
+ };
@@ -0,0 +1,13 @@
1
+ import { useEffect } from 'react';
2
+
3
+ // 进入时隐藏指定元素,销毁时恢复该元素
4
+ export var useHideFooter = function useHideFooter() {
5
+ var el = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '.ant-layout-footer';
6
+ useEffect(function () {
7
+ var e = document.querySelector(el);
8
+ e.style.display = 'none';
9
+ return function () {
10
+ e.style.display = 'block';
11
+ };
12
+ }, []);
13
+ };
@@ -0,0 +1,66 @@
1
+ import { message } from 'antd';
2
+ import { useRequest } from 'ahooks';
3
+ import { noop, omit } from '@wzyjs/utils';
4
+
5
+ // 在 useRequest 的基础上扩展的功能
6
+
7
+ // 取出 data 里的 data
8
+
9
+ var defaultResult = {
10
+ loading: false,
11
+ data: undefined,
12
+ error: undefined,
13
+ params: [],
14
+ cancel: noop,
15
+ refresh: noop,
16
+ refreshAsync: noop,
17
+ run: noop,
18
+ runAsync: noop,
19
+ mutate: noop
20
+ };
21
+ export var useRequestPro = function useRequestPro(reqPromise, userOptions) {
22
+ var _state$data;
23
+ if (!reqPromise) {
24
+ return defaultResult;
25
+ }
26
+ var _ref = userOptions || {},
27
+ _ref$alertErrorMessag = _ref.alertErrorMessage,
28
+ alertErrorMessage = _ref$alertErrorMessag === void 0 ? true : _ref$alertErrorMessag,
29
+ _ref$alertSuccessMess = _ref.alertSuccessMessage,
30
+ alertSuccessMessage = _ref$alertSuccessMess === void 0 ? false : _ref$alertSuccessMess;
31
+ var defaultOptions = {
32
+ cacheKey: String(reqPromise),
33
+ staleTime: 5000,
34
+ debounceWait: 300,
35
+ throttleWait: 300,
36
+ onError: function onError(err, params) {
37
+ var _userOptions$onError;
38
+ if (alertErrorMessage) {
39
+ message.error(typeof alertErrorMessage === 'string' ? alertErrorMessage : err.message || '请求失败');
40
+ }
41
+ userOptions === null || userOptions === void 0 ? void 0 : (_userOptions$onError = userOptions.onError) === null || _userOptions$onError === void 0 ? void 0 : _userOptions$onError.call(userOptions, err, params);
42
+ },
43
+ onSuccess: function onSuccess(res, params) {
44
+ var _userOptions$onSucces;
45
+ // 如果失败,则还是调用 onError
46
+ if (!(res !== null && res !== void 0 && res.success)) {
47
+ defaultOptions.onError(new Error(res === null || res === void 0 ? void 0 : res.message), params);
48
+ return;
49
+ }
50
+
51
+ // 如果成功,并且需要自动弹出提示
52
+ if (alertSuccessMessage) {
53
+ message.success(typeof alertSuccessMessage === 'string' ? alertSuccessMessage : res.message || '请求成功');
54
+ }
55
+ userOptions === null || userOptions === void 0 ? void 0 : (_userOptions$onSucces = userOptions.onSuccess) === null || _userOptions$onSucces === void 0 ? void 0 : _userOptions$onSucces.call(userOptions, res, params);
56
+ }
57
+ };
58
+
59
+ // 用户配置与默认配置合并
60
+ var options = Object.assign(defaultOptions, omit(userOptions, ['onSuccess', 'onError']));
61
+
62
+ // 取出 data 里的 data
63
+ var state = useRequest(reqPromise, options);
64
+ state.data = (_state$data = state.data) === null || _state$data === void 0 ? void 0 : _state$data.data;
65
+ return state;
66
+ };
@@ -0,0 +1,32 @@
1
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
2
+ 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."); }
3
+ 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); }
4
+ 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; }
5
+ function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
6
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
7
+ import { useState } from 'react';
8
+
9
+ // visible和info的集合体
10
+ export var useVisibleInfo = function useVisibleInfo() {
11
+ var v = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
12
+ var i = arguments.length > 1 ? arguments[1] : undefined;
13
+ var _useState = useState(v),
14
+ _useState2 = _slicedToArray(_useState, 2),
15
+ visible = _useState2[0],
16
+ setVisible = _useState2[1];
17
+ var _useState3 = useState(i),
18
+ _useState4 = _slicedToArray(_useState3, 2),
19
+ info = _useState4[0],
20
+ setInfo = _useState4[1];
21
+ return [visible, {
22
+ setTrue: function setTrue() {
23
+ return setVisible(true);
24
+ },
25
+ setFalse: function setFalse() {
26
+ return setVisible(false);
27
+ },
28
+ toggle: function toggle() {
29
+ return setVisible(!visible);
30
+ }
31
+ }, [info, setInfo]];
32
+ };
package/package.json CHANGED
@@ -1,28 +1,33 @@
1
1
  {
2
2
  "name": "@wzyjs/hooks",
3
- "version": "0.0.31",
3
+ "version": "0.1.0",
4
4
  "description": "description",
5
5
  "author": "wzy",
6
6
  "license": "ISC",
7
- "main": "dist/index.js",
8
- "typings": "dist/index.d.ts",
9
7
  "scripts": {
10
- "dev": "tsc -w",
11
- "build": "tsc"
8
+ "dev": "father dev",
9
+ "build": "father build"
10
+ },
11
+ "exports": {
12
+ ".": {
13
+ "import": "./dist/esm/index.js",
14
+ "require": "./dist/cjs/index.js"
15
+ }
12
16
  },
13
17
  "files": [
14
18
  "dist"
15
19
  ],
16
20
  "dependencies": {
17
- "@wzyjs/types": "^0.0.31",
18
- "@wzyjs/utils": "^0.0.31",
21
+ "@wzyjs/types": "^0.1.0",
22
+ "@wzyjs/utils": "^0.1.0",
19
23
  "ahooks": "^3.7.3",
20
24
  "antd": "^5.6.2",
21
25
  "react-use": "^17.4.0"
22
26
  },
23
27
  "devDependencies": {
24
28
  "@types/node": "^18.11.17",
25
- "@types/react": "latest"
29
+ "@types/react": "latest",
30
+ "father": "^4.2.3"
26
31
  },
27
32
  "publishConfig": {
28
33
  "access": "public"
@@ -31,5 +36,5 @@
31
36
  "type": "git",
32
37
  "url": "https://gitee.com/wang-zhenyu/work.git"
33
38
  },
34
- "gitHead": "eb60e54549c1be64d94b0a4864dacd1f4ed97473"
39
+ "gitHead": "3efe774801f46de9c70693ace99d01eb7fb06da1"
35
40
  }
package/dist/index.d.ts DELETED
@@ -1,7 +0,0 @@
1
- export * from 'ahooks';
2
- export { useCopyToClipboard, useCookie, useUpdate } from 'react-use';
3
- export * from './useRequestPro';
4
- export * from './useEffectValue';
5
- export * from './useElementScrollVisible';
6
- export * from './useHideFooter';
7
- export * from './useVisibleInfo';
package/dist/index.js DELETED
@@ -1,7 +0,0 @@
1
- export * from 'ahooks';
2
- export { useCopyToClipboard, useCookie, useUpdate } from 'react-use';
3
- export * from './useRequestPro';
4
- export * from './useEffectValue';
5
- export * from './useElementScrollVisible';
6
- export * from './useHideFooter';
7
- export * from './useVisibleInfo';
@@ -1,2 +0,0 @@
1
- /// <reference types="react" />
2
- export declare const useEffectValue: (fn: any, deps?: never[]) => (import("react").Dispatch<import("react").SetStateAction<undefined>> | undefined)[];
@@ -1,6 +0,0 @@
1
- import { useEffect, useState } from 'react';
2
- export const useEffectValue = (fn, deps = []) => {
3
- const [value, setValue] = useState();
4
- useEffect(() => setValue(fn()), deps);
5
- return [value, setValue];
6
- };
@@ -1,5 +0,0 @@
1
- export declare const useElementScrollVisible: (scroll: any, target: any) => {
2
- visible: boolean;
3
- direction: string;
4
- rect: string;
5
- };
@@ -1,36 +0,0 @@
1
- import { useEffect, useState } from 'react';
2
- // 判断某元素滚动时,指定元素是否显示
3
- export const useElementScrollVisible = (scroll, target) => {
4
- const $scroll = document.querySelector(scroll) || {};
5
- const $scrollRect = $scroll.getBoundingClientRect();
6
- const [visible, setVisible] = useState(false);
7
- const [direction, setDirection] = useState('');
8
- const [rect, setRect] = useState('');
9
- useEffect(() => {
10
- const fn = () => {
11
- const $target = document.querySelector(target) || {};
12
- const rect = $target.getBoundingClientRect();
13
- setRect(rect);
14
- if (rect.bottom < $scrollRect.top) {
15
- setDirection('top');
16
- setVisible(false);
17
- }
18
- else if (rect.top > $scrollRect.bottom) {
19
- setDirection('bottom');
20
- setVisible(false);
21
- }
22
- else {
23
- setDirection('show');
24
- setVisible(true);
25
- }
26
- };
27
- fn();
28
- $scroll.addEventListener('scroll', fn);
29
- return () => $scroll.removeEventListener('scroll', fn);
30
- }, []);
31
- return {
32
- visible,
33
- direction,
34
- rect,
35
- };
36
- };
@@ -1,2 +0,0 @@
1
- /// <reference types="react" />
2
- export declare const useFormRef: <Values>() => import("react").MutableRefObject<any>;
@@ -1,4 +0,0 @@
1
- import { useRef } from 'react';
2
- export const useFormRef = () => {
3
- return useRef();
4
- };
@@ -1 +0,0 @@
1
- export declare const useHideFooter: (el?: string) => void;
@@ -1,11 +0,0 @@
1
- import { useEffect } from 'react';
2
- // 进入时隐藏指定元素,销毁时恢复该元素
3
- export const useHideFooter = (el = '.ant-layout-footer') => {
4
- useEffect(() => {
5
- const e = document.querySelector(el);
6
- e.style.display = 'none';
7
- return () => {
8
- e.style.display = 'block';
9
- };
10
- }, []);
11
- };
@@ -1,11 +0,0 @@
1
- import { RequestRes } from '@wzyjs/types';
2
- import { Options, Result, Service } from 'ahooks/lib/useRequest/src/types';
3
- interface UserOptions<P extends any[], R> extends Options<R, P> {
4
- alertErrorMessage?: false | string;
5
- alertSuccessMessage?: true | string;
6
- }
7
- interface IResult<R extends RequestRes, P extends any[]> extends Omit<Result<R, P>, 'data'> {
8
- data?: R['data'];
9
- }
10
- export declare const useRequestPro: <P extends any[], R extends RequestRes<any>>(reqPromise?: Service<R, P> | undefined, userOptions?: UserOptions<P, R> | undefined) => IResult<R, P>;
11
- export {};
@@ -1,51 +0,0 @@
1
- import { message } from 'antd';
2
- import { useRequest } from 'ahooks';
3
- import { noop, omit } from '@wzyjs/utils';
4
- const defaultResult = {
5
- loading: false,
6
- data: undefined,
7
- error: undefined,
8
- params: [],
9
- cancel: noop,
10
- refresh: noop,
11
- refreshAsync: noop,
12
- run: noop,
13
- runAsync: noop,
14
- mutate: noop,
15
- };
16
- export const useRequestPro = (reqPromise, userOptions) => {
17
- if (!reqPromise) {
18
- return defaultResult;
19
- }
20
- const { alertErrorMessage = true, alertSuccessMessage = false } = userOptions || {};
21
- const defaultOptions = {
22
- cacheKey: String(reqPromise),
23
- staleTime: 5000,
24
- debounceWait: 300,
25
- throttleWait: 300,
26
- onError: (err, params) => {
27
- if (alertErrorMessage) {
28
- message.error(typeof alertErrorMessage === 'string' ? alertErrorMessage : err.message || '请求失败');
29
- }
30
- userOptions?.onError?.(err, params);
31
- },
32
- onSuccess: (res, params) => {
33
- // 如果失败,则还是调用 onError
34
- if (!res?.success) {
35
- defaultOptions.onError(new Error(res?.message), params);
36
- return;
37
- }
38
- // 如果成功,并且需要自动弹出提示
39
- if (alertSuccessMessage) {
40
- message.success(typeof alertSuccessMessage === 'string' ? alertSuccessMessage : res.message || '请求成功');
41
- }
42
- userOptions?.onSuccess?.(res, params);
43
- },
44
- };
45
- // 用户配置与默认配置合并
46
- const options = Object.assign(defaultOptions, omit(userOptions, ['onSuccess', 'onError']));
47
- // 取出 data 里的 data
48
- const state = useRequest(reqPromise, options);
49
- state.data = state.data?.data;
50
- return state;
51
- };
@@ -1 +0,0 @@
1
- export declare const useStateCallback: (od: any) => any[];
@@ -1,12 +0,0 @@
1
- import { useState, useRef, useEffect } from 'react';
2
- export const useStateCallback = (od) => {
3
- const cbRef = useRef();
4
- const [data, setData] = useState(od);
5
- useEffect(() => {
6
- cbRef.current && cbRef.current(data);
7
- }, [data]);
8
- return [data, function (d, callback) {
9
- cbRef.current = callback;
10
- setData(d);
11
- }];
12
- };
@@ -1,5 +0,0 @@
1
- export declare const useVisibleInfo: (v: boolean | undefined, i: any) => (boolean | any[] | {
2
- setTrue: () => void;
3
- setFalse: () => void;
4
- toggle: () => void;
5
- })[];
@@ -1,18 +0,0 @@
1
- import { useState } from 'react';
2
- // visible和info的集合体
3
- export const useVisibleInfo = (v = false, i) => {
4
- const [visible, setVisible] = useState(v);
5
- const [info, setInfo] = useState(i);
6
- return [
7
- visible,
8
- {
9
- setTrue: () => setVisible(true),
10
- setFalse: () => setVisible(false),
11
- toggle: () => setVisible(!visible),
12
- },
13
- [
14
- info,
15
- setInfo,
16
- ],
17
- ];
18
- };