@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,160 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.useEditModal = useEditModal;
|
|
7
|
+
var _react = require("react");
|
|
8
|
+
var _store = require("@chaomingd/store");
|
|
9
|
+
var _utils = require("@chaomingd/utils");
|
|
10
|
+
function defaultHandler() {
|
|
11
|
+
return Promise.resolve({});
|
|
12
|
+
}
|
|
13
|
+
function useEditModal({
|
|
14
|
+
onAdd = defaultHandler,
|
|
15
|
+
onEdit = defaultHandler,
|
|
16
|
+
title = 'Modal Title',
|
|
17
|
+
saveService = defaultHandler,
|
|
18
|
+
hasLoading = true,
|
|
19
|
+
hasConfirmLoading = true,
|
|
20
|
+
onSaveClose = true,
|
|
21
|
+
onCloseResetState = true,
|
|
22
|
+
onClose,
|
|
23
|
+
onSave,
|
|
24
|
+
defaultState,
|
|
25
|
+
form
|
|
26
|
+
} = {}) {
|
|
27
|
+
const formRef = (0, _react.useRef)(form || null);
|
|
28
|
+
const originDataRef = (0, _react.useRef)({});
|
|
29
|
+
const defaultEditState = {
|
|
30
|
+
visible: false,
|
|
31
|
+
title,
|
|
32
|
+
loading: false,
|
|
33
|
+
isEdit: false,
|
|
34
|
+
confirmLoading: false,
|
|
35
|
+
...defaultState
|
|
36
|
+
};
|
|
37
|
+
const model = (0, _store.useModel)({
|
|
38
|
+
state: defaultEditState
|
|
39
|
+
});
|
|
40
|
+
const state = model.useGetState();
|
|
41
|
+
const createHandler = isEdit => {
|
|
42
|
+
return data => {
|
|
43
|
+
if (model.getState().visible) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
originDataRef.current = data;
|
|
47
|
+
const handler = isEdit ? onEdit : onAdd;
|
|
48
|
+
const formValues = {
|
|
49
|
+
...(data.formValue || {})
|
|
50
|
+
};
|
|
51
|
+
const openState = {
|
|
52
|
+
...(data.state || {}),
|
|
53
|
+
visible: true,
|
|
54
|
+
isEdit
|
|
55
|
+
};
|
|
56
|
+
if (hasLoading) {
|
|
57
|
+
openState.loading = true;
|
|
58
|
+
}
|
|
59
|
+
model.setState(openState);
|
|
60
|
+
const newState = {
|
|
61
|
+
loading: false
|
|
62
|
+
};
|
|
63
|
+
Promise.resolve(handler(data)).then(returnValue => {
|
|
64
|
+
if (returnValue && returnValue.formValue) {
|
|
65
|
+
Object.assign(formValues, returnValue?.formValue || {});
|
|
66
|
+
}
|
|
67
|
+
if (returnValue && returnValue.state) {
|
|
68
|
+
Object.assign(newState, returnValue.state);
|
|
69
|
+
}
|
|
70
|
+
}).finally(() => {
|
|
71
|
+
model.setState(newState);
|
|
72
|
+
formRef.current?.setFieldsValue(formValues);
|
|
73
|
+
});
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
const add = createHandler(false);
|
|
77
|
+
const edit = createHandler(true);
|
|
78
|
+
const close = () => {
|
|
79
|
+
if (onCloseResetState) {
|
|
80
|
+
model.setState((0, _utils.omit)(defaultEditState, ['title']));
|
|
81
|
+
} else {
|
|
82
|
+
model.setState({
|
|
83
|
+
visible: false
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
onClose && onClose();
|
|
87
|
+
};
|
|
88
|
+
const afterOpenChange = open => {
|
|
89
|
+
if (!open) {
|
|
90
|
+
if (formRef.current) {
|
|
91
|
+
formRef.current.resetFields();
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
const onSubmit = () => {
|
|
96
|
+
// eslint-disable-next-line @typescript-eslint/no-shadow
|
|
97
|
+
const handleService = (values, state) => {
|
|
98
|
+
return saveService?.(values, state).then(res => {
|
|
99
|
+
const currentState = model.getState();
|
|
100
|
+
onSaveClose && close();
|
|
101
|
+
onSave && onSave(res, currentState);
|
|
102
|
+
originDataRef.current.onSave && originDataRef.current.onSave(res, currentState);
|
|
103
|
+
if (hasConfirmLoading) {
|
|
104
|
+
model.setState({
|
|
105
|
+
confirmLoading: false
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
return res;
|
|
109
|
+
});
|
|
110
|
+
};
|
|
111
|
+
if (hasConfirmLoading) {
|
|
112
|
+
model.setState({
|
|
113
|
+
confirmLoading: true
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
if (formRef.current) {
|
|
117
|
+
formRef.current?.validateFields().then(values => {
|
|
118
|
+
return handleService(values, model.getState());
|
|
119
|
+
}).catch(e => {
|
|
120
|
+
console.error(e);
|
|
121
|
+
originDataRef.current.onError && originDataRef.current.onError(e);
|
|
122
|
+
if (hasConfirmLoading) {
|
|
123
|
+
model.setState({
|
|
124
|
+
confirmLoading: false
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
} else {
|
|
129
|
+
handleService({}, model.getState()).catch(e => {
|
|
130
|
+
console.error(e);
|
|
131
|
+
originDataRef.current.onError && originDataRef.current.onError(e);
|
|
132
|
+
if (hasConfirmLoading) {
|
|
133
|
+
model.setState({
|
|
134
|
+
confirmLoading: false
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
const modalProps = {
|
|
141
|
+
open: state.visible,
|
|
142
|
+
title: state.title,
|
|
143
|
+
onCancel: close,
|
|
144
|
+
onOk: onSubmit,
|
|
145
|
+
confirmLoading: state.confirmLoading,
|
|
146
|
+
afterOpenChange
|
|
147
|
+
};
|
|
148
|
+
return {
|
|
149
|
+
modalProps,
|
|
150
|
+
state: state,
|
|
151
|
+
originDataRef,
|
|
152
|
+
formRef,
|
|
153
|
+
add,
|
|
154
|
+
edit,
|
|
155
|
+
close,
|
|
156
|
+
onSubmit,
|
|
157
|
+
submit: onSubmit,
|
|
158
|
+
model
|
|
159
|
+
};
|
|
160
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.useEffectWithTarget = useEffectWithTarget;
|
|
7
|
+
var _ahooks = require("ahooks");
|
|
8
|
+
var _react = require("react");
|
|
9
|
+
var _utils = require("@chaomingd/utils");
|
|
10
|
+
function useEffectWithTarget(effect, targetRef, deps, strict = false) {
|
|
11
|
+
const lastDepsRef = (0, _react.useRef)();
|
|
12
|
+
const lastElementRef = (0, _react.useRef)();
|
|
13
|
+
const lastEffectReturnRef = (0, _react.useRef)();
|
|
14
|
+
const clearEffect = () => {
|
|
15
|
+
if (lastEffectReturnRef.current && typeof lastEffectReturnRef.current === 'function') {
|
|
16
|
+
lastEffectReturnRef.current();
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
(0, _react.useEffect)(() => {
|
|
20
|
+
const currentTarget = (Array.isArray(targetRef) ? targetRef.map(ref => ref.current) : targetRef ? [targetRef.current] : []).filter(el => (0, _utils.isDefined)(el));
|
|
21
|
+
if (!(0, _utils.isSameArr)(lastElementRef.current, currentTarget) || lastDepsRef.current && deps && !(0, _utils.isSameArr)(lastDepsRef.current, deps)) {
|
|
22
|
+
lastElementRef.current = currentTarget;
|
|
23
|
+
lastDepsRef.current = deps;
|
|
24
|
+
clearEffect();
|
|
25
|
+
if (strict) {
|
|
26
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
|
27
|
+
currentTarget.length && (lastEffectReturnRef.current = effect());
|
|
28
|
+
} else {
|
|
29
|
+
lastEffectReturnRef.current = effect();
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
(0, _ahooks.useUnmount)(() => {
|
|
34
|
+
lastElementRef.current = undefined;
|
|
35
|
+
clearEffect();
|
|
36
|
+
});
|
|
37
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Dispatch, SetStateAction } from 'react';
|
|
2
|
+
declare function useLatestState<T>(initialState: T | (() => T)): [T, Dispatch<SetStateAction<T>>, () => T];
|
|
3
|
+
declare function useLatestState<T = undefined>(): [
|
|
4
|
+
T | undefined,
|
|
5
|
+
Dispatch<SetStateAction<T | undefined>>,
|
|
6
|
+
() => T | undefined
|
|
7
|
+
];
|
|
8
|
+
export default useLatestState;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _react = require("react");
|
|
8
|
+
const isFunction = val => typeof val === 'function';
|
|
9
|
+
|
|
10
|
+
// 获取新的状态值,兼容传值和传函数情况
|
|
11
|
+
function getStateVal(preState, initVal) {
|
|
12
|
+
if (isFunction(initVal)) {
|
|
13
|
+
return initVal(preState);
|
|
14
|
+
}
|
|
15
|
+
return initVal;
|
|
16
|
+
}
|
|
17
|
+
function useLatestState(initVal) {
|
|
18
|
+
const initState = getStateVal(undefined, initVal);
|
|
19
|
+
const [val, setVal] = (0, _react.useState)(initState);
|
|
20
|
+
const valRef = (0, _react.useRef)(initState);
|
|
21
|
+
const setState = (0, _react.useCallback)(newVal => {
|
|
22
|
+
const newState = getStateVal(valRef.current, newVal);
|
|
23
|
+
valRef.current = newState;
|
|
24
|
+
setVal(newState);
|
|
25
|
+
}, []);
|
|
26
|
+
const getRealState = (0, _react.useCallback)(() => valRef.current, []);
|
|
27
|
+
return [val, setState, getRealState];
|
|
28
|
+
}
|
|
29
|
+
var _default = exports.default = useLatestState;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { MutableRefObject } from 'react';
|
|
2
|
+
import { px } from '@chaomingd/utils';
|
|
3
|
+
export declare function setDefaultTransform(transform: typeof px): void;
|
|
4
|
+
export interface UseListLayoutConfig {
|
|
5
|
+
containerRef?: MutableRefObject<HTMLElement | null>;
|
|
6
|
+
baseWidth: number;
|
|
7
|
+
baseHeight?: number;
|
|
8
|
+
gap?: number | [number, number];
|
|
9
|
+
onChange?: (state: IState) => any;
|
|
10
|
+
removeScrollBarWidth?: boolean;
|
|
11
|
+
pxTransform?: (base: number, width: number) => number;
|
|
12
|
+
}
|
|
13
|
+
interface IState {
|
|
14
|
+
col: number;
|
|
15
|
+
row: number;
|
|
16
|
+
width: number;
|
|
17
|
+
height: number;
|
|
18
|
+
}
|
|
19
|
+
export declare function useListLayout(config: UseListLayoutConfig): IState;
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.setDefaultTransform = setDefaultTransform;
|
|
7
|
+
exports.useListLayout = useListLayout;
|
|
8
|
+
var _ahooks = require("ahooks");
|
|
9
|
+
var _useEffectWithTarget = require("../useEffectWithTarget");
|
|
10
|
+
var _react = require("react");
|
|
11
|
+
var _utils = require("@chaomingd/utils");
|
|
12
|
+
var _store = require("@chaomingd/store");
|
|
13
|
+
let defaultTransform = _utils.px;
|
|
14
|
+
function setDefaultTransform(transform) {
|
|
15
|
+
defaultTransform = transform;
|
|
16
|
+
}
|
|
17
|
+
function getCount(base, gap, length) {
|
|
18
|
+
let count = Math.floor(length / base);
|
|
19
|
+
while (count) {
|
|
20
|
+
if (count * base + gap * (count - 1) <= length) break;
|
|
21
|
+
count--;
|
|
22
|
+
}
|
|
23
|
+
return count;
|
|
24
|
+
}
|
|
25
|
+
const scrollBarSize = (0, _utils.getScrollBarSize)();
|
|
26
|
+
function useListLayout(config) {
|
|
27
|
+
const configRef = (0, _ahooks.useLatest)(config);
|
|
28
|
+
const timerRef = (0, _react.useRef)();
|
|
29
|
+
const model = (0, _store.useModel)({
|
|
30
|
+
state: {
|
|
31
|
+
col: 0,
|
|
32
|
+
row: 0,
|
|
33
|
+
width: 0,
|
|
34
|
+
height: 0
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
const layoutState = model.useGetState();
|
|
38
|
+
(0, _useEffectWithTarget.useEffectWithTarget)(() => {
|
|
39
|
+
let offResize;
|
|
40
|
+
const pxTransform = (base, width) => {
|
|
41
|
+
if (configRef.current?.pxTransform) {
|
|
42
|
+
return configRef.current?.pxTransform(base, width);
|
|
43
|
+
}
|
|
44
|
+
return defaultTransform(base);
|
|
45
|
+
};
|
|
46
|
+
const onResize = () => {
|
|
47
|
+
clearTimeout(timerRef.current);
|
|
48
|
+
timerRef.current = setTimeout(() => {
|
|
49
|
+
const {
|
|
50
|
+
gap,
|
|
51
|
+
baseHeight,
|
|
52
|
+
baseWidth
|
|
53
|
+
} = configRef.current || {};
|
|
54
|
+
// eslint-disable-next-line @typescript-eslint/no-shadow
|
|
55
|
+
const config = configRef.current;
|
|
56
|
+
let width;
|
|
57
|
+
let height;
|
|
58
|
+
if (config?.containerRef?.current) {
|
|
59
|
+
const container = config.containerRef.current;
|
|
60
|
+
const scrollContainer = (0, _utils.getScrollContainer)(config.containerRef.current);
|
|
61
|
+
width = container.offsetWidth;
|
|
62
|
+
height = container.offsetHeight;
|
|
63
|
+
if (scrollContainer !== container && config.removeScrollBarWidth !== false) {
|
|
64
|
+
width += scrollBarSize.width;
|
|
65
|
+
height += scrollBarSize.height;
|
|
66
|
+
}
|
|
67
|
+
} else {
|
|
68
|
+
width = window.innerWidth;
|
|
69
|
+
height = window.innerHeight;
|
|
70
|
+
}
|
|
71
|
+
let colGap = 0;
|
|
72
|
+
let rowGap = 0;
|
|
73
|
+
if (gap) {
|
|
74
|
+
if (Array.isArray(gap)) {
|
|
75
|
+
colGap = pxTransform(gap[0], width);
|
|
76
|
+
rowGap = pxTransform(gap[1], height);
|
|
77
|
+
} else {
|
|
78
|
+
colGap = rowGap = pxTransform(gap, width);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
const newState = {};
|
|
82
|
+
const col = Math.abs(model.getState().width - width) === scrollBarSize.width ? model.getState().col : getCount(pxTransform(baseWidth || 0, width), colGap, width);
|
|
83
|
+
newState.col = col;
|
|
84
|
+
newState.row = 0;
|
|
85
|
+
newState.width = width;
|
|
86
|
+
newState.height = height;
|
|
87
|
+
newState.windowWidth = window.innerWidth;
|
|
88
|
+
newState.windowHeight = window.innerHeight;
|
|
89
|
+
if (baseHeight) {
|
|
90
|
+
const row = Math.abs(model.getState().height - height) === scrollBarSize.height ? model.getState().row : getCount(pxTransform(baseHeight, height), rowGap, height) + 1;
|
|
91
|
+
newState.row = row;
|
|
92
|
+
}
|
|
93
|
+
const oldState = model.getState();
|
|
94
|
+
if (newState.col === oldState.col && newState.row === oldState.row) return;
|
|
95
|
+
model.setState(newState);
|
|
96
|
+
configRef.current?.onChange?.(model.getState());
|
|
97
|
+
}, 17);
|
|
98
|
+
};
|
|
99
|
+
onResize();
|
|
100
|
+
if (config?.containerRef?.current) {
|
|
101
|
+
offResize = (0, _utils.resizeObserver)({
|
|
102
|
+
el: config.containerRef.current,
|
|
103
|
+
onResize
|
|
104
|
+
});
|
|
105
|
+
} else {
|
|
106
|
+
offResize = (0, _utils.listen)(window, 'resize', onResize);
|
|
107
|
+
}
|
|
108
|
+
return () => {
|
|
109
|
+
offResize();
|
|
110
|
+
};
|
|
111
|
+
}, config?.containerRef);
|
|
112
|
+
(0, _ahooks.useUnmount)(() => {
|
|
113
|
+
clearTimeout(timerRef.current);
|
|
114
|
+
});
|
|
115
|
+
return layoutState;
|
|
116
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.DEFAULT_PAGE_SIZE_OPTIONS = exports.DEFAULT_PAGE_SIZE = void 0;
|
|
7
|
+
const DEFAULT_PAGE_SIZE = exports.DEFAULT_PAGE_SIZE = 10;
|
|
8
|
+
const DEFAULT_PAGE_SIZE_OPTIONS = exports.DEFAULT_PAGE_SIZE_OPTIONS = [10, 20, 50, 100];
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { TService, TMediaQueryItem, UsePaginationParams, UsePaginationReturnValue, UsePaginationReturnValueWidthResponsive, UsePaginationReturnValueWidthListLayout, IData } from './type';
|
|
2
|
+
import { UseListLayoutConfig } from '../useListLayout';
|
|
3
|
+
export declare function usePagination<TData extends IData = any, TFormValues extends Record<string, any> = Record<string, any>>(service: TService<TData, TFormValues>, params?: UsePaginationParams): UsePaginationReturnValue<TData>;
|
|
4
|
+
export declare function usePaginationWithResponsive<TData extends IData = any, TFormValues extends Record<string, any> = Record<string, any>>(service: TService<TData, TFormValues>, params: UsePaginationParams & {
|
|
5
|
+
mediaQuery: TMediaQueryItem[];
|
|
6
|
+
}): UsePaginationReturnValueWidthResponsive<TData>;
|
|
7
|
+
/**
|
|
8
|
+
* 列表布局
|
|
9
|
+
*/
|
|
10
|
+
export declare function usePaginationWithListLayout<TData extends IData = any, TFormValues extends Record<string, any> = Record<string, any>>(service: TService<TData, TFormValues>, params: UsePaginationParams & {
|
|
11
|
+
listLayoutConfig: UseListLayoutConfig & {
|
|
12
|
+
row?: number;
|
|
13
|
+
};
|
|
14
|
+
}): UsePaginationReturnValueWidthListLayout<TData>;
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.usePagination = usePagination;
|
|
7
|
+
exports.usePaginationWithListLayout = usePaginationWithListLayout;
|
|
8
|
+
exports.usePaginationWithResponsive = usePaginationWithResponsive;
|
|
9
|
+
var _ahooks = require("ahooks");
|
|
10
|
+
var _store = require("@chaomingd/store");
|
|
11
|
+
var _useResponsive = require("../useResponsive");
|
|
12
|
+
var _react = require("react");
|
|
13
|
+
var _constant = require("./constant");
|
|
14
|
+
var _useListLayout = require("../useListLayout");
|
|
15
|
+
var _utils = require("./utils");
|
|
16
|
+
function usePagination(service, params) {
|
|
17
|
+
const serviceRef = (0, _ahooks.useLatest)(service);
|
|
18
|
+
const paramsRef = (0, _ahooks.useLatest)(params);
|
|
19
|
+
const model = (0, _store.useModel)({
|
|
20
|
+
state: {
|
|
21
|
+
items: params?.defaultData || [],
|
|
22
|
+
current: params?.defaultCurrent || 1,
|
|
23
|
+
pageSize: params?.defaultPageSize || _constant.DEFAULT_PAGE_SIZE,
|
|
24
|
+
loading: false,
|
|
25
|
+
totalCount: 0,
|
|
26
|
+
pageSizeOptions: _constant.DEFAULT_PAGE_SIZE_OPTIONS,
|
|
27
|
+
total: 0,
|
|
28
|
+
list: []
|
|
29
|
+
},
|
|
30
|
+
effects: {
|
|
31
|
+
fetchData: (0, _store.createAsyncEffect)(async () => {
|
|
32
|
+
const form = paramsRef.current?.form;
|
|
33
|
+
const {
|
|
34
|
+
current,
|
|
35
|
+
pageSize
|
|
36
|
+
} = model.getState();
|
|
37
|
+
if (!pageSize) return null;
|
|
38
|
+
const res = await serviceRef.current?.(current, pageSize, form ? form.getFieldsValue() : {}).catch(err => console.log(err));
|
|
39
|
+
if (res) {
|
|
40
|
+
return {
|
|
41
|
+
items: res.list || res.items || [],
|
|
42
|
+
totalCount: res.total || res.totalCount || 0,
|
|
43
|
+
data: res
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
return null;
|
|
47
|
+
}, {
|
|
48
|
+
loadingKey: 'loading'
|
|
49
|
+
})
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
(0, _react.useEffect)(() => {
|
|
53
|
+
if (paramsRef.current?.manu !== true) {
|
|
54
|
+
model.getEffect('fetchData')();
|
|
55
|
+
}
|
|
56
|
+
}, params?.refreshDeps || []);
|
|
57
|
+
const state = model.useGetState();
|
|
58
|
+
const returnValue = {
|
|
59
|
+
model,
|
|
60
|
+
items: state.items,
|
|
61
|
+
list: state.list,
|
|
62
|
+
data: state.data,
|
|
63
|
+
loading: state.loading,
|
|
64
|
+
refresh: options => {
|
|
65
|
+
return model.getEffect('fetchData')(options);
|
|
66
|
+
},
|
|
67
|
+
search: options => {
|
|
68
|
+
model.setState({
|
|
69
|
+
current: 1
|
|
70
|
+
}, {
|
|
71
|
+
silent: true
|
|
72
|
+
});
|
|
73
|
+
return model.getEffect('fetchData')(options);
|
|
74
|
+
},
|
|
75
|
+
reset: options => {
|
|
76
|
+
model.setState({
|
|
77
|
+
current: 1
|
|
78
|
+
}, {
|
|
79
|
+
silent: true
|
|
80
|
+
});
|
|
81
|
+
const form = paramsRef.current?.form;
|
|
82
|
+
if (form) {
|
|
83
|
+
form.resetFields();
|
|
84
|
+
}
|
|
85
|
+
return model.getEffect('fetchData')(options);
|
|
86
|
+
},
|
|
87
|
+
paginationProps: {
|
|
88
|
+
current: state.current,
|
|
89
|
+
pageSize: state.pageSize,
|
|
90
|
+
pageSizeOptions: state.pageSizeOptions,
|
|
91
|
+
total: state.totalCount,
|
|
92
|
+
onChange: (current, pageSize) => {
|
|
93
|
+
model.setState({
|
|
94
|
+
current,
|
|
95
|
+
pageSize
|
|
96
|
+
}, {
|
|
97
|
+
silent: true
|
|
98
|
+
});
|
|
99
|
+
model.getEffect('fetchData')();
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
return returnValue;
|
|
104
|
+
}
|
|
105
|
+
function usePaginationWithResponsive(service, params) {
|
|
106
|
+
const paginationValue = usePagination(service, params);
|
|
107
|
+
const {
|
|
108
|
+
mediaQueryMatchedItem,
|
|
109
|
+
matchMedia
|
|
110
|
+
} = (0, _useResponsive.useResponsive)({
|
|
111
|
+
mediaQuery: params.mediaQuery,
|
|
112
|
+
// eslint-disable-next-line @typescript-eslint/no-shadow
|
|
113
|
+
onChange: mediaQueryMatchedItem => {
|
|
114
|
+
const pageSize = mediaQueryMatchedItem.data.col * mediaQueryMatchedItem.data.row;
|
|
115
|
+
const pageSizeOptions = (0, _utils.gerationPageSizeOptions)(pageSize);
|
|
116
|
+
paginationValue.model.setState({
|
|
117
|
+
pageSizeOptions,
|
|
118
|
+
pageSize
|
|
119
|
+
}, {
|
|
120
|
+
silent: true
|
|
121
|
+
});
|
|
122
|
+
paginationValue.refresh();
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
paginationValue.mediaQueryMatchedItem = mediaQueryMatchedItem;
|
|
126
|
+
paginationValue.matchMedia = matchMedia;
|
|
127
|
+
return paginationValue;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* 列表布局
|
|
132
|
+
*/
|
|
133
|
+
function usePaginationWithListLayout(service, params) {
|
|
134
|
+
const paginationValue = usePagination(service, {
|
|
135
|
+
...(params || {}),
|
|
136
|
+
manu: true
|
|
137
|
+
});
|
|
138
|
+
const listLayout = (0, _useListLayout.useListLayout)(params.listLayoutConfig);
|
|
139
|
+
const row = Math.max(params.listLayoutConfig.row || 0, listLayout.row);
|
|
140
|
+
(0, _react.useEffect)(() => {
|
|
141
|
+
if (row) {
|
|
142
|
+
const pageSize = row * listLayout.col;
|
|
143
|
+
const pageSizeOptions = (0, _utils.gerationPageSizeOptions)(pageSize);
|
|
144
|
+
paginationValue.model.setState({
|
|
145
|
+
pageSizeOptions,
|
|
146
|
+
pageSize,
|
|
147
|
+
current: 1
|
|
148
|
+
}, {
|
|
149
|
+
silent: true
|
|
150
|
+
});
|
|
151
|
+
paginationValue.refresh();
|
|
152
|
+
}
|
|
153
|
+
}, [listLayout, ...(params.refreshDeps || [])]);
|
|
154
|
+
Object.assign(paginationValue, listLayout);
|
|
155
|
+
paginationValue.row = row;
|
|
156
|
+
return paginationValue;
|
|
157
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { Nullable } from '@chaomingd/utils';
|
|
2
|
+
import { FormInstance } from 'antd';
|
|
3
|
+
import { DependencyList } from 'react';
|
|
4
|
+
import { Model } from '@chaomingd/store';
|
|
5
|
+
import { IResponsiveConfigItem } from '../useResponsive';
|
|
6
|
+
export type TMediaQueryItem = IResponsiveConfigItem<{
|
|
7
|
+
col: number;
|
|
8
|
+
row: number;
|
|
9
|
+
}>;
|
|
10
|
+
export interface UsePaginationParams<TDataItem extends any = any> {
|
|
11
|
+
defaultData?: TDataItem[];
|
|
12
|
+
defaultPageSize?: number;
|
|
13
|
+
defaultCurrent?: number;
|
|
14
|
+
refreshDeps?: DependencyList;
|
|
15
|
+
form?: FormInstance;
|
|
16
|
+
manu?: boolean;
|
|
17
|
+
}
|
|
18
|
+
export interface IData {
|
|
19
|
+
totalCount?: number;
|
|
20
|
+
items?: any[];
|
|
21
|
+
total?: number;
|
|
22
|
+
list?: any[];
|
|
23
|
+
}
|
|
24
|
+
export type TService<TData extends IData = any, TFormValues extends Record<string, any> = Record<string, any>> = (current: number, pageSize: number, formValues: TFormValues, ...args: any[]) => Promise<Nullable<TData>>;
|
|
25
|
+
export interface fetchDataOptions {
|
|
26
|
+
showLoading?: boolean;
|
|
27
|
+
}
|
|
28
|
+
export interface UsePaginationReturnValue<TData extends IData = any> {
|
|
29
|
+
model: Model<IUsePaginationState<TData>, UsePaginationEffects>;
|
|
30
|
+
items: TData['items'];
|
|
31
|
+
list: TData['list'];
|
|
32
|
+
data?: TData;
|
|
33
|
+
loading: boolean;
|
|
34
|
+
paginationProps: {
|
|
35
|
+
current: number;
|
|
36
|
+
pageSize: number;
|
|
37
|
+
total: number;
|
|
38
|
+
onChange: (current: number, pageSize: number) => void;
|
|
39
|
+
pageSizeOptions: number[];
|
|
40
|
+
};
|
|
41
|
+
refresh: (options?: fetchDataOptions, ...args: any[]) => Promise<Nullable<TData>>;
|
|
42
|
+
reset: (options?: fetchDataOptions, ...args: any[]) => Promise<Nullable<TData>>;
|
|
43
|
+
search: (options?: fetchDataOptions, ...args: any[]) => Promise<Nullable<TData>>;
|
|
44
|
+
}
|
|
45
|
+
export interface UsePaginationReturnValueWidthResponsive<TData extends IData = any> extends UsePaginationReturnValue<TData> {
|
|
46
|
+
mediaQueryMatchedItem: TMediaQueryItem;
|
|
47
|
+
matchMedia: () => void;
|
|
48
|
+
}
|
|
49
|
+
export interface UsePaginationReturnValueWidthListLayout<TData extends IData = any> extends UsePaginationReturnValue<TData> {
|
|
50
|
+
col: number;
|
|
51
|
+
row: number;
|
|
52
|
+
}
|
|
53
|
+
export interface UsePaginationEffects<TData extends IData = any> {
|
|
54
|
+
fetchData: (options?: {
|
|
55
|
+
showLoading?: boolean;
|
|
56
|
+
}, ...args: any[]) => Promise<Nullable<TData>>;
|
|
57
|
+
}
|
|
58
|
+
export interface IUsePaginationState<TData extends IData = any> {
|
|
59
|
+
items: TData['items'];
|
|
60
|
+
loading: boolean;
|
|
61
|
+
current: number;
|
|
62
|
+
pageSize: number;
|
|
63
|
+
totalCount: number;
|
|
64
|
+
pageSizeOptions: number[];
|
|
65
|
+
data?: TData;
|
|
66
|
+
total: number;
|
|
67
|
+
list: TData['items'];
|
|
68
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function gerationPageSizeOptions(pageSize: number): number[];
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.gerationPageSizeOptions = gerationPageSizeOptions;
|
|
7
|
+
function gerationPageSizeOptions(pageSize) {
|
|
8
|
+
const count = 5;
|
|
9
|
+
const pageSizeOptions = [];
|
|
10
|
+
for (let i = 1; i <= count; i++) {
|
|
11
|
+
pageSizeOptions.push(pageSize * i);
|
|
12
|
+
}
|
|
13
|
+
return pageSizeOptions;
|
|
14
|
+
}
|