@chaomingd/hooks 0.0.34
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/LICENSE +21 -0
- package/README.md +23 -0
- package/dist/esm/index.d.ts +10 -0
- package/dist/esm/index.js +10 -0
- package/dist/esm/useDragAndDrop/index.d.ts +35 -0
- package/dist/esm/useDragAndDrop/index.js +193 -0
- package/dist/esm/useDragAndDrop/index.less +29 -0
- package/dist/esm/useEditModal/index.d.ts +56 -0
- package/dist/esm/useEditModal/index.js +166 -0
- package/dist/esm/useEffectWithTarget/index.d.ts +2 -0
- package/dist/esm/useEffectWithTarget/index.js +36 -0
- package/dist/esm/useLatestState/index.d.ts +8 -0
- package/dist/esm/useLatestState/index.js +36 -0
- package/dist/esm/useListLayout/index.d.ts +20 -0
- package/dist/esm/useListLayout/index.js +112 -0
- package/dist/esm/usePagination/constant.d.ts +2 -0
- package/dist/esm/usePagination/constant.js +2 -0
- package/dist/esm/usePagination/index.d.ts +14 -0
- package/dist/esm/usePagination/index.js +184 -0
- package/dist/esm/usePagination/type.d.ts +68 -0
- package/dist/esm/usePagination/type.js +1 -0
- package/dist/esm/usePagination/utils.d.ts +1 -0
- package/dist/esm/usePagination/utils.js +8 -0
- package/dist/esm/useResizer/index.d.ts +37 -0
- package/dist/esm/useResizer/index.js +173 -0
- package/dist/esm/useResponsive/index.d.ts +44 -0
- package/dist/esm/useResponsive/index.js +94 -0
- package/dist/esm/useSelectionList/index.d.ts +13 -0
- package/dist/esm/useSelectionList/index.js +49 -0
- package/dist/lib/index.d.ts +10 -0
- package/dist/lib/index.js +112 -0
- package/dist/lib/useDragAndDrop/index.d.ts +35 -0
- package/dist/lib/useDragAndDrop/index.js +191 -0
- package/dist/lib/useDragAndDrop/index.less +29 -0
- package/dist/lib/useEditModal/index.d.ts +56 -0
- package/dist/lib/useEditModal/index.js +160 -0
- package/dist/lib/useEffectWithTarget/index.d.ts +2 -0
- package/dist/lib/useEffectWithTarget/index.js +37 -0
- package/dist/lib/useLatestState/index.d.ts +8 -0
- package/dist/lib/useLatestState/index.js +29 -0
- package/dist/lib/useListLayout/index.d.ts +20 -0
- package/dist/lib/useListLayout/index.js +116 -0
- package/dist/lib/usePagination/constant.d.ts +2 -0
- package/dist/lib/usePagination/constant.js +8 -0
- package/dist/lib/usePagination/index.d.ts +14 -0
- package/dist/lib/usePagination/index.js +157 -0
- package/dist/lib/usePagination/type.d.ts +68 -0
- package/dist/lib/usePagination/type.js +5 -0
- package/dist/lib/usePagination/utils.d.ts +1 -0
- package/dist/lib/usePagination/utils.js +14 -0
- package/dist/lib/useResizer/index.d.ts +37 -0
- package/dist/lib/useResizer/index.js +168 -0
- package/dist/lib/useResponsive/index.d.ts +44 -0
- package/dist/lib/useResponsive/index.js +88 -0
- package/dist/lib/useSelectionList/index.d.ts +13 -0
- package/dist/lib/useSelectionList/index.js +45 -0
- package/package.json +39 -0
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { MutableRefObject } from 'react';
|
|
2
|
+
export interface IResponsiveConfigItem<TData = any> {
|
|
3
|
+
key: string;
|
|
4
|
+
val: number;
|
|
5
|
+
minWidth?: number;
|
|
6
|
+
maxWidth?: number;
|
|
7
|
+
data: TData;
|
|
8
|
+
[key: string]: any;
|
|
9
|
+
}
|
|
10
|
+
export interface IUseResponseParams<TData = any> {
|
|
11
|
+
mediaQuery?: IResponsiveConfigItem<TData>[];
|
|
12
|
+
onChange?: (item: IResponsiveConfigItem<TData>) => void;
|
|
13
|
+
enable?: boolean;
|
|
14
|
+
containerRef?: MutableRefObject<HTMLElement | null>;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* @examples
|
|
18
|
+
* ```ts
|
|
19
|
+
// window resize
|
|
20
|
+
const { matchedItem } = useResponsive({
|
|
21
|
+
mediaQuery: [
|
|
22
|
+
{ key: 'small', maxWidth: 760 },
|
|
23
|
+
{ key: 'middle', minWidth: 761, maxWidth: 1440 },
|
|
24
|
+
{ key: 'big', minWidth: 1441 }
|
|
25
|
+
],
|
|
26
|
+
onChange: (matchedItem) => {}
|
|
27
|
+
});
|
|
28
|
+
// dom resize
|
|
29
|
+
const containerRef = useRef<HTMLDivElement | null>(null);
|
|
30
|
+
const { matchedItem } = useResponsive({
|
|
31
|
+
containerRef,
|
|
32
|
+
mediaQuery: [
|
|
33
|
+
{ key: 'small', maxWidth: 760 },
|
|
34
|
+
{ key: 'middle', minWidth: 761, maxWidth: 1440 },
|
|
35
|
+
{ key: 'big', minWidth: 1441 }
|
|
36
|
+
],
|
|
37
|
+
onChange: (matchedItem) => {}
|
|
38
|
+
});
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export declare function useResponsive<TData = any>(params: IUseResponseParams<TData>): {
|
|
42
|
+
mediaQueryMatchedItem: IResponsiveConfigItem<TData> | null;
|
|
43
|
+
matchMedia: (this: unknown) => void;
|
|
44
|
+
};
|
|
@@ -0,0 +1,94 @@
|
|
|
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(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
|
6
|
+
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
7
|
+
import { listen, isSameObject, resizeObserver } from '@chaomingd/utils';
|
|
8
|
+
import { useLatest, useMemoizedFn } from 'ahooks';
|
|
9
|
+
import { useRef, useState } from 'react';
|
|
10
|
+
import { useEffectWithTarget } from "../useEffectWithTarget";
|
|
11
|
+
/**
|
|
12
|
+
* @examples
|
|
13
|
+
* ```ts
|
|
14
|
+
// window resize
|
|
15
|
+
const { matchedItem } = useResponsive({
|
|
16
|
+
mediaQuery: [
|
|
17
|
+
{ key: 'small', maxWidth: 760 },
|
|
18
|
+
{ key: 'middle', minWidth: 761, maxWidth: 1440 },
|
|
19
|
+
{ key: 'big', minWidth: 1441 }
|
|
20
|
+
],
|
|
21
|
+
onChange: (matchedItem) => {}
|
|
22
|
+
});
|
|
23
|
+
// dom resize
|
|
24
|
+
const containerRef = useRef<HTMLDivElement | null>(null);
|
|
25
|
+
const { matchedItem } = useResponsive({
|
|
26
|
+
containerRef,
|
|
27
|
+
mediaQuery: [
|
|
28
|
+
{ key: 'small', maxWidth: 760 },
|
|
29
|
+
{ key: 'middle', minWidth: 761, maxWidth: 1440 },
|
|
30
|
+
{ key: 'big', minWidth: 1441 }
|
|
31
|
+
],
|
|
32
|
+
onChange: (matchedItem) => {}
|
|
33
|
+
});
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
export function useResponsive(params) {
|
|
38
|
+
var _useState = useState(null),
|
|
39
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
40
|
+
mediaQueryMatchedItem = _useState2[0],
|
|
41
|
+
setMatchedItem = _useState2[1];
|
|
42
|
+
var paramsRef = useLatest(params);
|
|
43
|
+
var currDataRef = useRef(null);
|
|
44
|
+
var matchMedia = useMemoizedFn(function () {
|
|
45
|
+
var _paramsRef$current, _params$containerRef;
|
|
46
|
+
if (!paramsRef.current.enable || !((_paramsRef$current = paramsRef.current) !== null && _paramsRef$current !== void 0 && _paramsRef$current.mediaQuery)) return;
|
|
47
|
+
var width;
|
|
48
|
+
if ((_params$containerRef = params.containerRef) !== null && _params$containerRef !== void 0 && _params$containerRef.current) {
|
|
49
|
+
width = params.containerRef.current.getBoundingClientRect().width;
|
|
50
|
+
} else {
|
|
51
|
+
width = window.innerWidth;
|
|
52
|
+
}
|
|
53
|
+
var mediaQuery = paramsRef.current.mediaQuery;
|
|
54
|
+
var matchItem = null;
|
|
55
|
+
if (mediaQuery) {
|
|
56
|
+
for (var i = 0; i < mediaQuery.length; i++) {
|
|
57
|
+
var _item = mediaQuery[i];
|
|
58
|
+
if (width >= (_item.minWidth || 0) && width <= (_item.maxWidth || Infinity)) {
|
|
59
|
+
matchItem = _item;
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
if (!matchItem) {
|
|
64
|
+
matchItem = mediaQuery[0];
|
|
65
|
+
}
|
|
66
|
+
if (matchItem && !isSameObject(currDataRef.current, matchItem)) {
|
|
67
|
+
var _onChange, _ref;
|
|
68
|
+
currDataRef.current = matchItem;
|
|
69
|
+
setMatchedItem(matchItem);
|
|
70
|
+
(_onChange = (_ref = paramsRef.current).onChange) === null || _onChange === void 0 || _onChange.call(_ref, matchItem);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
useEffectWithTarget(function () {
|
|
75
|
+
var _params$containerRef2;
|
|
76
|
+
matchMedia();
|
|
77
|
+
var offResize;
|
|
78
|
+
if ((_params$containerRef2 = params.containerRef) !== null && _params$containerRef2 !== void 0 && _params$containerRef2.current) {
|
|
79
|
+
offResize = resizeObserver({
|
|
80
|
+
el: params.containerRef.current,
|
|
81
|
+
onResize: matchMedia
|
|
82
|
+
});
|
|
83
|
+
} else {
|
|
84
|
+
offResize = listen(window, 'resize', matchMedia);
|
|
85
|
+
}
|
|
86
|
+
return function () {
|
|
87
|
+
offResize();
|
|
88
|
+
};
|
|
89
|
+
}, params.containerRef, [], true);
|
|
90
|
+
return {
|
|
91
|
+
mediaQueryMatchedItem: mediaQueryMatchedItem,
|
|
92
|
+
matchMedia: matchMedia
|
|
93
|
+
};
|
|
94
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
interface IConfig<T = any> {
|
|
3
|
+
dataList?: T[];
|
|
4
|
+
listKey?: string;
|
|
5
|
+
getKey?: (item: T) => string;
|
|
6
|
+
}
|
|
7
|
+
export declare function useSelectionList<TData = any>({ dataList, listKey, getKey, }: IConfig<TData>): {
|
|
8
|
+
selectedKeys: string[];
|
|
9
|
+
selectedListKeys: string[];
|
|
10
|
+
selectedListItems: TData[];
|
|
11
|
+
changeSelectedListKeys: import("react").Dispatch<import("react").SetStateAction<string[]>>;
|
|
12
|
+
};
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,49 @@
|
|
|
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(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
|
6
|
+
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
7
|
+
import { arrayToMap } from '@chaomingd/utils';
|
|
8
|
+
import { useLatest } from 'ahooks';
|
|
9
|
+
import { useMemo, useState } from 'react';
|
|
10
|
+
var DEFAULT_LIST_KEY = 'key';
|
|
11
|
+
export function useSelectionList(_ref) {
|
|
12
|
+
var dataList = _ref.dataList,
|
|
13
|
+
listKey = _ref.listKey,
|
|
14
|
+
getKey = _ref.getKey;
|
|
15
|
+
var _useState = useState([]),
|
|
16
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
17
|
+
selectedKeys = _useState2[0],
|
|
18
|
+
changeSelectedListKeys = _useState2[1];
|
|
19
|
+
var getKeyRef = useLatest(getKey);
|
|
20
|
+
var selectedListData = useMemo(function () {
|
|
21
|
+
if (!dataList || !dataList.length) {
|
|
22
|
+
return {
|
|
23
|
+
selectedKeys: selectedKeys,
|
|
24
|
+
selectedListKeys: [],
|
|
25
|
+
selectedListItems: [],
|
|
26
|
+
changeSelectedListKeys: changeSelectedListKeys
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
var getKeyFunc = getKeyRef.current ? getKeyRef.current : function (item) {
|
|
30
|
+
return item && item[listKey || DEFAULT_LIST_KEY];
|
|
31
|
+
};
|
|
32
|
+
var listDatMap = arrayToMap(dataList, getKeyFunc);
|
|
33
|
+
var selectedListKeys = [];
|
|
34
|
+
var selectedListItems = [];
|
|
35
|
+
selectedKeys.forEach(function (key) {
|
|
36
|
+
if (listDatMap[key]) {
|
|
37
|
+
selectedListKeys.push(key);
|
|
38
|
+
selectedListItems.push(listDatMap[key]);
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
return {
|
|
42
|
+
selectedKeys: selectedKeys,
|
|
43
|
+
selectedListKeys: selectedListKeys,
|
|
44
|
+
selectedListItems: selectedListItems,
|
|
45
|
+
changeSelectedListKeys: changeSelectedListKeys
|
|
46
|
+
};
|
|
47
|
+
}, [dataList, listKey, getKeyRef, selectedKeys]);
|
|
48
|
+
return selectedListData;
|
|
49
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import useLatestState from './useLatestState';
|
|
2
|
+
export * from './useDragAndDrop';
|
|
3
|
+
export * from './usePagination';
|
|
4
|
+
export * from './useResizer';
|
|
5
|
+
export * from './useResponsive';
|
|
6
|
+
export * from './useEffectWithTarget';
|
|
7
|
+
export * from './useEditModal';
|
|
8
|
+
export * from './useListLayout';
|
|
9
|
+
export * from './useSelectionList';
|
|
10
|
+
export { useLatestState };
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
var _exportNames = {
|
|
7
|
+
useLatestState: true
|
|
8
|
+
};
|
|
9
|
+
Object.defineProperty(exports, "useLatestState", {
|
|
10
|
+
enumerable: true,
|
|
11
|
+
get: function () {
|
|
12
|
+
return _useLatestState.default;
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
var _useLatestState = _interopRequireDefault(require("./useLatestState"));
|
|
16
|
+
var _useDragAndDrop = require("./useDragAndDrop");
|
|
17
|
+
Object.keys(_useDragAndDrop).forEach(function (key) {
|
|
18
|
+
if (key === "default" || key === "__esModule") return;
|
|
19
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
20
|
+
if (key in exports && exports[key] === _useDragAndDrop[key]) return;
|
|
21
|
+
Object.defineProperty(exports, key, {
|
|
22
|
+
enumerable: true,
|
|
23
|
+
get: function () {
|
|
24
|
+
return _useDragAndDrop[key];
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
var _usePagination = require("./usePagination");
|
|
29
|
+
Object.keys(_usePagination).forEach(function (key) {
|
|
30
|
+
if (key === "default" || key === "__esModule") return;
|
|
31
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
32
|
+
if (key in exports && exports[key] === _usePagination[key]) return;
|
|
33
|
+
Object.defineProperty(exports, key, {
|
|
34
|
+
enumerable: true,
|
|
35
|
+
get: function () {
|
|
36
|
+
return _usePagination[key];
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
var _useResizer = require("./useResizer");
|
|
41
|
+
Object.keys(_useResizer).forEach(function (key) {
|
|
42
|
+
if (key === "default" || key === "__esModule") return;
|
|
43
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
44
|
+
if (key in exports && exports[key] === _useResizer[key]) return;
|
|
45
|
+
Object.defineProperty(exports, key, {
|
|
46
|
+
enumerable: true,
|
|
47
|
+
get: function () {
|
|
48
|
+
return _useResizer[key];
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
var _useResponsive = require("./useResponsive");
|
|
53
|
+
Object.keys(_useResponsive).forEach(function (key) {
|
|
54
|
+
if (key === "default" || key === "__esModule") return;
|
|
55
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
56
|
+
if (key in exports && exports[key] === _useResponsive[key]) return;
|
|
57
|
+
Object.defineProperty(exports, key, {
|
|
58
|
+
enumerable: true,
|
|
59
|
+
get: function () {
|
|
60
|
+
return _useResponsive[key];
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
var _useEffectWithTarget = require("./useEffectWithTarget");
|
|
65
|
+
Object.keys(_useEffectWithTarget).forEach(function (key) {
|
|
66
|
+
if (key === "default" || key === "__esModule") return;
|
|
67
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
68
|
+
if (key in exports && exports[key] === _useEffectWithTarget[key]) return;
|
|
69
|
+
Object.defineProperty(exports, key, {
|
|
70
|
+
enumerable: true,
|
|
71
|
+
get: function () {
|
|
72
|
+
return _useEffectWithTarget[key];
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
var _useEditModal = require("./useEditModal");
|
|
77
|
+
Object.keys(_useEditModal).forEach(function (key) {
|
|
78
|
+
if (key === "default" || key === "__esModule") return;
|
|
79
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
80
|
+
if (key in exports && exports[key] === _useEditModal[key]) return;
|
|
81
|
+
Object.defineProperty(exports, key, {
|
|
82
|
+
enumerable: true,
|
|
83
|
+
get: function () {
|
|
84
|
+
return _useEditModal[key];
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
var _useListLayout = require("./useListLayout");
|
|
89
|
+
Object.keys(_useListLayout).forEach(function (key) {
|
|
90
|
+
if (key === "default" || key === "__esModule") return;
|
|
91
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
92
|
+
if (key in exports && exports[key] === _useListLayout[key]) return;
|
|
93
|
+
Object.defineProperty(exports, key, {
|
|
94
|
+
enumerable: true,
|
|
95
|
+
get: function () {
|
|
96
|
+
return _useListLayout[key];
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
var _useSelectionList = require("./useSelectionList");
|
|
101
|
+
Object.keys(_useSelectionList).forEach(function (key) {
|
|
102
|
+
if (key === "default" || key === "__esModule") return;
|
|
103
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
104
|
+
if (key in exports && exports[key] === _useSelectionList[key]) return;
|
|
105
|
+
Object.defineProperty(exports, key, {
|
|
106
|
+
enumerable: true,
|
|
107
|
+
get: function () {
|
|
108
|
+
return _useSelectionList[key];
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { MutableRefObject } from 'react';
|
|
2
|
+
import './index.less';
|
|
3
|
+
type TEffect = 'disabled' | 'drop' | 'auto' | '';
|
|
4
|
+
declare function updateDragEffect(effect: TEffect): void;
|
|
5
|
+
export declare function setDragData(data: Record<string, any>): void;
|
|
6
|
+
export declare function getDragData(): {
|
|
7
|
+
[x: string]: any;
|
|
8
|
+
};
|
|
9
|
+
type TDomRef = MutableRefObject<HTMLElement | null>;
|
|
10
|
+
interface ICallbackOptions {
|
|
11
|
+
setDragData: typeof setDragData;
|
|
12
|
+
updateDragEffect: typeof updateDragEffect;
|
|
13
|
+
node: HTMLElement;
|
|
14
|
+
}
|
|
15
|
+
export interface IDragOptions {
|
|
16
|
+
onDragStart?: (e: MouseEvent, options: ICallbackOptions) => void;
|
|
17
|
+
onDragEnd?: (e: MouseEvent, options: ICallbackOptions) => void;
|
|
18
|
+
key: string;
|
|
19
|
+
containerClassName?: string;
|
|
20
|
+
}
|
|
21
|
+
export declare function handlerDragMouseDown(e: any, options: IDragOptions, dom: HTMLElement | null): void;
|
|
22
|
+
interface IDropCallbackOptions {
|
|
23
|
+
updateDragEffect: typeof updateDragEffect;
|
|
24
|
+
getDragData: typeof getDragData;
|
|
25
|
+
}
|
|
26
|
+
export interface IDropOptions {
|
|
27
|
+
key: string;
|
|
28
|
+
onDragEnter?: (e: MouseEvent, options: IDropCallbackOptions) => void;
|
|
29
|
+
onDragLeave?: (e: MouseEvent, options: IDropCallbackOptions) => void;
|
|
30
|
+
onDragMove?: (e: MouseEvent, options: IDropCallbackOptions) => void;
|
|
31
|
+
onDrop?: (e: MouseEvent, options: IDropCallbackOptions) => void;
|
|
32
|
+
onValideDrop?: (e: MouseEvent, options: IDropCallbackOptions) => boolean;
|
|
33
|
+
}
|
|
34
|
+
export declare function useDrop(domRef: TDomRef, options: IDropOptions): void;
|
|
35
|
+
export {};
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.getDragData = getDragData;
|
|
7
|
+
exports.handlerDragMouseDown = handlerDragMouseDown;
|
|
8
|
+
exports.setDragData = setDragData;
|
|
9
|
+
exports.useDrop = useDrop;
|
|
10
|
+
var _utils = require("@chaomingd/utils");
|
|
11
|
+
var _react = require("react");
|
|
12
|
+
require("./index.less");
|
|
13
|
+
const Signal = new _utils.EventEmitter();
|
|
14
|
+
const store = getInitialStore();
|
|
15
|
+
function getInitialStore() {
|
|
16
|
+
return {
|
|
17
|
+
data: {},
|
|
18
|
+
container: null,
|
|
19
|
+
draging: false,
|
|
20
|
+
enterDropArea: false,
|
|
21
|
+
offset: {
|
|
22
|
+
x: 0,
|
|
23
|
+
y: 0
|
|
24
|
+
},
|
|
25
|
+
effect: ''
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
function resetStore() {
|
|
29
|
+
Object.assign(store, getInitialStore());
|
|
30
|
+
}
|
|
31
|
+
function updateDragEffect(effect) {
|
|
32
|
+
if (effect === store.effect) return;
|
|
33
|
+
store.effect = effect;
|
|
34
|
+
const container = store.container;
|
|
35
|
+
if (container) {
|
|
36
|
+
container.setAttribute('effect', effect);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function setDragData(data) {
|
|
40
|
+
Object.assign(store.data, data);
|
|
41
|
+
}
|
|
42
|
+
function getDragData() {
|
|
43
|
+
return {
|
|
44
|
+
...store.data
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
function initContainer(options) {
|
|
48
|
+
if (!store.container) {
|
|
49
|
+
store.container = document.createElement('div');
|
|
50
|
+
}
|
|
51
|
+
if (store.container.parentNode === document.body) return;
|
|
52
|
+
let className = 'laf-drag-item';
|
|
53
|
+
if (options.containerClassName) {
|
|
54
|
+
className += ` ${options.containerClassName}`;
|
|
55
|
+
}
|
|
56
|
+
store.container.className = className;
|
|
57
|
+
document.body.appendChild(store.container);
|
|
58
|
+
}
|
|
59
|
+
function addTargetCloneNode(dom) {
|
|
60
|
+
const node = dom.cloneNode(true);
|
|
61
|
+
node.style.position = 'static';
|
|
62
|
+
const rect = dom.getBoundingClientRect();
|
|
63
|
+
store.container.style.width = rect.width + 'px';
|
|
64
|
+
store.container.style.height = rect.height + 'px';
|
|
65
|
+
store.container.appendChild(node);
|
|
66
|
+
return node;
|
|
67
|
+
}
|
|
68
|
+
function updateContainerPosition(e, dom) {
|
|
69
|
+
const container = store.container;
|
|
70
|
+
const offset = store.offset;
|
|
71
|
+
// container.style.left = e.clientX - offset.x + 'px';
|
|
72
|
+
// container.style.top = e.clientY - offset.y + 'px';
|
|
73
|
+
container.style.transform = `translate3d(${e.clientX - offset.x}px, ${e.clientY - offset.y}px, 0)`;
|
|
74
|
+
if (isInDom(dom, e)) {
|
|
75
|
+
updateDragEffect('auto');
|
|
76
|
+
} else {
|
|
77
|
+
if (!store.enterDropArea) {
|
|
78
|
+
updateDragEffect('disabled');
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
function removeContainer() {
|
|
83
|
+
if (store.container) {
|
|
84
|
+
store.container.firstElementChild && store.container.removeChild(store.container.firstElementChild);
|
|
85
|
+
store.container.parentNode?.removeChild(store.container);
|
|
86
|
+
store.container = null;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
function setOffset(e, dom) {
|
|
90
|
+
if (!dom) return;
|
|
91
|
+
const rect = dom.getBoundingClientRect();
|
|
92
|
+
store.offset = {
|
|
93
|
+
x: e.clientX - rect.left,
|
|
94
|
+
y: e.clientY - rect.top
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
function handlerDragMouseDown(e, options, dom) {
|
|
98
|
+
if (!dom) return;
|
|
99
|
+
let move = false;
|
|
100
|
+
let cloneNode;
|
|
101
|
+
initContainer(options);
|
|
102
|
+
const SignalKey = options.key;
|
|
103
|
+
setOffset(e, dom);
|
|
104
|
+
// eslint-disable-next-line @typescript-eslint/no-shadow
|
|
105
|
+
const offMouseMove = (0, _utils.listen)(document, 'mousemove', e => {
|
|
106
|
+
e.preventDefault();
|
|
107
|
+
if (!move) {
|
|
108
|
+
cloneNode = addTargetCloneNode(dom);
|
|
109
|
+
options.onDragStart?.(e, {
|
|
110
|
+
setDragData,
|
|
111
|
+
updateDragEffect,
|
|
112
|
+
node: cloneNode
|
|
113
|
+
});
|
|
114
|
+
move = true;
|
|
115
|
+
store.draging = true;
|
|
116
|
+
} else {
|
|
117
|
+
Signal.emit(`${SignalKey}-mousemove`, e);
|
|
118
|
+
}
|
|
119
|
+
updateContainerPosition(e, dom);
|
|
120
|
+
});
|
|
121
|
+
// eslint-disable-next-line @typescript-eslint/no-shadow
|
|
122
|
+
const offMouseUp = (0, _utils.listen)(document, 'mouseup', e => {
|
|
123
|
+
e.preventDefault();
|
|
124
|
+
offMouseMove();
|
|
125
|
+
offMouseUp();
|
|
126
|
+
if (move) {
|
|
127
|
+
options.onDragEnd?.(e, {
|
|
128
|
+
node: cloneNode,
|
|
129
|
+
setDragData,
|
|
130
|
+
updateDragEffect
|
|
131
|
+
});
|
|
132
|
+
Signal.emit(`${SignalKey}-mouseup`, e);
|
|
133
|
+
}
|
|
134
|
+
updateDragEffect('auto');
|
|
135
|
+
removeContainer();
|
|
136
|
+
resetStore();
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
function useDrop(domRef, options) {
|
|
140
|
+
const optionsRef = (0, _react.useRef)(options);
|
|
141
|
+
(0, _react.useEffect)(() => {
|
|
142
|
+
const SignalKey = optionsRef.current.key;
|
|
143
|
+
let isDragEnter = false;
|
|
144
|
+
const callbackOptions = {
|
|
145
|
+
updateDragEffect,
|
|
146
|
+
getDragData
|
|
147
|
+
};
|
|
148
|
+
const onMouseMove = e => {
|
|
149
|
+
if (isInDom(domRef.current, e)) {
|
|
150
|
+
if (!isDragEnter) {
|
|
151
|
+
isDragEnter = true;
|
|
152
|
+
optionsRef.current.onDragEnter?.(e, callbackOptions);
|
|
153
|
+
store.enterDropArea = true;
|
|
154
|
+
updateDragEffect('drop');
|
|
155
|
+
}
|
|
156
|
+
optionsRef.current.onDragMove?.(e, callbackOptions);
|
|
157
|
+
} else {
|
|
158
|
+
if (isDragEnter) {
|
|
159
|
+
store.enterDropArea = false;
|
|
160
|
+
isDragEnter = false;
|
|
161
|
+
updateDragEffect('disabled');
|
|
162
|
+
optionsRef.current.onDragLeave?.(e, callbackOptions);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
const onMouseUp = e => {
|
|
167
|
+
if (isInDom(domRef.current, e)) {
|
|
168
|
+
let validate = true;
|
|
169
|
+
if (optionsRef.current.onValideDrop) {
|
|
170
|
+
validate = optionsRef.current.onValideDrop(e, callbackOptions);
|
|
171
|
+
}
|
|
172
|
+
if (validate) {
|
|
173
|
+
optionsRef.current.onDrop?.(e, callbackOptions);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
store.enterDropArea = false;
|
|
177
|
+
};
|
|
178
|
+
Signal.on(`${SignalKey}-mousemove`, onMouseMove);
|
|
179
|
+
Signal.on(`${SignalKey}-mouseup`, onMouseUp);
|
|
180
|
+
}, [domRef]);
|
|
181
|
+
}
|
|
182
|
+
function isInDom(dom, e) {
|
|
183
|
+
if (!dom) return false;
|
|
184
|
+
const rect = dom.getBoundingClientRect();
|
|
185
|
+
const clientX = e.clientX;
|
|
186
|
+
const clientY = e.clientY;
|
|
187
|
+
if (clientX < rect.left || clientX > rect.left + rect.width || clientY < rect.top || clientY > rect.top + rect.height) {
|
|
188
|
+
return false;
|
|
189
|
+
}
|
|
190
|
+
return true;
|
|
191
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
.laf-drag-item {
|
|
2
|
+
position: fixed;
|
|
3
|
+
z-index: 10000;
|
|
4
|
+
opacity: 0.7;
|
|
5
|
+
top: 0;
|
|
6
|
+
left: 0;
|
|
7
|
+
|
|
8
|
+
&::after {
|
|
9
|
+
content: '';
|
|
10
|
+
position: absolute;
|
|
11
|
+
top: 50%;
|
|
12
|
+
left: 50%;
|
|
13
|
+
width: 200%;
|
|
14
|
+
height: 200%;
|
|
15
|
+
transform: translate(-50%, -50%);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
&[effect='disabled'] {
|
|
19
|
+
&::after {
|
|
20
|
+
cursor: not-allowed;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
&[effect='drop'] {
|
|
25
|
+
&::after {
|
|
26
|
+
cursor: copy;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import { FormInstance } from 'antd';
|
|
3
|
+
interface IState {
|
|
4
|
+
visible: boolean;
|
|
5
|
+
title: string;
|
|
6
|
+
loading: boolean;
|
|
7
|
+
confirmLoading: boolean;
|
|
8
|
+
isEdit: boolean;
|
|
9
|
+
[key: string]: any;
|
|
10
|
+
}
|
|
11
|
+
interface IHandlerReturnValue<IFormValues extends Record<string, any> = Record<string, any>, IUserState extends Record<string, any> = Record<string, any>> {
|
|
12
|
+
formValue?: Partial<IFormValues>;
|
|
13
|
+
state?: Partial<IState & IUserState>;
|
|
14
|
+
}
|
|
15
|
+
interface IHandlerData<IFormValues extends Record<string, any> = Record<string, any>, IUserState extends Record<string, any> = Record<string, any>> {
|
|
16
|
+
formValue?: Partial<IFormValues>;
|
|
17
|
+
state?: Partial<IState & IUserState>;
|
|
18
|
+
onSave?: (res: any, state: IState & IUserState) => void;
|
|
19
|
+
onError?: (e: Error) => void;
|
|
20
|
+
[key: string]: any;
|
|
21
|
+
}
|
|
22
|
+
type TPromiseValue<T> = T | Promise<T>;
|
|
23
|
+
interface IConfig<IFormValues extends Record<string, any> = Record<string, any>, IUserState extends Record<string, any> = Record<string, any>> {
|
|
24
|
+
title?: string;
|
|
25
|
+
hasLoading?: boolean;
|
|
26
|
+
hasConfirmLoading?: boolean;
|
|
27
|
+
saveService?: (formValues: IFormValues, state: IState & IUserState) => Promise<any>;
|
|
28
|
+
onSaveClose?: boolean;
|
|
29
|
+
onCloseResetState?: boolean;
|
|
30
|
+
onClose?: () => void;
|
|
31
|
+
onSave?: (res: any, state: IState & IUserState) => void;
|
|
32
|
+
onAdd?: (data: IHandlerReturnValue<IFormValues, IUserState>) => TPromiseValue<void | undefined | IHandlerReturnValue<IFormValues, IUserState>>;
|
|
33
|
+
onEdit?: (data: IHandlerReturnValue<IFormValues, IUserState>) => TPromiseValue<void | undefined | IHandlerReturnValue<IFormValues, IUserState>>;
|
|
34
|
+
defaultState?: IUserState;
|
|
35
|
+
form?: FormInstance;
|
|
36
|
+
}
|
|
37
|
+
export declare function useEditModal<IFormValues extends Record<string, any> = Record<string, any>, IUserState extends Record<string, any> = Record<string, any>>({ onAdd, onEdit, title, saveService, hasLoading, hasConfirmLoading, onSaveClose, onCloseResetState, onClose, onSave, defaultState, form, }?: IConfig<IFormValues, IUserState>): {
|
|
38
|
+
modalProps: {
|
|
39
|
+
open: boolean;
|
|
40
|
+
title: string;
|
|
41
|
+
onCancel: () => void;
|
|
42
|
+
onOk: () => void;
|
|
43
|
+
confirmLoading: boolean;
|
|
44
|
+
afterOpenChange: (open: boolean) => void;
|
|
45
|
+
};
|
|
46
|
+
state: IState & IUserState;
|
|
47
|
+
originDataRef: import("react").MutableRefObject<IHandlerData<IFormValues, IUserState>>;
|
|
48
|
+
formRef: import("react").MutableRefObject<FormInstance<any> | null>;
|
|
49
|
+
add: (data: IHandlerData<IFormValues, IUserState>) => void;
|
|
50
|
+
edit: (data: IHandlerData<IFormValues, IUserState>) => void;
|
|
51
|
+
close: () => void;
|
|
52
|
+
onSubmit: () => void;
|
|
53
|
+
submit: () => void;
|
|
54
|
+
model: import("@chaomingd/store").Model<IState, object, Record<string, any>>;
|
|
55
|
+
};
|
|
56
|
+
export {};
|