@pisell/utils 3.0.4 → 3.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/es/_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js +27 -0
- package/es/_virtual/_@oxc-project_runtime@0.122.0/helpers/defineProperty.js +12 -0
- package/es/_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js +25 -0
- package/es/_virtual/_@oxc-project_runtime@0.122.0/helpers/toPrimitive.js +14 -0
- package/es/_virtual/_@oxc-project_runtime@0.122.0/helpers/toPropertyKey.js +9 -0
- package/es/_virtual/_@oxc-project_runtime@0.122.0/helpers/typeof.js +11 -0
- package/es/arrayUtils.d.ts +76 -1
- package/es/arrayUtils.js +103 -24
- package/es/common/stateHelper.d.ts +22 -0
- package/es/common/stateHelper.js +23 -0
- package/es/common/types.d.ts +31 -0
- package/es/componentStorage.d.ts +38 -0
- package/es/componentStorage.js +106 -0
- package/es/constants/index.js +7 -9
- package/es/date.d.ts +6 -3
- package/es/date.js +36 -53
- package/es/document.d.ts +6 -3
- package/es/document.js +43 -67
- package/es/escPosPrinter/EscPosPrinter.js +353 -490
- package/es/escPosPrinter/index.d.ts +9 -9
- package/es/escPosPrinter/index.js +37 -52
- package/es/format.d.ts +29 -0
- package/es/format.js +90 -112
- package/es/image.d.ts +13 -10
- package/es/image.js +48 -60
- package/es/index.d.ts +39 -18
- package/es/index.js +40 -29
- package/es/jsBridge/index.d.ts +14 -106
- package/es/jsBridge/index.js +57 -40
- package/es/jsBridge/regDeviceApi.js +87 -126
- package/es/jsBridge/types.d.ts +129 -0
- package/es/locales.d.ts +11 -11
- package/es/locales.js +79 -69
- package/es/log.d.ts +11 -6
- package/es/log.js +37 -59
- package/es/miniRedux.d.ts +20 -13
- package/es/miniRedux.js +42 -71
- package/es/number-input/hooks/useNumberInputState.d.ts +29 -0
- package/es/number-input/hooks/useNumberInputState.js +72 -0
- package/es/number-input/types.d.ts +96 -0
- package/es/number-input/utils/formatter.d.ts +56 -0
- package/es/number-input/utils/formatter.js +56 -0
- package/es/number-input/utils/validation.d.ts +32 -0
- package/es/number-input/utils/validation.js +52 -0
- package/es/otherUtils.d.ts +12 -9
- package/es/otherUtils.js +71 -120
- package/es/platform.d.ts +12 -9
- package/es/platform.js +61 -85
- package/es/select/hooks/useDebouncedSearch.d.ts +23 -0
- package/es/select/hooks/useDebouncedSearch.js +41 -0
- package/es/select/hooks/useSelectState.d.ts +19 -0
- package/es/select/hooks/useSelectState.js +60 -0
- package/es/select/index.d.ts +7 -0
- package/es/select/types.d.ts +256 -0
- package/es/select/utils/filterOptions.d.ts +23 -0
- package/es/select/utils/filterOptions.js +29 -0
- package/es/select/utils/helpers.d.ts +43 -0
- package/es/select/utils/helpers.js +51 -0
- package/es/select/utils/index.d.ts +4 -0
- package/es/select/utils/sortOptions.d.ts +13 -0
- package/es/select/utils/sortOptions.js +20 -0
- package/es/select/utils/tagAggregation.d.ts +25 -0
- package/es/select/utils/tagAggregation.js +35 -0
- package/es/text-input/hooks/useTextInputState.d.ts +28 -0
- package/es/text-input/hooks/useTextInputState.js +70 -0
- package/es/text-input/types.d.ts +56 -0
- package/es/text-input/utils/validation.d.ts +28 -0
- package/es/text-input/utils/validation.js +51 -0
- package/es/typeUtils.d.ts +11 -8
- package/es/typeUtils.js +61 -84
- package/es/walletValidity.d.ts +57 -0
- package/es/walletValidity.js +404 -460
- package/es/weighing.d.ts +65 -0
- package/es/weighing.js +96 -0
- package/lib/_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js +27 -0
- package/lib/_virtual/_@oxc-project_runtime@0.122.0/helpers/defineProperty.js +12 -0
- package/lib/_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js +25 -0
- package/lib/_virtual/_@oxc-project_runtime@0.122.0/helpers/toPrimitive.js +14 -0
- package/lib/_virtual/_@oxc-project_runtime@0.122.0/helpers/toPropertyKey.js +9 -0
- package/lib/_virtual/_@oxc-project_runtime@0.122.0/helpers/typeof.js +16 -0
- package/lib/_virtual/_rolldown/runtime.js +23 -0
- package/lib/arrayUtils.d.ts +76 -1
- package/lib/arrayUtils.js +102 -37
- package/lib/common/stateHelper.d.ts +22 -0
- package/lib/common/stateHelper.js +23 -0
- package/lib/common/types.d.ts +31 -0
- package/lib/componentStorage.d.ts +38 -0
- package/lib/componentStorage.js +114 -0
- package/lib/constants/index.js +7 -36
- package/lib/date.d.ts +6 -3
- package/lib/date.js +43 -65
- package/lib/document.d.ts +6 -3
- package/lib/document.js +46 -62
- package/lib/escPosPrinter/EscPosPrinter.js +327 -403
- package/lib/escPosPrinter/index.d.ts +9 -9
- package/lib/escPosPrinter/index.js +38 -84
- package/lib/format.d.ts +29 -0
- package/lib/format.js +91 -93
- package/lib/image.d.ts +13 -10
- package/lib/image.js +48 -74
- package/lib/index.d.ts +39 -18
- package/lib/index.js +137 -68
- package/lib/jsBridge/index.d.ts +14 -106
- package/lib/jsBridge/index.js +60 -60
- package/lib/jsBridge/regDeviceApi.js +86 -133
- package/lib/jsBridge/types.d.ts +129 -0
- package/lib/locales.d.ts +11 -11
- package/lib/locales.js +79 -63
- package/lib/log.d.ts +11 -6
- package/lib/log.js +37 -61
- package/lib/miniRedux.d.ts +20 -13
- package/lib/miniRedux.js +43 -91
- package/lib/number-input/hooks/useNumberInputState.d.ts +29 -0
- package/lib/number-input/hooks/useNumberInputState.js +73 -0
- package/lib/number-input/types.d.ts +96 -0
- package/lib/number-input/utils/formatter.d.ts +56 -0
- package/lib/number-input/utils/formatter.js +58 -0
- package/lib/number-input/utils/validation.d.ts +32 -0
- package/lib/number-input/utils/validation.js +52 -0
- package/lib/otherUtils.d.ts +12 -9
- package/lib/otherUtils.js +86 -91
- package/lib/platform.d.ts +12 -9
- package/lib/platform.js +68 -123
- package/lib/select/hooks/useDebouncedSearch.d.ts +23 -0
- package/lib/select/hooks/useDebouncedSearch.js +42 -0
- package/lib/select/hooks/useSelectState.d.ts +19 -0
- package/lib/select/hooks/useSelectState.js +62 -0
- package/lib/select/index.d.ts +7 -0
- package/lib/select/types.d.ts +256 -0
- package/lib/select/utils/filterOptions.d.ts +23 -0
- package/lib/select/utils/filterOptions.js +30 -0
- package/lib/select/utils/helpers.d.ts +43 -0
- package/lib/select/utils/helpers.js +55 -0
- package/lib/select/utils/index.d.ts +4 -0
- package/lib/select/utils/sortOptions.d.ts +13 -0
- package/lib/select/utils/sortOptions.js +20 -0
- package/lib/select/utils/tagAggregation.d.ts +25 -0
- package/lib/select/utils/tagAggregation.js +35 -0
- package/lib/text-input/hooks/useTextInputState.d.ts +28 -0
- package/lib/text-input/hooks/useTextInputState.js +71 -0
- package/lib/text-input/types.d.ts +56 -0
- package/lib/text-input/utils/validation.d.ts +28 -0
- package/lib/text-input/utils/validation.js +51 -0
- package/lib/typeUtils.d.ts +11 -8
- package/lib/typeUtils.js +68 -73
- package/lib/walletValidity.d.ts +57 -0
- package/lib/walletValidity.js +420 -323
- package/lib/weighing.d.ts +65 -0
- package/lib/weighing.js +114 -0
- package/package.json +1 -1
- package/es/constants/index.d.ts +0 -7
- package/es/firebase/config/index.d.ts +0 -13
- package/es/firebase/config/index.js +0 -54
- package/es/firebase/index.d.ts +0 -28
- package/es/firebase/index.js +0 -232
- package/lib/constants/index.d.ts +0 -7
- package/lib/firebase/config/index.d.ts +0 -13
- package/lib/firebase/config/index.js +0 -79
- package/lib/firebase/index.d.ts +0 -28
- package/lib/firebase/index.js +0 -180
package/es/platform.js
CHANGED
|
@@ -1,93 +1,69 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
return true;
|
|
1
|
+
//#region src/platform.ts
|
|
2
|
+
const isBrowser = typeof window != "undefined" && window.document;
|
|
3
|
+
const isIpad = () => {
|
|
4
|
+
var ua = navigator.userAgent;
|
|
5
|
+
var isSafari = ua.indexOf("Safari") != -1 && ua.indexOf("Version") != -1;
|
|
6
|
+
var isIphone = ua.indexOf("iPhone") != -1 && ua.indexOf("Version") != -1;
|
|
7
|
+
var isIPad = isSafari && !isIphone && "ontouchend" in document;
|
|
8
|
+
if (!/iphone|ios|ipad|android|mobile/i.test(navigator.userAgent.toLowerCase()) && !isIPad) return false;
|
|
9
|
+
return true;
|
|
11
10
|
};
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
} else {
|
|
25
|
-
return false;
|
|
26
|
-
}
|
|
11
|
+
const isMobile = () => {
|
|
12
|
+
let sUserAgent = window.navigator.userAgent.toLowerCase();
|
|
13
|
+
let bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
|
|
14
|
+
let bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
|
|
15
|
+
let bIsMidp = sUserAgent.match(/midp/i) == "midp";
|
|
16
|
+
let bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
|
|
17
|
+
let bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
|
|
18
|
+
let bIsAndroid = sUserAgent.match(/android/i) == "android";
|
|
19
|
+
let bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
|
|
20
|
+
let bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
|
|
21
|
+
if (bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM || isIpad()) return true;
|
|
22
|
+
else return false;
|
|
27
23
|
};
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
export var isAndroid = function isAndroid() {
|
|
31
|
-
return navigator.userAgent.toLowerCase().indexOf("android") !== -1;
|
|
24
|
+
const isAndroid = () => {
|
|
25
|
+
return navigator.userAgent.toLowerCase().indexOf("android") !== -1;
|
|
32
26
|
};
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
export var isIos = function isIos() {
|
|
36
|
-
return /iphone|ipad|ipod/.test(navigator.userAgent.toLowerCase());
|
|
27
|
+
const isIos = () => {
|
|
28
|
+
return /iphone|ipad|ipod/.test(navigator.userAgent.toLowerCase());
|
|
37
29
|
};
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
env = 0;
|
|
52
|
-
} else {
|
|
53
|
-
env = 1;
|
|
54
|
-
}
|
|
55
|
-
return env;
|
|
30
|
+
const checkEnv = () => {
|
|
31
|
+
let env = 0;
|
|
32
|
+
let userAgent = window.navigator.userAgent;
|
|
33
|
+
let isOpera = userAgent.indexOf("Opera") > -1 || userAgent.indexOf("OPR") > -1;
|
|
34
|
+
let isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1;
|
|
35
|
+
let isIE11 = userAgent.indexOf("compatible") === -1 && userAgent.indexOf("Trident") > -1;
|
|
36
|
+
let isEdge = userAgent.indexOf("Edge") > -1;
|
|
37
|
+
let isFF = userAgent.indexOf("Firefox") > -1;
|
|
38
|
+
let isSafari = userAgent.indexOf("Safari") > -1 && userAgent.indexOf("Chrome") === -1;
|
|
39
|
+
let isChrome = userAgent.indexOf("Chrome") > -1 && userAgent.indexOf("; Win") > -1 && userAgent.indexOf("Safari") > -1;
|
|
40
|
+
if (isOpera || isIE || isIE11 || isEdge || isFF || isSafari || isChrome) env = 0;
|
|
41
|
+
else env = 1;
|
|
42
|
+
return env;
|
|
56
43
|
};
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
return true;
|
|
66
|
-
}
|
|
67
|
-
return false;
|
|
68
|
-
}
|
|
69
|
-
return false;
|
|
44
|
+
const isWx = () => {
|
|
45
|
+
if (isBrowser) {
|
|
46
|
+
let ua = "";
|
|
47
|
+
if (isBrowser) ua = window.navigator.userAgent.toLowerCase();
|
|
48
|
+
if (ua.match(/MicroMessenger/i) == "micromessenger") return true;
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
return false;
|
|
70
52
|
};
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
|
|
75
|
-
return isiOS;
|
|
76
|
-
}
|
|
77
|
-
return false;
|
|
53
|
+
const isIOS = () => {
|
|
54
|
+
if (isBrowser) return !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
|
|
55
|
+
return false;
|
|
78
56
|
};
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
var isWxapp = u.indexOf('miniProgram') !== -1;
|
|
83
|
-
return isWxapp;
|
|
84
|
-
}
|
|
85
|
-
return false;
|
|
57
|
+
const isWxApp = () => {
|
|
58
|
+
if (isBrowser) return navigator.userAgent.indexOf("miniProgram") !== -1;
|
|
59
|
+
return false;
|
|
86
60
|
};
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
};
|
|
61
|
+
const isSafari = () => {
|
|
62
|
+
if (isBrowser) {
|
|
63
|
+
let userAgent = window.navigator.userAgent;
|
|
64
|
+
return userAgent.indexOf("Safari") > -1 && userAgent.indexOf("Chrome") === -1 && userAgent.indexOf("CriOS") === -1 && userAgent.indexOf("ChannelName/Feishu") === -1;
|
|
65
|
+
}
|
|
66
|
+
return false;
|
|
67
|
+
};
|
|
68
|
+
//#endregion
|
|
69
|
+
export { checkEnv, isAndroid, isIOS, isIos, isIpad, isMobile, isSafari, isWx, isWxApp };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
//#region src/select/hooks/useDebouncedSearch.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* 防抖搜索 Hook
|
|
4
|
+
* 对用户输入进行防抖处理,避免频繁触发搜索
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* 防抖搜索 Hook
|
|
8
|
+
* @template T 值的类型
|
|
9
|
+
* @param initialValue 初始值
|
|
10
|
+
* @param delay 防抖延迟时间(毫秒)
|
|
11
|
+
* @param callback 防抖后的回调函数
|
|
12
|
+
* @returns [防抖后的值, 设置值函数]
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```tsx
|
|
16
|
+
* const [debouncedSearch, setSearchInput] = useDebouncedSearch('', 300, (text) => {
|
|
17
|
+
* console.log('搜索关键词:', text);
|
|
18
|
+
* });
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
declare function useDebouncedSearch<T = string>(initialValue: T, delay: number, callback?: (value: T) => void): [T, (value: T) => void];
|
|
22
|
+
//#endregion
|
|
23
|
+
export { useDebouncedSearch };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { useEffect, useState } from "react";
|
|
2
|
+
//#region src/select/hooks/useDebouncedSearch.ts
|
|
3
|
+
/**
|
|
4
|
+
* 防抖搜索 Hook
|
|
5
|
+
* 对用户输入进行防抖处理,避免频繁触发搜索
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* 防抖搜索 Hook
|
|
9
|
+
* @template T 值的类型
|
|
10
|
+
* @param initialValue 初始值
|
|
11
|
+
* @param delay 防抖延迟时间(毫秒)
|
|
12
|
+
* @param callback 防抖后的回调函数
|
|
13
|
+
* @returns [防抖后的值, 设置值函数]
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```tsx
|
|
17
|
+
* const [debouncedSearch, setSearchInput] = useDebouncedSearch('', 300, (text) => {
|
|
18
|
+
* console.log('搜索关键词:', text);
|
|
19
|
+
* });
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
function useDebouncedSearch(initialValue, delay, callback) {
|
|
23
|
+
const [value, setValue] = useState(initialValue);
|
|
24
|
+
const [debouncedValue, setDebouncedValue] = useState(initialValue);
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
const timer = setTimeout(() => {
|
|
27
|
+
setDebouncedValue(value);
|
|
28
|
+
callback === null || callback === void 0 || callback(value);
|
|
29
|
+
}, delay);
|
|
30
|
+
return () => {
|
|
31
|
+
clearTimeout(timer);
|
|
32
|
+
};
|
|
33
|
+
}, [
|
|
34
|
+
value,
|
|
35
|
+
delay,
|
|
36
|
+
callback
|
|
37
|
+
]);
|
|
38
|
+
return [debouncedValue, setValue];
|
|
39
|
+
}
|
|
40
|
+
//#endregion
|
|
41
|
+
export { useDebouncedSearch };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { MultipleSelectState, SelectState, UseMultipleSelectStateOptions, UseSelectStateOptions } from "../types.js";
|
|
2
|
+
|
|
3
|
+
//#region src/select/hooks/useSelectState.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* 单选状态管理 Hook
|
|
6
|
+
* @template T 值的类型
|
|
7
|
+
* @param options Hook 配置选项
|
|
8
|
+
* @returns 状态管理对象
|
|
9
|
+
*/
|
|
10
|
+
declare function useSelectState<T = string | number>(options: UseSelectStateOptions<T>): SelectState<T>;
|
|
11
|
+
/**
|
|
12
|
+
* 多选状态管理 Hook
|
|
13
|
+
* @template T 值的类型
|
|
14
|
+
* @param options Hook 配置选项
|
|
15
|
+
* @returns 状态管理对象
|
|
16
|
+
*/
|
|
17
|
+
declare function useMultipleSelectState<T = string | number>(options: UseMultipleSelectStateOptions<T>): MultipleSelectState<T>;
|
|
18
|
+
//#endregion
|
|
19
|
+
export { useMultipleSelectState, useSelectState };
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { useCallback, useState } from "react";
|
|
2
|
+
//#region src/select/hooks/useSelectState.ts
|
|
3
|
+
/**
|
|
4
|
+
* Select 状态管理 Hook
|
|
5
|
+
* 处理受控/非受控模式,适用于单选和多选
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* 单选状态管理 Hook
|
|
9
|
+
* @template T 值的类型
|
|
10
|
+
* @param options Hook 配置选项
|
|
11
|
+
* @returns 状态管理对象
|
|
12
|
+
*/
|
|
13
|
+
function useSelectState(options) {
|
|
14
|
+
const { value: controlledValue, defaultValue, onChange } = options;
|
|
15
|
+
const isControlled = controlledValue !== void 0;
|
|
16
|
+
const [internalValue, setInternalValue] = useState(defaultValue !== null && defaultValue !== void 0 ? defaultValue : null);
|
|
17
|
+
const value = isControlled ? controlledValue : internalValue;
|
|
18
|
+
const setValue = useCallback((newValue) => {
|
|
19
|
+
if (!isControlled) setInternalValue(newValue);
|
|
20
|
+
onChange === null || onChange === void 0 || onChange(newValue, null);
|
|
21
|
+
}, [isControlled, onChange]);
|
|
22
|
+
return {
|
|
23
|
+
value,
|
|
24
|
+
setValue,
|
|
25
|
+
clearValue: useCallback(() => {
|
|
26
|
+
setValue(null);
|
|
27
|
+
}, [setValue])
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* 多选状态管理 Hook
|
|
32
|
+
* @template T 值的类型
|
|
33
|
+
* @param options Hook 配置选项
|
|
34
|
+
* @returns 状态管理对象
|
|
35
|
+
*/
|
|
36
|
+
function useMultipleSelectState(options) {
|
|
37
|
+
const { value: controlledValue, defaultValue, onChange } = options;
|
|
38
|
+
const isControlled = controlledValue !== void 0;
|
|
39
|
+
const [internalValue, setInternalValue] = useState(defaultValue !== null && defaultValue !== void 0 ? defaultValue : []);
|
|
40
|
+
const value = isControlled ? controlledValue !== null && controlledValue !== void 0 ? controlledValue : [] : internalValue;
|
|
41
|
+
const setValue = useCallback((newValue) => {
|
|
42
|
+
if (!isControlled) setInternalValue(newValue);
|
|
43
|
+
onChange === null || onChange === void 0 || onChange(newValue, []);
|
|
44
|
+
}, [isControlled, onChange]);
|
|
45
|
+
return {
|
|
46
|
+
value,
|
|
47
|
+
setValue,
|
|
48
|
+
addValue: useCallback((val) => {
|
|
49
|
+
setValue([...value, val]);
|
|
50
|
+
}, [value, setValue]),
|
|
51
|
+
removeValue: useCallback((val) => {
|
|
52
|
+
setValue(value.filter((v) => v !== val));
|
|
53
|
+
}, [value, setValue]),
|
|
54
|
+
clearValue: useCallback(() => {
|
|
55
|
+
setValue([]);
|
|
56
|
+
}, [setValue])
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
//#endregion
|
|
60
|
+
export { useMultipleSelectState, useSelectState };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { BaseSelectProps, FilterOptionFunc, MultipleSelectState, SelectOption, SelectState, SortDirection, TagDisplayInfo, UseMultipleSelectStateOptions, UseSelectStateOptions } from "./types.js";
|
|
2
|
+
import { useMultipleSelectState, useSelectState } from "./hooks/useSelectState.js";
|
|
3
|
+
import { useDebouncedSearch } from "./hooks/useDebouncedSearch.js";
|
|
4
|
+
import { calculateTagDisplay } from "./utils/tagAggregation.js";
|
|
5
|
+
import { defaultFilterOption, filterOptions } from "./utils/filterOptions.js";
|
|
6
|
+
import { sortOptions } from "./utils/sortOptions.js";
|
|
7
|
+
import { findOptionByValue, findOptionsByValues, generateId, isOptionSelected, isOptionSelectedInMultiple } from "./utils/helpers.js";
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import { DisplayState } from "../common/types.js";
|
|
2
|
+
import React from "react";
|
|
3
|
+
|
|
4
|
+
//#region src/select/types.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* 选项基础类型
|
|
7
|
+
* @template T 选项值的类型,默认为 string | number
|
|
8
|
+
*/
|
|
9
|
+
interface SelectOption<T = string | number> {
|
|
10
|
+
/**
|
|
11
|
+
* 选项值(唯一标识)
|
|
12
|
+
*/
|
|
13
|
+
value: T;
|
|
14
|
+
/**
|
|
15
|
+
* 选项显示文本
|
|
16
|
+
*/
|
|
17
|
+
label: string;
|
|
18
|
+
/**
|
|
19
|
+
* 是否禁用该选项
|
|
20
|
+
* @default false
|
|
21
|
+
*/
|
|
22
|
+
disabled?: boolean;
|
|
23
|
+
/**
|
|
24
|
+
* 选项图标(可选)
|
|
25
|
+
*/
|
|
26
|
+
icon?: React.ReactNode;
|
|
27
|
+
/**
|
|
28
|
+
* 标签背景色(多选时用于自定义标签颜色)
|
|
29
|
+
* @example '#61dafb'
|
|
30
|
+
*/
|
|
31
|
+
color?: string;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* 选项过滤函数类型
|
|
35
|
+
* @template T 选项值的类型
|
|
36
|
+
* @param input 用户输入的搜索文本
|
|
37
|
+
* @param option 当前选项对象
|
|
38
|
+
* @returns 是否保留该选项
|
|
39
|
+
*/
|
|
40
|
+
type FilterOptionFunc<T = string | number> = (input: string, option: SelectOption<T>) => boolean;
|
|
41
|
+
/**
|
|
42
|
+
* 排序方向
|
|
43
|
+
*/
|
|
44
|
+
type SortDirection = 'asc' | 'desc';
|
|
45
|
+
/**
|
|
46
|
+
* Select 状态 Hook 返回值(单选)
|
|
47
|
+
* @template T 值的类型
|
|
48
|
+
*/
|
|
49
|
+
interface SelectState<T = string | number> {
|
|
50
|
+
/**
|
|
51
|
+
* 当前选中值
|
|
52
|
+
*/
|
|
53
|
+
value: T | null | undefined;
|
|
54
|
+
/**
|
|
55
|
+
* 设置选中值
|
|
56
|
+
*/
|
|
57
|
+
setValue: (value: T | null) => void;
|
|
58
|
+
/**
|
|
59
|
+
* 清空选中值
|
|
60
|
+
*/
|
|
61
|
+
clearValue: () => void;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Multiple Select 状态 Hook 返回值(多选)
|
|
65
|
+
* @template T 值的类型
|
|
66
|
+
*/
|
|
67
|
+
interface MultipleSelectState<T = string | number> {
|
|
68
|
+
/**
|
|
69
|
+
* 当前选中值数组
|
|
70
|
+
*/
|
|
71
|
+
value: T[];
|
|
72
|
+
/**
|
|
73
|
+
* 设置选中值数组
|
|
74
|
+
*/
|
|
75
|
+
setValue: (value: T[]) => void;
|
|
76
|
+
/**
|
|
77
|
+
* 添加一个选中值
|
|
78
|
+
*/
|
|
79
|
+
addValue: (value: T) => void;
|
|
80
|
+
/**
|
|
81
|
+
* 移除一个选中值
|
|
82
|
+
*/
|
|
83
|
+
removeValue: (value: T) => void;
|
|
84
|
+
/**
|
|
85
|
+
* 清空所有选中值
|
|
86
|
+
*/
|
|
87
|
+
clearValue: () => void;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* 标签显示信息(多选专用)
|
|
91
|
+
* @template T 选项值的类型
|
|
92
|
+
*/
|
|
93
|
+
interface TagDisplayInfo<T = string | number> {
|
|
94
|
+
/**
|
|
95
|
+
* 可见的标签列表
|
|
96
|
+
*/
|
|
97
|
+
visibleTags: SelectOption<T>[];
|
|
98
|
+
/**
|
|
99
|
+
* 隐藏的标签列表
|
|
100
|
+
*/
|
|
101
|
+
hiddenTags: SelectOption<T>[];
|
|
102
|
+
/**
|
|
103
|
+
* 隐藏的标签数量
|
|
104
|
+
*/
|
|
105
|
+
hiddenCount: number;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* useSelectState Hook 的配置选项(单选)
|
|
109
|
+
* @template T 值的类型
|
|
110
|
+
*/
|
|
111
|
+
interface UseSelectStateOptions<T = string | number> {
|
|
112
|
+
/**
|
|
113
|
+
* 受控模式:当前值
|
|
114
|
+
*/
|
|
115
|
+
value?: T | null;
|
|
116
|
+
/**
|
|
117
|
+
* 非受控模式:默认值
|
|
118
|
+
*/
|
|
119
|
+
defaultValue?: T;
|
|
120
|
+
/**
|
|
121
|
+
* 值变化回调
|
|
122
|
+
*/
|
|
123
|
+
onChange?: (value: T | null, option: SelectOption<T> | null) => void;
|
|
124
|
+
/**
|
|
125
|
+
* 模式(用于类型区分)
|
|
126
|
+
*/
|
|
127
|
+
mode: 'single';
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* useMultipleSelectState Hook 的配置选项(多选)
|
|
131
|
+
* @template T 值的类型
|
|
132
|
+
*/
|
|
133
|
+
interface UseMultipleSelectStateOptions<T = string | number> {
|
|
134
|
+
/**
|
|
135
|
+
* 受控模式:当前值数组
|
|
136
|
+
*/
|
|
137
|
+
value?: T[];
|
|
138
|
+
/**
|
|
139
|
+
* 非受控模式:默认值数组
|
|
140
|
+
*/
|
|
141
|
+
defaultValue?: T[];
|
|
142
|
+
/**
|
|
143
|
+
* 值变化回调
|
|
144
|
+
*/
|
|
145
|
+
onChange?: (value: T[], options: SelectOption<T>[]) => void;
|
|
146
|
+
/**
|
|
147
|
+
* 模式(用于类型区分)
|
|
148
|
+
*/
|
|
149
|
+
mode: 'multiple';
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* 基础 Select Props(公共属性)
|
|
153
|
+
* @template T 值的类型
|
|
154
|
+
*/
|
|
155
|
+
interface BaseSelectProps<T = string | number> {
|
|
156
|
+
/**
|
|
157
|
+
* 选项列表(必填)
|
|
158
|
+
*/
|
|
159
|
+
options: SelectOption<T>[];
|
|
160
|
+
/**
|
|
161
|
+
* 占位符文本
|
|
162
|
+
* @default '请选择'
|
|
163
|
+
*/
|
|
164
|
+
placeholder?: string;
|
|
165
|
+
/**
|
|
166
|
+
* 是否显示搜索框
|
|
167
|
+
* @default false
|
|
168
|
+
*/
|
|
169
|
+
showSearch?: boolean;
|
|
170
|
+
/**
|
|
171
|
+
* 是否显示清空按钮
|
|
172
|
+
* @default false
|
|
173
|
+
*/
|
|
174
|
+
allowClear?: boolean;
|
|
175
|
+
/**
|
|
176
|
+
* 是否禁用
|
|
177
|
+
* @default false
|
|
178
|
+
*/
|
|
179
|
+
disabled?: boolean;
|
|
180
|
+
/**
|
|
181
|
+
* 是否只读
|
|
182
|
+
* @default false
|
|
183
|
+
*/
|
|
184
|
+
readOnly?: boolean;
|
|
185
|
+
/**
|
|
186
|
+
* 尺寸
|
|
187
|
+
* @default 'middle'
|
|
188
|
+
*/
|
|
189
|
+
size?: 'small' | 'middle' | 'large';
|
|
190
|
+
/**
|
|
191
|
+
* 自定义类名
|
|
192
|
+
*/
|
|
193
|
+
className?: string;
|
|
194
|
+
/**
|
|
195
|
+
* 自定义样式
|
|
196
|
+
*/
|
|
197
|
+
style?: React.CSSProperties;
|
|
198
|
+
/**
|
|
199
|
+
* 标签文本
|
|
200
|
+
*/
|
|
201
|
+
label?: string;
|
|
202
|
+
/**
|
|
203
|
+
* 辅助提示文本
|
|
204
|
+
*/
|
|
205
|
+
helperText?: string;
|
|
206
|
+
/**
|
|
207
|
+
* 是否必填
|
|
208
|
+
* @default false
|
|
209
|
+
*/
|
|
210
|
+
required?: boolean;
|
|
211
|
+
/**
|
|
212
|
+
* 是否显示错误状态
|
|
213
|
+
* @default false
|
|
214
|
+
*/
|
|
215
|
+
error?: boolean;
|
|
216
|
+
/**
|
|
217
|
+
* 错误提示文本
|
|
218
|
+
*/
|
|
219
|
+
errorText?: string;
|
|
220
|
+
/**
|
|
221
|
+
* 是否启用过滤
|
|
222
|
+
* @default false
|
|
223
|
+
*/
|
|
224
|
+
filterable?: boolean;
|
|
225
|
+
/**
|
|
226
|
+
* 自定义过滤函数
|
|
227
|
+
*/
|
|
228
|
+
filterOption?: FilterOptionFunc<T>;
|
|
229
|
+
/**
|
|
230
|
+
* 是否启用排序
|
|
231
|
+
* @default false
|
|
232
|
+
*/
|
|
233
|
+
sortable?: boolean;
|
|
234
|
+
/**
|
|
235
|
+
* 排序方向
|
|
236
|
+
*/
|
|
237
|
+
sortDirection?: SortDirection;
|
|
238
|
+
/**
|
|
239
|
+
* 搜索输入回调
|
|
240
|
+
*/
|
|
241
|
+
onSearch?: (value: string) => void;
|
|
242
|
+
/**
|
|
243
|
+
* 获得焦点回调
|
|
244
|
+
*/
|
|
245
|
+
onFocus?: (event: React.FocusEvent) => void;
|
|
246
|
+
/**
|
|
247
|
+
* 失去焦点回调
|
|
248
|
+
*/
|
|
249
|
+
onBlur?: (event: React.FocusEvent) => void;
|
|
250
|
+
/**
|
|
251
|
+
* 下拉框显隐回调
|
|
252
|
+
*/
|
|
253
|
+
onDropdownVisibleChange?: (visible: boolean) => void;
|
|
254
|
+
}
|
|
255
|
+
//#endregion
|
|
256
|
+
export { BaseSelectProps, FilterOptionFunc, MultipleSelectState, SelectOption, SelectState, SortDirection, TagDisplayInfo, UseMultipleSelectStateOptions, UseSelectStateOptions };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { FilterOptionFunc, SelectOption } from "../types.js";
|
|
2
|
+
|
|
3
|
+
//#region src/select/utils/filterOptions.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* 默认的选项过滤函数
|
|
6
|
+
* 根据 label 进行模糊匹配(不区分大小写)
|
|
7
|
+
* @template T 选项值的类型
|
|
8
|
+
* @param input 搜索输入文本
|
|
9
|
+
* @param option 选项对象
|
|
10
|
+
* @returns 是否保留该选项
|
|
11
|
+
*/
|
|
12
|
+
declare function defaultFilterOption<T = string | number>(input: string, option: SelectOption<T>): boolean;
|
|
13
|
+
/**
|
|
14
|
+
* 过滤选项列表
|
|
15
|
+
* @template T 选项值的类型
|
|
16
|
+
* @param options 原始选项列表
|
|
17
|
+
* @param searchText 搜索文本
|
|
18
|
+
* @param filterFunc 自定义过滤函数(可选)
|
|
19
|
+
* @returns 过滤后的选项列表
|
|
20
|
+
*/
|
|
21
|
+
declare function filterOptions<T = string | number>(options: SelectOption<T>[], searchText: string, filterFunc?: FilterOptionFunc<T>): SelectOption<T>[];
|
|
22
|
+
//#endregion
|
|
23
|
+
export { defaultFilterOption, filterOptions };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
//#region src/select/utils/filterOptions.ts
|
|
2
|
+
/**
|
|
3
|
+
* 默认的选项过滤函数
|
|
4
|
+
* 根据 label 进行模糊匹配(不区分大小写)
|
|
5
|
+
* @template T 选项值的类型
|
|
6
|
+
* @param input 搜索输入文本
|
|
7
|
+
* @param option 选项对象
|
|
8
|
+
* @returns 是否保留该选项
|
|
9
|
+
*/
|
|
10
|
+
function defaultFilterOption(input, option) {
|
|
11
|
+
if (!input) return true;
|
|
12
|
+
const searchText = input.toLowerCase();
|
|
13
|
+
return option.label.toLowerCase().includes(searchText);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* 过滤选项列表
|
|
17
|
+
* @template T 选项值的类型
|
|
18
|
+
* @param options 原始选项列表
|
|
19
|
+
* @param searchText 搜索文本
|
|
20
|
+
* @param filterFunc 自定义过滤函数(可选)
|
|
21
|
+
* @returns 过滤后的选项列表
|
|
22
|
+
*/
|
|
23
|
+
function filterOptions(options, searchText, filterFunc) {
|
|
24
|
+
if (!searchText) return options;
|
|
25
|
+
const filterFn = filterFunc || defaultFilterOption;
|
|
26
|
+
return options.filter((opt) => filterFn(searchText, opt));
|
|
27
|
+
}
|
|
28
|
+
//#endregion
|
|
29
|
+
export { defaultFilterOption, filterOptions };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { SelectOption } from "../types.js";
|
|
2
|
+
|
|
3
|
+
//#region src/select/utils/helpers.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* 根据值查找选项对象
|
|
6
|
+
* @template T 选项值的类型
|
|
7
|
+
* @param value 选项值
|
|
8
|
+
* @param options 选项列表
|
|
9
|
+
* @returns 找到的选项对象,如果没找到返回 undefined
|
|
10
|
+
*/
|
|
11
|
+
declare function findOptionByValue<T = string | number>(value: T, options: SelectOption<T>[]): SelectOption<T> | undefined;
|
|
12
|
+
/**
|
|
13
|
+
* 根据值数组查找选项对象数组
|
|
14
|
+
* @template T 选项值的类型
|
|
15
|
+
* @param values 选项值数组
|
|
16
|
+
* @param options 选项列表
|
|
17
|
+
* @returns 找到的选项对象数组
|
|
18
|
+
*/
|
|
19
|
+
declare function findOptionsByValues<T = string | number>(values: T[], options: SelectOption<T>[]): SelectOption<T>[];
|
|
20
|
+
/**
|
|
21
|
+
* 判断选项是否被选中(单选)
|
|
22
|
+
* @template T 选项值的类型
|
|
23
|
+
* @param optionValue 选项值
|
|
24
|
+
* @param selectedValue 当前选中值
|
|
25
|
+
* @returns 是否选中
|
|
26
|
+
*/
|
|
27
|
+
declare function isOptionSelected<T = string | number>(optionValue: T, selectedValue: T | null | undefined): boolean;
|
|
28
|
+
/**
|
|
29
|
+
* 判断选项是否被选中(多选)
|
|
30
|
+
* @template T 选项值的类型
|
|
31
|
+
* @param optionValue 选项值
|
|
32
|
+
* @param selectedValues 当前选中值数组
|
|
33
|
+
* @returns 是否选中
|
|
34
|
+
*/
|
|
35
|
+
declare function isOptionSelectedInMultiple<T = string | number>(optionValue: T, selectedValues: T[]): boolean;
|
|
36
|
+
/**
|
|
37
|
+
* 生成唯一 ID
|
|
38
|
+
* @param prefix ID 前缀
|
|
39
|
+
* @returns 唯一 ID
|
|
40
|
+
*/
|
|
41
|
+
declare function generateId(prefix?: string): string;
|
|
42
|
+
//#endregion
|
|
43
|
+
export { findOptionByValue, findOptionsByValues, generateId, isOptionSelected, isOptionSelectedInMultiple };
|