@vef-framework/hooks 1.0.122 → 1.0.123
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/cjs/index.cjs +107 -0
- package/cjs/lib.cjs +56 -0
- package/cjs/use-authorized-items.cjs +26 -0
- package/cjs/use-color-tokens.cjs +34 -0
- package/cjs/use-computed-action-buttons.cjs +45 -0
- package/cjs/use-computed-options.cjs +69 -0
- package/cjs/use-context-disabled.cjs +14 -0
- package/cjs/use-data-query.cjs +110 -0
- package/cjs/use-deep-callback.cjs +13 -0
- package/cjs/use-deep-memo.cjs +21 -0
- package/cjs/use-deep-selector.cjs +16 -0
- package/cjs/use-fallback-options.cjs +105 -0
- package/cjs/use-gap-size-normalizer.cjs +38 -0
- package/cjs/use-normalized-gap-size.cjs +12 -0
- package/cjs/use-normalized-menu-items.cjs +48 -0
- package/cjs/use-normalized-options.cjs +104 -0
- package/cjs/use-option-filter.cjs +20 -0
- package/cjs/use-remote-filter.cjs +39 -0
- package/cjs/use-shallow-callback.cjs +13 -0
- package/cjs/use-shallow-memo.cjs +21 -0
- package/cjs/use-shallow-selector.cjs +16 -0
- package/cjs/use-singleton.cjs +16 -0
- package/cjs/use-theme-tokens.cjs +13 -0
- package/cjs/use-transient-store.cjs +34 -0
- package/cjs/use-window-size.cjs +21 -0
- package/esm/index.js +29 -0
- package/esm/lib.js +5 -0
- package/esm/use-authorized-items.js +24 -0
- package/esm/use-color-tokens.js +30 -0
- package/esm/use-computed-action-buttons.js +43 -0
- package/esm/use-computed-options.js +67 -0
- package/esm/use-context-disabled.js +11 -0
- package/esm/use-data-query.js +108 -0
- package/esm/use-deep-callback.js +11 -0
- package/esm/use-deep-memo.js +19 -0
- package/esm/use-deep-selector.js +14 -0
- package/esm/use-fallback-options.js +103 -0
- package/esm/use-gap-size-normalizer.js +36 -0
- package/esm/use-normalized-gap-size.js +10 -0
- package/esm/use-normalized-menu-items.js +45 -0
- package/esm/use-normalized-options.js +102 -0
- package/esm/use-option-filter.js +18 -0
- package/esm/use-remote-filter.js +37 -0
- package/esm/use-shallow-callback.js +11 -0
- package/esm/use-shallow-memo.js +19 -0
- package/esm/use-shallow-selector.js +14 -0
- package/esm/use-singleton.js +14 -0
- package/esm/use-theme-tokens.js +11 -0
- package/esm/use-transient-store.js +32 -0
- package/esm/use-window-size.js +19 -0
- package/package.json +9 -8
- package/es/index.js +0 -1
- package/es/lib.js +0 -1
- package/es/use-authorized-items.js +0 -1
- package/es/use-color-tokens.js +0 -1
- package/es/use-computed-action-buttons.js +0 -1
- package/es/use-computed-options.js +0 -1
- package/es/use-context-disabled.js +0 -1
- package/es/use-data-query.js +0 -1
- package/es/use-deep-callback.js +0 -1
- package/es/use-deep-memo.js +0 -1
- package/es/use-deep-selector.js +0 -1
- package/es/use-fallback-options.js +0 -1
- package/es/use-gap-size-normalizer.js +0 -1
- package/es/use-normalized-gap-size.js +0 -1
- package/es/use-normalized-menu-items.js +0 -1
- package/es/use-normalized-options.js +0 -1
- package/es/use-option-filter.js +0 -1
- package/es/use-remote-filter.js +0 -1
- package/es/use-shallow-callback.js +0 -1
- package/es/use-shallow-memo.js +0 -1
- package/es/use-shallow-selector.js +0 -1
- package/es/use-singleton.js +0 -1
- package/es/use-theme-tokens.js +0 -1
- package/es/use-transient-store.js +0 -1
- package/es/use-window-size.js +0 -1
- package/lib/index.cjs +0 -1
- package/lib/lib.cjs +0 -1
- package/lib/use-authorized-items.cjs +0 -1
- package/lib/use-color-tokens.cjs +0 -1
- package/lib/use-computed-action-buttons.cjs +0 -1
- package/lib/use-computed-options.cjs +0 -1
- package/lib/use-context-disabled.cjs +0 -1
- package/lib/use-data-query.cjs +0 -1
- package/lib/use-deep-callback.cjs +0 -1
- package/lib/use-deep-memo.cjs +0 -1
- package/lib/use-deep-selector.cjs +0 -1
- package/lib/use-fallback-options.cjs +0 -1
- package/lib/use-gap-size-normalizer.cjs +0 -1
- package/lib/use-normalized-gap-size.cjs +0 -1
- package/lib/use-normalized-menu-items.cjs +0 -1
- package/lib/use-normalized-options.cjs +0 -1
- package/lib/use-option-filter.cjs +0 -1
- package/lib/use-remote-filter.cjs +0 -1
- package/lib/use-shallow-callback.cjs +0 -1
- package/lib/use-shallow-memo.cjs +0 -1
- package/lib/use-shallow-selector.cjs +0 -1
- package/lib/use-singleton.cjs +0 -1
- package/lib/use-theme-tokens.cjs +0 -1
- package/lib/use-transient-store.cjs +0 -1
- package/lib/use-window-size.cjs +0 -1
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
var react = require('react');
|
|
5
|
+
|
|
6
|
+
"use strict";
|
|
7
|
+
function normalizeMenuItem(item) {
|
|
8
|
+
if (item.type === "item") {
|
|
9
|
+
return {
|
|
10
|
+
type: "item",
|
|
11
|
+
key: item.key,
|
|
12
|
+
label: item.label,
|
|
13
|
+
disabled: item.disabled,
|
|
14
|
+
icon: item.icon,
|
|
15
|
+
extra: item.extra
|
|
16
|
+
};
|
|
17
|
+
} else if (item.type === "submenu") {
|
|
18
|
+
return {
|
|
19
|
+
type: "submenu",
|
|
20
|
+
key: item.key,
|
|
21
|
+
label: item.label,
|
|
22
|
+
disabled: item.disabled,
|
|
23
|
+
icon: item.icon,
|
|
24
|
+
children: item.children.map(normalizeMenuItem)
|
|
25
|
+
};
|
|
26
|
+
} else if (item.type === "group") {
|
|
27
|
+
return {
|
|
28
|
+
type: "group",
|
|
29
|
+
key: item.key,
|
|
30
|
+
label: item.label,
|
|
31
|
+
children: item.children.map(normalizeMenuItem)
|
|
32
|
+
};
|
|
33
|
+
} else {
|
|
34
|
+
return {
|
|
35
|
+
type: "divider",
|
|
36
|
+
key: item.key
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
function useNormalizedMenuItems(items) {
|
|
41
|
+
return react.useMemo(
|
|
42
|
+
() => items.map(normalizeMenuItem),
|
|
43
|
+
[items]
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
exports.normalizeMenuItem = normalizeMenuItem;
|
|
48
|
+
exports.useNormalizedMenuItems = useNormalizedMenuItems;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
var shared = require('@vef-framework/shared');
|
|
5
|
+
var react = require('react');
|
|
6
|
+
|
|
7
|
+
"use strict";
|
|
8
|
+
function useNormalizedDataOptions(options, {
|
|
9
|
+
labelKey = "label",
|
|
10
|
+
valueKey = "value",
|
|
11
|
+
childrenKey = "children",
|
|
12
|
+
descriptionKey = "description",
|
|
13
|
+
disabledKey = "disabled",
|
|
14
|
+
defaultToFirst = false
|
|
15
|
+
}, {
|
|
16
|
+
isTree = false,
|
|
17
|
+
isGrouped = false,
|
|
18
|
+
parsePinyin = false,
|
|
19
|
+
selectedOptionValues = []
|
|
20
|
+
} = {}) {
|
|
21
|
+
return react.useMemo(() => {
|
|
22
|
+
const selectedOptionValuesSet = new Set(selectedOptionValues);
|
|
23
|
+
const isGroupedOptions = !isTree && isGrouped;
|
|
24
|
+
const normalizeOption = (option, isTopLevel = false) => {
|
|
25
|
+
const label = option[labelKey];
|
|
26
|
+
if (shared.isNullish(label)) {
|
|
27
|
+
if (labelKey === "label") {
|
|
28
|
+
throw new shared.VefError(-10001, `The label value of the option is required.`);
|
|
29
|
+
}
|
|
30
|
+
throw new shared.VefError(-10001, `The label value pointed by '${labelKey}' of the option is required.`);
|
|
31
|
+
}
|
|
32
|
+
const value = option[valueKey];
|
|
33
|
+
if (shared.isNullish(value)) {
|
|
34
|
+
if (valueKey === "value") {
|
|
35
|
+
throw new shared.VefError(-10002, `The value of the option is required.`);
|
|
36
|
+
}
|
|
37
|
+
throw new shared.VefError(-10002, `The value pointed by '${valueKey}' of the option is required.`);
|
|
38
|
+
}
|
|
39
|
+
const children = option[childrenKey];
|
|
40
|
+
const description = option[descriptionKey];
|
|
41
|
+
const disabled = option[disabledKey] ?? false;
|
|
42
|
+
const normalizedOption = {
|
|
43
|
+
...option,
|
|
44
|
+
label,
|
|
45
|
+
value,
|
|
46
|
+
children,
|
|
47
|
+
description,
|
|
48
|
+
disabled
|
|
49
|
+
};
|
|
50
|
+
if (parsePinyin) {
|
|
51
|
+
const { labelText, descriptionText } = normalizedOption;
|
|
52
|
+
if (shared.isString(label) || shared.isString(labelText)) {
|
|
53
|
+
normalizedOption.labelPinyin = shared.parsePinyinFirstLetter(
|
|
54
|
+
shared.isString(label) ? label : labelText
|
|
55
|
+
).join("");
|
|
56
|
+
}
|
|
57
|
+
if (shared.isString(description) || shared.isString(descriptionText)) {
|
|
58
|
+
normalizedOption.descriptionPinyin = shared.parsePinyinFirstLetter(
|
|
59
|
+
shared.isString(description) ? description : descriptionText
|
|
60
|
+
).join("");
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
if ((!isTopLevel || !isGroupedOptions) && selectedOptionValuesSet.has(value)) {
|
|
64
|
+
selectedOptionValuesSet.delete(value);
|
|
65
|
+
}
|
|
66
|
+
if (shared.isArray(normalizedOption.children)) {
|
|
67
|
+
normalizedOption.children = normalizedOption.children.map((option2) => normalizeOption(option2));
|
|
68
|
+
}
|
|
69
|
+
return normalizedOption;
|
|
70
|
+
};
|
|
71
|
+
const normalizedOptions = options.map((option) => normalizeOption(option, true));
|
|
72
|
+
const missingOptionValues = Array.from(selectedOptionValuesSet);
|
|
73
|
+
if (!defaultToFirst) {
|
|
74
|
+
return [normalizedOptions, missingOptionValues];
|
|
75
|
+
}
|
|
76
|
+
if (isGroupedOptions) {
|
|
77
|
+
const firstNonEmptyGroupOption = normalizedOptions.find((item) => shared.isArray(item.children) && item.children.length > 0);
|
|
78
|
+
return [
|
|
79
|
+
normalizedOptions,
|
|
80
|
+
missingOptionValues,
|
|
81
|
+
firstNonEmptyGroupOption?.children?.[0]
|
|
82
|
+
];
|
|
83
|
+
}
|
|
84
|
+
return [
|
|
85
|
+
normalizedOptions,
|
|
86
|
+
missingOptionValues,
|
|
87
|
+
normalizedOptions[0]
|
|
88
|
+
];
|
|
89
|
+
}, [
|
|
90
|
+
childrenKey,
|
|
91
|
+
defaultToFirst,
|
|
92
|
+
descriptionKey,
|
|
93
|
+
disabledKey,
|
|
94
|
+
isGrouped,
|
|
95
|
+
isTree,
|
|
96
|
+
labelKey,
|
|
97
|
+
options,
|
|
98
|
+
parsePinyin,
|
|
99
|
+
// selectedOptionValues,
|
|
100
|
+
valueKey
|
|
101
|
+
]);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
exports.useNormalizedDataOptions = useNormalizedDataOptions;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
var shared = require('@vef-framework/shared');
|
|
5
|
+
var react = require('react');
|
|
6
|
+
|
|
7
|
+
"use strict";
|
|
8
|
+
function useOptionFilter(filterable, filterFromRemote) {
|
|
9
|
+
return react.useMemo(() => {
|
|
10
|
+
if (!filterable) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
if (filterFromRemote) {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
return (filterValue, option) => (shared.isString(option.label) && option.label.includes(filterValue) || shared.isString(option.labelText) && option.labelText.includes(filterValue) || option.labelPinyin?.includes(filterValue) || option.value.includes(filterValue) || shared.isString(option.description) && option.description.includes(filterValue) || shared.isString(option.descriptionText) && option.descriptionText.includes(filterValue) || option.descriptionPinyin?.includes(filterValue)) ?? false;
|
|
17
|
+
}, [filterable, filterFromRemote]);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
exports.useOptionFilter = useOptionFilter;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
var shared = require('@vef-framework/shared');
|
|
5
|
+
var react = require('react');
|
|
6
|
+
|
|
7
|
+
"use strict";
|
|
8
|
+
function useRemoteFilter(enabled, apiParams, keywordKey = "keyword") {
|
|
9
|
+
const [filterKeyword, setFilterKeyword] = react.useState();
|
|
10
|
+
const mergedApiParams = react.useMemo(() => {
|
|
11
|
+
if (!enabled) {
|
|
12
|
+
return apiParams;
|
|
13
|
+
}
|
|
14
|
+
const params = {
|
|
15
|
+
...apiParams
|
|
16
|
+
};
|
|
17
|
+
const keywordToUse = shared.trim(filterKeyword);
|
|
18
|
+
if (keywordToUse) {
|
|
19
|
+
params[keywordKey] = keywordToUse;
|
|
20
|
+
}
|
|
21
|
+
return params;
|
|
22
|
+
}, [
|
|
23
|
+
enabled,
|
|
24
|
+
apiParams,
|
|
25
|
+
filterKeyword,
|
|
26
|
+
keywordKey
|
|
27
|
+
]);
|
|
28
|
+
const handleInputKeyword = react.useMemo(() => {
|
|
29
|
+
if (!enabled) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
return shared.debounce({ delay: 500 }, (keyword) => {
|
|
33
|
+
setFilterKeyword(keyword);
|
|
34
|
+
});
|
|
35
|
+
}, [enabled]);
|
|
36
|
+
return [mergedApiParams, handleInputKeyword];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
exports.useRemoteFilter = useRemoteFilter;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
var react = require('react');
|
|
5
|
+
var useShallowMemo = require('./use-shallow-memo.cjs');
|
|
6
|
+
|
|
7
|
+
"use strict";
|
|
8
|
+
function useShallowCallback(callback, dependencies) {
|
|
9
|
+
const memoizedDependencies = useShallowMemo.useShallowMemo(() => dependencies, dependencies);
|
|
10
|
+
return react.useCallback(callback, memoizedDependencies);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
exports.useShallowCallback = useShallowCallback;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
var shared = require('@vef-framework/shared');
|
|
5
|
+
var react = require('react');
|
|
6
|
+
|
|
7
|
+
"use strict";
|
|
8
|
+
function useShallowMemo(factory, dependencies) {
|
|
9
|
+
const lastDependencies = react.useRef();
|
|
10
|
+
const signal = react.useRef(0);
|
|
11
|
+
if (lastDependencies.current === void 0 || !isEqual(dependencies, lastDependencies.current)) {
|
|
12
|
+
signal.current += 1;
|
|
13
|
+
}
|
|
14
|
+
lastDependencies.current = dependencies;
|
|
15
|
+
return react.useMemo(factory, [signal.current]);
|
|
16
|
+
}
|
|
17
|
+
function isEqual(one, another) {
|
|
18
|
+
return one.length === another.length && one.every((value, index) => shared.isShallowEqual(value, another[index]));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
exports.useShallowMemo = useShallowMemo;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
var shared = require('@vef-framework/shared');
|
|
5
|
+
var react = require('react');
|
|
6
|
+
|
|
7
|
+
"use strict";
|
|
8
|
+
function useShallowSelector(selector) {
|
|
9
|
+
const prevSelectedState = react.useRef();
|
|
10
|
+
return (state) => {
|
|
11
|
+
const nextSelectedState = selector(state);
|
|
12
|
+
return shared.isShallowEqual(prevSelectedState.current, nextSelectedState) ? prevSelectedState.current : prevSelectedState.current = nextSelectedState;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
exports.useShallowSelector = useShallowSelector;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
var shared = require('@vef-framework/shared');
|
|
5
|
+
var react = require('react');
|
|
6
|
+
|
|
7
|
+
"use strict";
|
|
8
|
+
function useSingleton(factory) {
|
|
9
|
+
const singleton = react.useRef();
|
|
10
|
+
if (shared.isNullish(singleton.current)) {
|
|
11
|
+
singleton.current = factory();
|
|
12
|
+
}
|
|
13
|
+
return singleton.current;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
exports.useSingleton = useSingleton;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
var shared = require('@vef-framework/shared');
|
|
5
|
+
var react = require('react');
|
|
6
|
+
|
|
7
|
+
"use strict";
|
|
8
|
+
function useTransientStore({ subscribe, getState }, {
|
|
9
|
+
selector = (state) => state,
|
|
10
|
+
equalityFn = shared.isShallowEqual,
|
|
11
|
+
listener
|
|
12
|
+
} = {}) {
|
|
13
|
+
const stateRef = react.useRef();
|
|
14
|
+
if (!stateRef.current) {
|
|
15
|
+
stateRef.current = selector(getState());
|
|
16
|
+
}
|
|
17
|
+
react.useEffect(
|
|
18
|
+
() => subscribe(
|
|
19
|
+
selector,
|
|
20
|
+
(state, prevState) => {
|
|
21
|
+
stateRef.current = state;
|
|
22
|
+
listener?.(state, prevState);
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
fireImmediately: false,
|
|
26
|
+
equalityFn
|
|
27
|
+
}
|
|
28
|
+
),
|
|
29
|
+
[equalityFn, listener, selector, subscribe]
|
|
30
|
+
);
|
|
31
|
+
return stateRef;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
exports.useTransientStore = useTransientStore;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
var ahooks = require('ahooks');
|
|
5
|
+
|
|
6
|
+
"use strict";
|
|
7
|
+
function useWindowSize() {
|
|
8
|
+
const [size, setSize] = ahooks.useRafState(() => ({
|
|
9
|
+
width: window.innerWidth,
|
|
10
|
+
height: window.innerHeight
|
|
11
|
+
}));
|
|
12
|
+
ahooks.useEventListener("resize", () => {
|
|
13
|
+
setSize({
|
|
14
|
+
width: window.innerWidth,
|
|
15
|
+
height: window.innerHeight
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
return size;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
exports.useWindowSize = useWindowSize;
|
package/esm/index.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
import './lib.js';
|
|
3
|
+
export { useAuthorizedItems } from './use-authorized-items.js';
|
|
4
|
+
export { useColorTokens, useDefaultColorTokens, useSemanticColorTokens } from './use-color-tokens.js';
|
|
5
|
+
export { useComputedActionButtons } from './use-computed-action-buttons.js';
|
|
6
|
+
export { useComputedOptions } from './use-computed-options.js';
|
|
7
|
+
export { DisabledContextProvider, useContextDisabled } from './use-context-disabled.js';
|
|
8
|
+
export { useDataQuery } from './use-data-query.js';
|
|
9
|
+
export { useDeepCallback } from './use-deep-callback.js';
|
|
10
|
+
export { useDeepMemo } from './use-deep-memo.js';
|
|
11
|
+
export { useDeepSelector } from './use-deep-selector.js';
|
|
12
|
+
export { useFallbackOptions } from './use-fallback-options.js';
|
|
13
|
+
export { useGapSizeNormalizer } from './use-gap-size-normalizer.js';
|
|
14
|
+
export { useNormalizedGapSize } from './use-normalized-gap-size.js';
|
|
15
|
+
export { normalizeMenuItem, useNormalizedMenuItems } from './use-normalized-menu-items.js';
|
|
16
|
+
export { useNormalizedDataOptions } from './use-normalized-options.js';
|
|
17
|
+
export { useOptionFilter } from './use-option-filter.js';
|
|
18
|
+
export { useRemoteFilter } from './use-remote-filter.js';
|
|
19
|
+
export { useShallowCallback } from './use-shallow-callback.js';
|
|
20
|
+
export { useShallowMemo } from './use-shallow-memo.js';
|
|
21
|
+
export { useShallowSelector } from './use-shallow-selector.js';
|
|
22
|
+
export { useSingleton } from './use-singleton.js';
|
|
23
|
+
export { useThemeTokens } from './use-theme-tokens.js';
|
|
24
|
+
export { useTransientStore } from './use-transient-store.js';
|
|
25
|
+
export { useWindowSize } from './use-window-size.js';
|
|
26
|
+
export { useClickAway, useMeasure } from '@uidotdev/usehooks';
|
|
27
|
+
export { useDeepCompareEffect as useDeepEffect, useDeepCompareLayoutEffect as useDeepLayoutEffect, useSize as useElementSize, useEventListener, useKeyPress, useMount, useUnmount, useUpdateEffect, useUpdateLayoutEffect, useUpdate as useUpdater } from 'ahooks';
|
|
28
|
+
|
|
29
|
+
"use strict";
|
package/esm/lib.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
export { useClickAway, useMeasure } from '@uidotdev/usehooks';
|
|
3
|
+
export { useDeepCompareEffect as useDeepEffect, useDeepCompareLayoutEffect as useDeepLayoutEffect, useSize as useElementSize, useEventListener, useKeyPress, useMount, useUnmount, useUpdateEffect, useUpdateLayoutEffect, useUpdate as useUpdater } from 'ahooks';
|
|
4
|
+
|
|
5
|
+
"use strict";
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
import { useAuthContext } from '@vef-framework/core';
|
|
3
|
+
import { useMemo } from 'react';
|
|
4
|
+
|
|
5
|
+
"use strict";
|
|
6
|
+
function useAuthorizedItems(items) {
|
|
7
|
+
const { checkPermission } = useAuthContext();
|
|
8
|
+
return useMemo(
|
|
9
|
+
() => items?.filter((item) => {
|
|
10
|
+
const { permissions } = item;
|
|
11
|
+
if (!permissions) {
|
|
12
|
+
return true;
|
|
13
|
+
}
|
|
14
|
+
const checkMode = item.checkMode ?? "any";
|
|
15
|
+
if (checkMode === "any") {
|
|
16
|
+
return permissions.some((permission) => checkPermission(permission));
|
|
17
|
+
}
|
|
18
|
+
return permissions.every((permission) => checkPermission(permission));
|
|
19
|
+
}),
|
|
20
|
+
[items, checkPermission]
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export { useAuthorizedItems };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
import { defaultColorTypes, semanticColorTypes, capitalize } from '@vef-framework/shared';
|
|
3
|
+
import { useMemo } from 'react';
|
|
4
|
+
import { useThemeTokens } from './use-theme-tokens.js';
|
|
5
|
+
|
|
6
|
+
"use strict";
|
|
7
|
+
function useDefaultColorTokens() {
|
|
8
|
+
const tokens = useThemeTokens();
|
|
9
|
+
return useMemo(() => defaultColorTypes.reduce(
|
|
10
|
+
(map, color) => {
|
|
11
|
+
map.set(color, tokens[color]);
|
|
12
|
+
return map;
|
|
13
|
+
},
|
|
14
|
+
/* @__PURE__ */ new Map()
|
|
15
|
+
), [tokens]);
|
|
16
|
+
}
|
|
17
|
+
function useSemanticColorTokens() {
|
|
18
|
+
const tokens = useThemeTokens();
|
|
19
|
+
return useMemo(() => semanticColorTypes.reduce((map, color) => {
|
|
20
|
+
map.set(color, tokens[`color${capitalize(color)}`]);
|
|
21
|
+
return map;
|
|
22
|
+
}, /* @__PURE__ */ new Map()), [tokens]);
|
|
23
|
+
}
|
|
24
|
+
function useColorTokens() {
|
|
25
|
+
const defaultColorTokens = useDefaultColorTokens();
|
|
26
|
+
const semanticColorTokens = useSemanticColorTokens();
|
|
27
|
+
return useMemo(() => new Map([...defaultColorTokens, ...semanticColorTokens]), [defaultColorTokens, semanticColorTokens]);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export { useColorTokens, useDefaultColorTokens, useSemanticColorTokens };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
import { isFunction, defaultMessageTitle } from '@vef-framework/shared';
|
|
3
|
+
import { useMemo } from 'react';
|
|
4
|
+
|
|
5
|
+
"use strict";
|
|
6
|
+
function useComputedActionButtons(buttons, context) {
|
|
7
|
+
return useMemo(() => buttons.filter((button) => {
|
|
8
|
+
const { show } = button;
|
|
9
|
+
if (isFunction(show)) {
|
|
10
|
+
return show(context);
|
|
11
|
+
}
|
|
12
|
+
return show ?? true;
|
|
13
|
+
}).map((button) => {
|
|
14
|
+
const {
|
|
15
|
+
disabled,
|
|
16
|
+
requireConfirmation,
|
|
17
|
+
confirmationMode,
|
|
18
|
+
confirmationTitle,
|
|
19
|
+
confirmationContent,
|
|
20
|
+
...rest
|
|
21
|
+
} = button;
|
|
22
|
+
const computedButton = {
|
|
23
|
+
...rest,
|
|
24
|
+
disabled: isFunction(disabled) ? disabled(context) : disabled ?? false,
|
|
25
|
+
requireConfirmation: isFunction(requireConfirmation) ? requireConfirmation(context) : requireConfirmation ?? false,
|
|
26
|
+
confirmationMode: isFunction(confirmationMode) ? confirmationMode(context) : confirmationMode ?? "simple",
|
|
27
|
+
confirmationTitle: isFunction(confirmationTitle) ? confirmationTitle(context) : confirmationTitle ?? defaultMessageTitle,
|
|
28
|
+
confirmationContent: isFunction(confirmationContent) ? confirmationContent(context) : confirmationContent ?? `确定要${rest.label}吗?`
|
|
29
|
+
};
|
|
30
|
+
if (computedButton.requireConfirmation && !computedButton.confirmationMode) {
|
|
31
|
+
computedButton.confirmationMode = "simple";
|
|
32
|
+
}
|
|
33
|
+
if (computedButton.requireConfirmation && !computedButton.confirmationTitle) {
|
|
34
|
+
computedButton.confirmationTitle = defaultMessageTitle;
|
|
35
|
+
}
|
|
36
|
+
if (computedButton.requireConfirmation && !computedButton.confirmationContent) {
|
|
37
|
+
computedButton.confirmationContent = `确定要${computedButton.label}吗?`;
|
|
38
|
+
}
|
|
39
|
+
return computedButton;
|
|
40
|
+
}), [buttons, context]);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export { useComputedActionButtons };
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
import { useDataQuery } from './use-data-query.js';
|
|
3
|
+
import { useFallbackOptions } from './use-fallback-options.js';
|
|
4
|
+
import { useNormalizedDataOptions } from './use-normalized-options.js';
|
|
5
|
+
import { useRemoteFilter } from './use-remote-filter.js';
|
|
6
|
+
|
|
7
|
+
"use strict";
|
|
8
|
+
function useComputedOptions({
|
|
9
|
+
labelKey,
|
|
10
|
+
valueKey,
|
|
11
|
+
descriptionKey,
|
|
12
|
+
childrenKey,
|
|
13
|
+
disabledKey,
|
|
14
|
+
defaultToFirst,
|
|
15
|
+
filterable = false,
|
|
16
|
+
filterFromRemote = false,
|
|
17
|
+
keywordKey = "keyword",
|
|
18
|
+
valuesKey = "ids",
|
|
19
|
+
fallbackOptionsApi,
|
|
20
|
+
fallbackOptionsApiParams,
|
|
21
|
+
resolveFallbackOptions,
|
|
22
|
+
isGrouped = false,
|
|
23
|
+
selectedOptionValues,
|
|
24
|
+
apiParams,
|
|
25
|
+
...queryConfig
|
|
26
|
+
}) {
|
|
27
|
+
const [mergedApiParams, onFilterKeywordChange] = useRemoteFilter(filterable && filterFromRemote, apiParams, keywordKey);
|
|
28
|
+
const { data: options, isLoading } = useDataQuery({
|
|
29
|
+
apiParams: mergedApiParams,
|
|
30
|
+
...queryConfig
|
|
31
|
+
});
|
|
32
|
+
const [normalizedOptions, missingOptionValues, defaultOption] = useNormalizedDataOptions(
|
|
33
|
+
options ?? [],
|
|
34
|
+
{
|
|
35
|
+
labelKey,
|
|
36
|
+
valueKey,
|
|
37
|
+
descriptionKey,
|
|
38
|
+
childrenKey,
|
|
39
|
+
disabledKey,
|
|
40
|
+
defaultToFirst
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
isGrouped,
|
|
44
|
+
parsePinyin: filterable && !filterFromRemote,
|
|
45
|
+
selectedOptionValues
|
|
46
|
+
}
|
|
47
|
+
);
|
|
48
|
+
const { data: fallbackOptions, isLoading: isFallbackOptionsLoading } = useFallbackOptions(
|
|
49
|
+
missingOptionValues,
|
|
50
|
+
{
|
|
51
|
+
fallbackOptionsApi: fallbackOptionsApi ?? queryConfig.api,
|
|
52
|
+
fallbackOptionsApiParams,
|
|
53
|
+
valuesKey,
|
|
54
|
+
resolveFallbackOptions
|
|
55
|
+
}
|
|
56
|
+
);
|
|
57
|
+
return {
|
|
58
|
+
onFilterKeywordChange,
|
|
59
|
+
isLoading,
|
|
60
|
+
normalizedOptions,
|
|
61
|
+
defaultOption,
|
|
62
|
+
fallbackOptions,
|
|
63
|
+
isFallbackOptionsLoading
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export { useComputedOptions };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
import { createContext, useContext } from 'react';
|
|
3
|
+
|
|
4
|
+
"use strict";
|
|
5
|
+
const DisabledContext = createContext(false);
|
|
6
|
+
function useContextDisabled() {
|
|
7
|
+
return useContext(DisabledContext);
|
|
8
|
+
}
|
|
9
|
+
const DisabledContextProvider = DisabledContext.Provider;
|
|
10
|
+
|
|
11
|
+
export { DisabledContextProvider, useContextDisabled };
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
import { useApiContext } from '@vef-framework/core';
|
|
3
|
+
import { isNullish, isFunction } from '@vef-framework/shared';
|
|
4
|
+
import { useUnmount } from 'ahooks';
|
|
5
|
+
import { useMemo } from 'react';
|
|
6
|
+
|
|
7
|
+
"use strict";
|
|
8
|
+
function useApi({
|
|
9
|
+
data,
|
|
10
|
+
dataDictionaryKey,
|
|
11
|
+
api,
|
|
12
|
+
apiParams,
|
|
13
|
+
apiEnabled,
|
|
14
|
+
staleTime,
|
|
15
|
+
keepPreviousData,
|
|
16
|
+
placeholderData
|
|
17
|
+
}, stubQueryApi, dataDictionaryApi) {
|
|
18
|
+
return useMemo(() => {
|
|
19
|
+
if (!isNullish(data)) {
|
|
20
|
+
return [
|
|
21
|
+
stubQueryApi,
|
|
22
|
+
data,
|
|
23
|
+
{
|
|
24
|
+
keepPreviousData: true,
|
|
25
|
+
initialData: data
|
|
26
|
+
}
|
|
27
|
+
];
|
|
28
|
+
}
|
|
29
|
+
if (!isNullish(dataDictionaryKey)) {
|
|
30
|
+
return [
|
|
31
|
+
dataDictionaryApi,
|
|
32
|
+
{
|
|
33
|
+
...apiParams,
|
|
34
|
+
key: dataDictionaryKey
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
enabled: apiEnabled ?? true,
|
|
38
|
+
staleTime,
|
|
39
|
+
keepPreviousData,
|
|
40
|
+
placeholderData
|
|
41
|
+
}
|
|
42
|
+
];
|
|
43
|
+
}
|
|
44
|
+
if (!isNullish(api)) {
|
|
45
|
+
return [
|
|
46
|
+
api,
|
|
47
|
+
apiParams,
|
|
48
|
+
{
|
|
49
|
+
enabled: apiEnabled ?? true,
|
|
50
|
+
staleTime,
|
|
51
|
+
keepPreviousData,
|
|
52
|
+
placeholderData
|
|
53
|
+
}
|
|
54
|
+
];
|
|
55
|
+
}
|
|
56
|
+
return [
|
|
57
|
+
stubQueryApi,
|
|
58
|
+
[],
|
|
59
|
+
{
|
|
60
|
+
keepPreviousData: true,
|
|
61
|
+
initialData: []
|
|
62
|
+
}
|
|
63
|
+
];
|
|
64
|
+
}, [
|
|
65
|
+
data,
|
|
66
|
+
dataDictionaryKey,
|
|
67
|
+
api,
|
|
68
|
+
stubQueryApi,
|
|
69
|
+
dataDictionaryApi,
|
|
70
|
+
apiParams,
|
|
71
|
+
apiEnabled,
|
|
72
|
+
staleTime,
|
|
73
|
+
keepPreviousData,
|
|
74
|
+
placeholderData
|
|
75
|
+
]);
|
|
76
|
+
}
|
|
77
|
+
function useDataQuery({ onFetched, ...config }) {
|
|
78
|
+
const {
|
|
79
|
+
useApiQuery,
|
|
80
|
+
stubQueryApi,
|
|
81
|
+
dataDictionaryApi: dataDictionaryQueryApi
|
|
82
|
+
} = useApiContext();
|
|
83
|
+
const [api, args, options] = useApi(config, stubQueryApi, dataDictionaryQueryApi);
|
|
84
|
+
const result = useApiQuery(api, args, options);
|
|
85
|
+
useUnmount(() => {
|
|
86
|
+
if (Object.is(api, stubQueryApi)) {
|
|
87
|
+
stubQueryApi.removeQueries(args);
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
const {
|
|
91
|
+
isSuccess,
|
|
92
|
+
data: rawData,
|
|
93
|
+
...rest
|
|
94
|
+
} = result;
|
|
95
|
+
const data = useMemo(() => {
|
|
96
|
+
if (isSuccess && isFunction(onFetched)) {
|
|
97
|
+
return onFetched(rawData);
|
|
98
|
+
}
|
|
99
|
+
return rawData;
|
|
100
|
+
}, [isSuccess, rawData, onFetched]);
|
|
101
|
+
return {
|
|
102
|
+
...rest,
|
|
103
|
+
isSuccess,
|
|
104
|
+
data
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export { useDataQuery };
|