@pisell/utils 3.0.6 → 3.0.7
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/arrayUtils.d.ts +23 -26
- package/es/arrayUtils.js +157 -97
- package/es/common/index.d.ts +5 -0
- package/es/common/index.js +5 -0
- package/es/common/stateHelper.d.ts +2 -6
- package/es/common/stateHelper.js +24 -22
- package/es/common/types.d.ts +10 -13
- package/es/common/types.js +1 -0
- package/es/componentStorage.d.ts +25 -27
- package/es/componentStorage.js +130 -94
- package/es/constants/index.js +9 -7
- package/es/date.js +53 -36
- package/es/document.js +67 -43
- package/es/escPosPrinter/EscPosPrinter.js +490 -353
- package/es/escPosPrinter/index.d.ts +9 -9
- package/es/escPosPrinter/index.js +52 -37
- package/es/firebase/config/index.js +54 -0
- package/es/firebase/index.js +232 -0
- package/es/format.d.ts +15 -12
- package/es/format.js +156 -89
- package/es/image.js +60 -48
- package/es/index.d.ts +24 -39
- package/es/index.js +35 -40
- package/es/jsBridge/index.d.ts +4 -8
- package/es/jsBridge/index.js +55 -56
- package/es/jsBridge/regDeviceApi.js +126 -87
- package/es/jsBridge/types.d.ts +115 -95
- package/es/jsBridge/types.js +1 -0
- package/es/locales.d.ts +8 -11
- package/es/locales.js +96 -77
- package/es/log.d.ts +6 -11
- package/es/log.js +59 -37
- package/es/miniRedux.d.ts +13 -20
- package/es/miniRedux.js +76 -42
- package/es/number-input/hooks/index.d.ts +4 -0
- package/es/number-input/hooks/index.js +5 -0
- package/es/number-input/hooks/useNumberInputState.d.ts +2 -6
- package/es/number-input/hooks/useNumberInputState.js +128 -71
- package/es/number-input/index.d.ts +14 -0
- package/es/number-input/index.js +21 -0
- package/es/number-input/types.d.ts +88 -88
- package/es/number-input/types.js +1 -0
- package/es/number-input/utils/formatter.d.ts +10 -13
- package/es/number-input/utils/formatter.js +77 -51
- package/es/number-input/utils/index.d.ts +7 -0
- package/es/number-input/utils/index.js +8 -0
- package/es/number-input/utils/validation.d.ts +2 -7
- package/es/number-input/utils/validation.js +66 -51
- package/es/otherUtils.d.ts +9 -12
- package/es/otherUtils.js +129 -71
- package/es/platform.d.ts +9 -12
- package/es/platform.js +85 -61
- package/es/select/hooks/index.d.ts +5 -0
- package/es/select/hooks/index.js +6 -0
- package/es/select/hooks/useDebouncedSearch.d.ts +1 -4
- package/es/select/hooks/useDebouncedSearch.js +53 -39
- package/es/select/hooks/useSelectState.d.ts +7 -7
- package/es/select/hooks/useSelectState.js +113 -56
- package/es/select/index.d.ts +8 -7
- package/es/select/index.js +13 -0
- package/es/select/types.d.ts +211 -211
- package/es/select/types.js +1 -0
- package/es/select/utils/filterOptions.d.ts +6 -7
- package/es/select/utils/filterOptions.js +31 -26
- package/es/select/utils/helpers.d.ts +9 -10
- package/es/select/utils/helpers.js +52 -42
- package/es/select/utils/index.d.ts +7 -4
- package/es/select/utils/index.js +8 -0
- package/es/select/utils/sortOptions.d.ts +5 -6
- package/es/select/utils/sortOptions.js +31 -19
- package/es/select/utils/tagAggregation.d.ts +6 -6
- package/es/select/utils/tagAggregation.js +47 -34
- package/es/text-input/hooks/index.d.ts +4 -0
- package/es/text-input/hooks/index.js +5 -0
- package/es/text-input/hooks/useTextInputState.d.ts +2 -6
- package/es/text-input/hooks/useTextInputState.js +126 -69
- package/es/text-input/index.d.ts +14 -0
- package/es/text-input/index.js +20 -0
- package/es/text-input/types.d.ts +48 -48
- package/es/text-input/types.js +1 -0
- package/es/text-input/utils/index.d.ts +5 -0
- package/es/text-input/utils/index.js +7 -0
- package/es/text-input/utils/validation.d.ts +2 -7
- package/es/text-input/utils/validation.js +70 -50
- package/es/typeUtils.js +84 -61
- package/es/walletValidity.d.ts +42 -44
- package/es/walletValidity.js +557 -396
- package/es/weighing.d.ts +45 -48
- package/es/weighing.js +106 -83
- package/lib/arrayUtils.d.ts +23 -26
- package/lib/arrayUtils.js +111 -101
- package/lib/common/index.d.ts +5 -0
- package/lib/common/index.js +29 -0
- package/lib/common/stateHelper.d.ts +2 -6
- package/lib/common/stateHelper.js +32 -21
- package/lib/common/types.d.ts +10 -13
- package/lib/common/types.js +17 -0
- package/lib/componentStorage.d.ts +25 -27
- package/lib/componentStorage.js +126 -96
- package/lib/constants/index.js +36 -7
- package/lib/date.js +65 -43
- package/lib/document.js +62 -46
- package/lib/escPosPrinter/EscPosPrinter.js +403 -327
- package/lib/escPosPrinter/index.d.ts +9 -9
- package/lib/escPosPrinter/index.js +84 -38
- package/lib/firebase/config/index.js +79 -0
- package/lib/firebase/index.js +180 -0
- package/lib/format.d.ts +15 -12
- package/lib/format.js +133 -91
- package/lib/image.js +74 -48
- package/lib/index.d.ts +24 -39
- package/lib/index.js +80 -137
- package/lib/jsBridge/index.d.ts +4 -8
- package/lib/jsBridge/index.js +76 -57
- package/lib/jsBridge/regDeviceApi.js +133 -86
- package/lib/jsBridge/types.d.ts +115 -95
- package/lib/jsBridge/types.js +17 -0
- package/lib/locales.d.ts +8 -11
- package/lib/locales.js +90 -79
- package/lib/log.d.ts +6 -11
- package/lib/log.js +61 -37
- package/lib/miniRedux.d.ts +13 -20
- package/lib/miniRedux.js +89 -43
- package/lib/number-input/hooks/index.d.ts +4 -0
- package/lib/number-input/hooks/index.js +29 -0
- package/lib/number-input/hooks/useNumberInputState.d.ts +2 -6
- package/lib/number-input/hooks/useNumberInputState.js +81 -71
- package/lib/number-input/index.d.ts +14 -0
- package/lib/number-input/index.js +41 -0
- package/lib/number-input/types.d.ts +88 -88
- package/lib/number-input/types.js +17 -0
- package/lib/number-input/utils/formatter.d.ts +10 -13
- package/lib/number-input/utils/formatter.js +49 -52
- package/lib/number-input/utils/index.d.ts +7 -0
- package/lib/number-input/utils/index.js +39 -0
- package/lib/number-input/utils/validation.d.ts +2 -7
- package/lib/number-input/utils/validation.js +52 -50
- package/lib/otherUtils.d.ts +9 -12
- package/lib/otherUtils.js +98 -86
- package/lib/platform.d.ts +9 -12
- package/lib/platform.js +123 -68
- package/lib/select/hooks/index.d.ts +5 -0
- package/lib/select/hooks/index.js +34 -0
- package/lib/select/hooks/useDebouncedSearch.d.ts +1 -4
- package/lib/select/hooks/useDebouncedSearch.js +41 -40
- package/lib/select/hooks/useSelectState.d.ts +7 -7
- package/lib/select/hooks/useSelectState.js +93 -58
- package/lib/select/index.d.ts +8 -7
- package/lib/select/index.js +53 -0
- package/lib/select/types.d.ts +211 -211
- package/lib/select/types.js +17 -0
- package/lib/select/utils/filterOptions.d.ts +6 -7
- package/lib/select/utils/filterOptions.js +37 -26
- package/lib/select/utils/helpers.d.ts +9 -10
- package/lib/select/utils/helpers.js +41 -45
- package/lib/select/utils/index.d.ts +7 -4
- package/lib/select/utils/index.js +48 -0
- package/lib/select/utils/sortOptions.d.ts +5 -6
- package/lib/select/utils/sortOptions.js +39 -18
- package/lib/select/utils/tagAggregation.d.ts +6 -6
- package/lib/select/utils/tagAggregation.js +41 -33
- package/lib/text-input/hooks/index.d.ts +4 -0
- package/lib/text-input/hooks/index.js +29 -0
- package/lib/text-input/hooks/useTextInputState.d.ts +2 -6
- package/lib/text-input/hooks/useTextInputState.js +79 -69
- package/lib/text-input/index.d.ts +14 -0
- package/lib/text-input/index.js +35 -0
- package/lib/text-input/types.d.ts +48 -48
- package/lib/text-input/types.js +17 -0
- package/lib/text-input/utils/index.d.ts +5 -0
- package/lib/text-input/utils/index.js +32 -0
- package/lib/text-input/utils/validation.d.ts +2 -7
- package/lib/text-input/utils/validation.js +58 -49
- package/lib/typeUtils.js +73 -68
- package/lib/walletValidity.d.ts +42 -44
- package/lib/walletValidity.js +393 -410
- package/lib/weighing.d.ts +45 -48
- package/lib/weighing.js +149 -104
- package/package.json +1 -1
- package/es/_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js +0 -27
- package/es/_virtual/_@oxc-project_runtime@0.122.0/helpers/defineProperty.js +0 -12
- package/es/_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js +0 -25
- package/es/_virtual/_@oxc-project_runtime@0.122.0/helpers/toPrimitive.js +0 -14
- package/es/_virtual/_@oxc-project_runtime@0.122.0/helpers/toPropertyKey.js +0 -9
- package/es/_virtual/_@oxc-project_runtime@0.122.0/helpers/typeof.js +0 -11
- package/es/date.d.ts +0 -31
- package/es/document.d.ts +0 -16
- package/es/image.d.ts +0 -14
- package/es/typeUtils.d.ts +0 -45
- package/lib/_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js +0 -27
- package/lib/_virtual/_@oxc-project_runtime@0.122.0/helpers/defineProperty.js +0 -12
- package/lib/_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js +0 -25
- package/lib/_virtual/_@oxc-project_runtime@0.122.0/helpers/toPrimitive.js +0 -14
- package/lib/_virtual/_@oxc-project_runtime@0.122.0/helpers/toPropertyKey.js +0 -9
- package/lib/_virtual/_@oxc-project_runtime@0.122.0/helpers/typeof.js +0 -16
- package/lib/_virtual/_rolldown/runtime.js +0 -23
- package/lib/date.d.ts +0 -31
- package/lib/document.d.ts +0 -16
- package/lib/image.d.ts +0 -14
- package/lib/typeUtils.d.ts +0 -45
|
@@ -1,41 +1,55 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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; }
|
|
3
7
|
/**
|
|
4
|
-
* 防抖搜索 Hook
|
|
5
|
-
* 对用户输入进行防抖处理,避免频繁触发搜索
|
|
6
|
-
*/
|
|
8
|
+
* 防抖搜索 Hook
|
|
9
|
+
* 对用户输入进行防抖处理,避免频繁触发搜索
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { useState, useEffect } from 'react';
|
|
13
|
+
|
|
7
14
|
/**
|
|
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
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
15
|
+
* 防抖搜索 Hook
|
|
16
|
+
* @template T 值的类型
|
|
17
|
+
* @param initialValue 初始值
|
|
18
|
+
* @param delay 防抖延迟时间(毫秒)
|
|
19
|
+
* @param callback 防抖后的回调函数
|
|
20
|
+
* @returns [防抖后的值, 设置值函数]
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```tsx
|
|
24
|
+
* const [debouncedSearch, setSearchInput] = useDebouncedSearch('', 300, (text) => {
|
|
25
|
+
* console.log('搜索关键词:', text);
|
|
26
|
+
* });
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export function useDebouncedSearch(initialValue, delay, callback) {
|
|
30
|
+
// 实时值(用户输入)
|
|
31
|
+
var _useState = useState(initialValue),
|
|
32
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
33
|
+
value = _useState2[0],
|
|
34
|
+
setValue = _useState2[1];
|
|
35
|
+
|
|
36
|
+
// 防抖后的值
|
|
37
|
+
var _useState3 = useState(initialValue),
|
|
38
|
+
_useState4 = _slicedToArray(_useState3, 2),
|
|
39
|
+
debouncedValue = _useState4[0],
|
|
40
|
+
setDebouncedValue = _useState4[1];
|
|
41
|
+
useEffect(function () {
|
|
42
|
+
// 创建定时器
|
|
43
|
+
var timer = setTimeout(function () {
|
|
44
|
+
setDebouncedValue(value);
|
|
45
|
+
// 触发回调
|
|
46
|
+
callback === null || callback === void 0 || callback(value);
|
|
47
|
+
}, delay);
|
|
48
|
+
|
|
49
|
+
// 清理定时器
|
|
50
|
+
return function () {
|
|
51
|
+
clearTimeout(timer);
|
|
52
|
+
};
|
|
53
|
+
}, [value, delay, callback]);
|
|
54
|
+
return [debouncedValue, setValue];
|
|
55
|
+
}
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Select 状态管理 Hook
|
|
3
|
+
* 处理受控/非受控模式,适用于单选和多选
|
|
4
|
+
*/
|
|
5
|
+
import type { UseSelectStateOptions, UseMultipleSelectStateOptions, SelectState, MultipleSelectState } from '../types';
|
|
4
6
|
/**
|
|
5
7
|
* 单选状态管理 Hook
|
|
6
8
|
* @template T 值的类型
|
|
7
9
|
* @param options Hook 配置选项
|
|
8
10
|
* @returns 状态管理对象
|
|
9
11
|
*/
|
|
10
|
-
declare function useSelectState<T = string | number>(options: UseSelectStateOptions<T>): SelectState<T>;
|
|
12
|
+
export declare function useSelectState<T = string | number>(options: UseSelectStateOptions<T>): SelectState<T>;
|
|
11
13
|
/**
|
|
12
14
|
* 多选状态管理 Hook
|
|
13
15
|
* @template T 值的类型
|
|
14
16
|
* @param options Hook 配置选项
|
|
15
17
|
* @returns 状态管理对象
|
|
16
18
|
*/
|
|
17
|
-
declare function useMultipleSelectState<T = string | number>(options: UseMultipleSelectStateOptions<T>): MultipleSelectState<T>;
|
|
18
|
-
//#endregion
|
|
19
|
-
export { useMultipleSelectState, useSelectState };
|
|
19
|
+
export declare function useMultipleSelectState<T = string | number>(options: UseMultipleSelectStateOptions<T>): MultipleSelectState<T>;
|
|
@@ -1,60 +1,117 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
2
|
+
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
3
|
+
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
4
|
+
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
5
|
+
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
6
|
+
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."); }
|
|
7
|
+
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); }
|
|
8
|
+
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; }
|
|
9
|
+
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; } }
|
|
10
|
+
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
3
11
|
/**
|
|
4
|
-
* Select 状态管理 Hook
|
|
5
|
-
* 处理受控/非受控模式,适用于单选和多选
|
|
6
|
-
*/
|
|
12
|
+
* Select 状态管理 Hook
|
|
13
|
+
* 处理受控/非受控模式,适用于单选和多选
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { useState, useCallback } from 'react';
|
|
7
17
|
/**
|
|
8
|
-
* 单选状态管理 Hook
|
|
9
|
-
* @template T 值的类型
|
|
10
|
-
* @param options Hook 配置选项
|
|
11
|
-
* @returns 状态管理对象
|
|
12
|
-
*/
|
|
13
|
-
function useSelectState(options) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
18
|
+
* 单选状态管理 Hook
|
|
19
|
+
* @template T 值的类型
|
|
20
|
+
* @param options Hook 配置选项
|
|
21
|
+
* @returns 状态管理对象
|
|
22
|
+
*/
|
|
23
|
+
export function useSelectState(options) {
|
|
24
|
+
var controlledValue = options.value,
|
|
25
|
+
defaultValue = options.defaultValue,
|
|
26
|
+
onChange = options.onChange;
|
|
27
|
+
|
|
28
|
+
// 判断是否为受控组件
|
|
29
|
+
var isControlled = controlledValue !== undefined;
|
|
30
|
+
|
|
31
|
+
// 内部状态(非受控模式使用)
|
|
32
|
+
var _useState = useState(defaultValue !== null && defaultValue !== void 0 ? defaultValue : null),
|
|
33
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
34
|
+
internalValue = _useState2[0],
|
|
35
|
+
setInternalValue = _useState2[1];
|
|
36
|
+
|
|
37
|
+
// 当前值
|
|
38
|
+
var value = isControlled ? controlledValue : internalValue;
|
|
39
|
+
|
|
40
|
+
// 设置值
|
|
41
|
+
var setValue = useCallback(function (newValue) {
|
|
42
|
+
if (!isControlled) {
|
|
43
|
+
setInternalValue(newValue);
|
|
44
|
+
}
|
|
45
|
+
// 触发 onChange 回调
|
|
46
|
+
onChange === null || onChange === void 0 || onChange(newValue, null); // TODO: 传入对应的 option 对象
|
|
47
|
+
}, [isControlled, onChange]);
|
|
48
|
+
|
|
49
|
+
// 清空值
|
|
50
|
+
var clearValue = useCallback(function () {
|
|
51
|
+
setValue(null);
|
|
52
|
+
}, [setValue]);
|
|
53
|
+
return {
|
|
54
|
+
value: value,
|
|
55
|
+
setValue: setValue,
|
|
56
|
+
clearValue: clearValue
|
|
57
|
+
};
|
|
29
58
|
}
|
|
59
|
+
|
|
30
60
|
/**
|
|
31
|
-
* 多选状态管理 Hook
|
|
32
|
-
* @template T 值的类型
|
|
33
|
-
* @param options Hook 配置选项
|
|
34
|
-
* @returns 状态管理对象
|
|
35
|
-
*/
|
|
36
|
-
function useMultipleSelectState(options) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
+
* 多选状态管理 Hook
|
|
62
|
+
* @template T 值的类型
|
|
63
|
+
* @param options Hook 配置选项
|
|
64
|
+
* @returns 状态管理对象
|
|
65
|
+
*/
|
|
66
|
+
export function useMultipleSelectState(options) {
|
|
67
|
+
var controlledValue = options.value,
|
|
68
|
+
defaultValue = options.defaultValue,
|
|
69
|
+
onChange = options.onChange;
|
|
70
|
+
|
|
71
|
+
// 判断是否为受控组件
|
|
72
|
+
var isControlled = controlledValue !== undefined;
|
|
73
|
+
|
|
74
|
+
// 内部状态(非受控模式使用)
|
|
75
|
+
var _useState3 = useState(defaultValue !== null && defaultValue !== void 0 ? defaultValue : []),
|
|
76
|
+
_useState4 = _slicedToArray(_useState3, 2),
|
|
77
|
+
internalValue = _useState4[0],
|
|
78
|
+
setInternalValue = _useState4[1];
|
|
79
|
+
|
|
80
|
+
// 当前值
|
|
81
|
+
var value = isControlled ? controlledValue !== null && controlledValue !== void 0 ? controlledValue : [] : internalValue;
|
|
82
|
+
|
|
83
|
+
// 设置值
|
|
84
|
+
var setValue = useCallback(function (newValue) {
|
|
85
|
+
if (!isControlled) {
|
|
86
|
+
setInternalValue(newValue);
|
|
87
|
+
}
|
|
88
|
+
// 触发 onChange 回调
|
|
89
|
+
onChange === null || onChange === void 0 || onChange(newValue, []); // TODO: 传入对应的 options 数组
|
|
90
|
+
}, [isControlled, onChange]);
|
|
91
|
+
|
|
92
|
+
// 添加一个值
|
|
93
|
+
var addValue = useCallback(function (val) {
|
|
94
|
+
var newValue = [].concat(_toConsumableArray(value), [val]);
|
|
95
|
+
setValue(newValue);
|
|
96
|
+
}, [value, setValue]);
|
|
97
|
+
|
|
98
|
+
// 移除一个值
|
|
99
|
+
var removeValue = useCallback(function (val) {
|
|
100
|
+
var newValue = value.filter(function (v) {
|
|
101
|
+
return v !== val;
|
|
102
|
+
});
|
|
103
|
+
setValue(newValue);
|
|
104
|
+
}, [value, setValue]);
|
|
105
|
+
|
|
106
|
+
// 清空所有值
|
|
107
|
+
var clearValue = useCallback(function () {
|
|
108
|
+
setValue([]);
|
|
109
|
+
}, [setValue]);
|
|
110
|
+
return {
|
|
111
|
+
value: value,
|
|
112
|
+
setValue: setValue,
|
|
113
|
+
addValue: addValue,
|
|
114
|
+
removeValue: removeValue,
|
|
115
|
+
clearValue: clearValue
|
|
116
|
+
};
|
|
117
|
+
}
|
package/es/select/index.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Select 组件相关工具和类型导出
|
|
3
|
+
* 供 PisellSingleSelect、PisellMultipleSelect 等组件使用
|
|
4
|
+
*/
|
|
5
|
+
export type { SelectOption, FilterOptionFunc, SortDirection, SelectState, MultipleSelectState, TagDisplayInfo, UseSelectStateOptions, UseMultipleSelectStateOptions, BaseSelectProps, } from './types';
|
|
6
|
+
export { useSelectState, useMultipleSelectState } from './hooks/useSelectState';
|
|
7
|
+
export { useDebouncedSearch } from './hooks/useDebouncedSearch';
|
|
8
|
+
export { filterOptions, defaultFilterOption, sortOptions, calculateTagDisplay, findOptionByValue, findOptionsByValues, isOptionSelected, isOptionSelectedInMultiple, generateId, } from './utils';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Select 组件相关工具和类型导出
|
|
3
|
+
* 供 PisellSingleSelect、PisellMultipleSelect 等组件使用
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// 导出所有类型
|
|
7
|
+
|
|
8
|
+
// 导出 Hooks
|
|
9
|
+
export { useSelectState, useMultipleSelectState } from "./hooks/useSelectState";
|
|
10
|
+
export { useDebouncedSearch } from "./hooks/useDebouncedSearch";
|
|
11
|
+
|
|
12
|
+
// 导出工具函数
|
|
13
|
+
export { filterOptions, defaultFilterOption, sortOptions, calculateTagDisplay, findOptionByValue, findOptionsByValues, isOptionSelected, isOptionSelectedInMultiple, generateId } from "./utils";
|