@pisell/utils 3.0.5 → 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.
Files changed (157) hide show
  1. package/es/arrayUtils.d.ts +72 -0
  2. package/es/arrayUtils.js +142 -3
  3. package/es/common/index.d.ts +5 -0
  4. package/es/common/index.js +5 -0
  5. package/es/common/stateHelper.d.ts +18 -0
  6. package/es/common/stateHelper.js +25 -0
  7. package/es/common/types.d.ts +28 -0
  8. package/es/common/types.js +1 -0
  9. package/es/componentStorage.d.ts +36 -0
  10. package/es/componentStorage.js +142 -0
  11. package/es/format.d.ts +5 -1
  12. package/es/format.js +53 -8
  13. package/es/index.d.ts +6 -0
  14. package/es/index.js +6 -0
  15. package/es/jsBridge/index.d.ts +9 -105
  16. package/es/jsBridge/index.js +28 -12
  17. package/es/jsBridge/types.d.ts +149 -0
  18. package/es/jsBridge/types.js +1 -0
  19. package/es/locales.d.ts +2 -5
  20. package/es/locales.js +36 -7
  21. package/es/miniRedux.js +7 -2
  22. package/es/number-input/hooks/index.d.ts +4 -0
  23. package/es/number-input/hooks/index.js +5 -0
  24. package/es/number-input/hooks/useNumberInputState.d.ts +25 -0
  25. package/es/number-input/hooks/useNumberInputState.js +129 -0
  26. package/es/number-input/index.d.ts +14 -0
  27. package/es/number-input/index.js +21 -0
  28. package/es/number-input/types.d.ts +96 -0
  29. package/es/number-input/types.js +1 -0
  30. package/es/number-input/utils/formatter.d.ts +53 -0
  31. package/es/number-input/utils/formatter.js +82 -0
  32. package/es/number-input/utils/index.d.ts +7 -0
  33. package/es/number-input/utils/index.js +8 -0
  34. package/es/number-input/utils/validation.d.ts +27 -0
  35. package/es/number-input/utils/validation.js +67 -0
  36. package/es/otherUtils.d.ts +48 -0
  37. package/es/select/hooks/index.d.ts +5 -0
  38. package/es/select/hooks/index.js +6 -0
  39. package/es/select/hooks/useDebouncedSearch.d.ts +20 -0
  40. package/es/select/hooks/useDebouncedSearch.js +55 -0
  41. package/es/select/hooks/useSelectState.d.ts +19 -0
  42. package/es/select/hooks/useSelectState.js +117 -0
  43. package/es/select/index.d.ts +8 -0
  44. package/es/select/index.js +13 -0
  45. package/es/select/types.d.ts +256 -0
  46. package/es/select/types.js +1 -0
  47. package/es/select/utils/filterOptions.d.ts +22 -0
  48. package/es/select/utils/filterOptions.js +34 -0
  49. package/es/select/utils/helpers.d.ts +42 -0
  50. package/es/select/utils/helpers.js +61 -0
  51. package/es/select/utils/index.d.ts +7 -0
  52. package/es/select/utils/index.js +8 -0
  53. package/es/select/utils/sortOptions.d.ts +12 -0
  54. package/es/select/utils/sortOptions.js +32 -0
  55. package/es/select/utils/tagAggregation.d.ts +25 -0
  56. package/es/select/utils/tagAggregation.js +48 -0
  57. package/es/text-input/hooks/index.d.ts +4 -0
  58. package/es/text-input/hooks/index.js +5 -0
  59. package/es/text-input/hooks/useTextInputState.d.ts +24 -0
  60. package/es/text-input/hooks/useTextInputState.js +127 -0
  61. package/es/text-input/index.d.ts +14 -0
  62. package/es/text-input/index.js +20 -0
  63. package/es/text-input/types.d.ts +56 -0
  64. package/es/text-input/types.js +1 -0
  65. package/es/text-input/utils/index.d.ts +5 -0
  66. package/es/text-input/utils/index.js +7 -0
  67. package/es/text-input/utils/validation.d.ts +23 -0
  68. package/es/text-input/utils/validation.js +71 -0
  69. package/es/walletValidity.d.ts +55 -0
  70. package/es/walletValidity.js +143 -38
  71. package/es/weighing.d.ts +62 -0
  72. package/es/weighing.js +119 -0
  73. package/lib/arrayUtils.d.ts +72 -0
  74. package/lib/arrayUtils.js +75 -0
  75. package/lib/common/index.d.ts +5 -0
  76. package/lib/common/index.js +29 -0
  77. package/lib/common/stateHelper.d.ts +18 -0
  78. package/lib/common/stateHelper.js +34 -0
  79. package/lib/common/types.d.ts +28 -0
  80. package/lib/common/types.js +17 -0
  81. package/lib/componentStorage.d.ts +36 -0
  82. package/lib/componentStorage.js +144 -0
  83. package/lib/format.d.ts +5 -1
  84. package/lib/format.js +45 -5
  85. package/lib/index.d.ts +6 -0
  86. package/lib/index.js +13 -1
  87. package/lib/jsBridge/index.d.ts +9 -105
  88. package/lib/jsBridge/index.js +20 -1
  89. package/lib/jsBridge/types.d.ts +149 -0
  90. package/lib/jsBridge/types.js +17 -0
  91. package/lib/locales.d.ts +2 -5
  92. package/lib/locales.js +34 -7
  93. package/lib/miniRedux.js +5 -7
  94. package/lib/number-input/hooks/index.d.ts +4 -0
  95. package/lib/number-input/hooks/index.js +29 -0
  96. package/lib/number-input/hooks/useNumberInputState.d.ts +25 -0
  97. package/lib/number-input/hooks/useNumberInputState.js +83 -0
  98. package/lib/number-input/index.d.ts +14 -0
  99. package/lib/number-input/index.js +41 -0
  100. package/lib/number-input/types.d.ts +96 -0
  101. package/lib/number-input/types.js +17 -0
  102. package/lib/number-input/utils/formatter.d.ts +53 -0
  103. package/lib/number-input/utils/formatter.js +55 -0
  104. package/lib/number-input/utils/index.d.ts +7 -0
  105. package/lib/number-input/utils/index.js +39 -0
  106. package/lib/number-input/utils/validation.d.ts +27 -0
  107. package/lib/number-input/utils/validation.js +54 -0
  108. package/lib/otherUtils.d.ts +48 -0
  109. package/lib/select/hooks/index.d.ts +5 -0
  110. package/lib/select/hooks/index.js +34 -0
  111. package/lib/select/hooks/useDebouncedSearch.d.ts +20 -0
  112. package/lib/select/hooks/useDebouncedSearch.js +43 -0
  113. package/lib/select/hooks/useSelectState.d.ts +19 -0
  114. package/lib/select/hooks/useSelectState.js +97 -0
  115. package/lib/select/index.d.ts +8 -0
  116. package/lib/select/index.js +53 -0
  117. package/lib/select/types.d.ts +256 -0
  118. package/lib/select/types.js +17 -0
  119. package/lib/select/utils/filterOptions.d.ts +22 -0
  120. package/lib/select/utils/filterOptions.js +41 -0
  121. package/lib/select/utils/helpers.d.ts +42 -0
  122. package/lib/select/utils/helpers.js +51 -0
  123. package/lib/select/utils/index.d.ts +7 -0
  124. package/lib/select/utils/index.js +48 -0
  125. package/lib/select/utils/sortOptions.d.ts +12 -0
  126. package/lib/select/utils/sortOptions.js +41 -0
  127. package/lib/select/utils/tagAggregation.d.ts +25 -0
  128. package/lib/select/utils/tagAggregation.js +43 -0
  129. package/lib/text-input/hooks/index.d.ts +4 -0
  130. package/lib/text-input/hooks/index.js +29 -0
  131. package/lib/text-input/hooks/useTextInputState.d.ts +24 -0
  132. package/lib/text-input/hooks/useTextInputState.js +81 -0
  133. package/lib/text-input/index.d.ts +14 -0
  134. package/lib/text-input/index.js +35 -0
  135. package/lib/text-input/types.d.ts +56 -0
  136. package/lib/text-input/types.js +17 -0
  137. package/lib/text-input/utils/index.d.ts +5 -0
  138. package/lib/text-input/utils/index.js +32 -0
  139. package/lib/text-input/utils/validation.d.ts +23 -0
  140. package/lib/text-input/utils/validation.js +60 -0
  141. package/lib/walletValidity.d.ts +55 -0
  142. package/lib/walletValidity.js +120 -40
  143. package/lib/weighing.d.ts +62 -0
  144. package/lib/weighing.js +159 -0
  145. package/package.json +1 -1
  146. package/es/date.d.ts +0 -28
  147. package/es/document.d.ts +0 -13
  148. package/es/firebase/config/index.d.ts +0 -13
  149. package/es/firebase/index.d.ts +0 -28
  150. package/es/image.d.ts +0 -11
  151. package/es/typeUtils.d.ts +0 -42
  152. package/lib/date.d.ts +0 -28
  153. package/lib/document.d.ts +0 -13
  154. package/lib/firebase/config/index.d.ts +0 -13
  155. package/lib/firebase/index.d.ts +0 -28
  156. package/lib/image.d.ts +0 -11
  157. package/lib/typeUtils.d.ts +0 -42
@@ -0,0 +1,96 @@
1
+ /**
2
+ * 数值输入组件公共类型定义
3
+ * 供 PisellNumber、PisellCurrency、PisellPercent 等组件使用
4
+ */
5
+ export type { DisplayState, ValidationResult } from '../common';
6
+ /**
7
+ * 数值输入状态 Hook 返回值
8
+ */
9
+ export interface NumberInputState {
10
+ /**
11
+ * 当前值
12
+ */
13
+ value: number | null | undefined;
14
+ /**
15
+ * 是否已被触摸(交互过)
16
+ */
17
+ isTouched: boolean;
18
+ /**
19
+ * 是否校验通过
20
+ */
21
+ isValid: boolean;
22
+ /**
23
+ * 错误信息数组
24
+ */
25
+ errors: string[];
26
+ /**
27
+ * 值变更处理函数
28
+ */
29
+ handleChange: (value: number | null) => void;
30
+ /**
31
+ * 失焦处理函数
32
+ */
33
+ handleBlur: () => void;
34
+ /**
35
+ * 聚焦处理函数
36
+ */
37
+ handleFocus: () => void;
38
+ }
39
+ /**
40
+ * 基础数值输入 Props(供校验和状态管理使用)
41
+ */
42
+ export interface BaseNumberInputProps {
43
+ /**
44
+ * 当前值(受控)
45
+ */
46
+ value?: number | null;
47
+ /**
48
+ * 默认值(非受控)
49
+ */
50
+ defaultValue?: number | null;
51
+ /**
52
+ * 是否必填
53
+ */
54
+ required?: boolean;
55
+ /**
56
+ * 最小值
57
+ */
58
+ min?: number;
59
+ /**
60
+ * 最大值
61
+ */
62
+ max?: number;
63
+ /**
64
+ * 自定义校验函数
65
+ * @param value 当前值
66
+ * @returns true 表示通过,false 或错误信息字符串表示不通过
67
+ */
68
+ validator?: (value: number | null) => boolean | string;
69
+ /**
70
+ * 校验触发时机
71
+ * - onChange: 输入时即时校验
72
+ * - onBlur: 失焦时校验
73
+ * - both: 输入和失焦时都校验
74
+ */
75
+ validateTrigger?: 'onChange' | 'onBlur' | 'both';
76
+ /**
77
+ * 自定义错误提示信息
78
+ */
79
+ errorMessage?: string;
80
+ /**
81
+ * 值变更回调
82
+ */
83
+ onChange?: (value: number | null) => void;
84
+ /**
85
+ * 失焦回调
86
+ */
87
+ onBlur?: (value: number | null) => void;
88
+ /**
89
+ * 聚焦回调
90
+ */
91
+ onFocus?: () => void;
92
+ /**
93
+ * 校验回调
94
+ */
95
+ onValidate?: (isValid: boolean, errors: string[]) => void;
96
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,53 @@
1
+ /**
2
+ * 数值格式化工具函数
3
+ */
4
+ /**
5
+ * 格式化选项
6
+ */
7
+ export interface NumberFormatOptions {
8
+ /** 小数精度 */
9
+ precision?: number;
10
+ /** 是否使用千分位分隔符 */
11
+ useGrouping?: boolean;
12
+ /** 自定义格式化函数 */
13
+ formatter?: (value: number | null) => string;
14
+ }
15
+ /**
16
+ * 格式化数值
17
+ *
18
+ * @param value 数值
19
+ * @param options 格式化选项
20
+ * @returns 格式化后的字符串,null 时返回 null
21
+ *
22
+ * @example
23
+ * formatNumber(1234.567, { precision: 2, useGrouping: true })
24
+ * // 返回: "1,234.57"
25
+ *
26
+ * @example
27
+ * formatNumber(1234, { precision: 0, useGrouping: true })
28
+ * // 返回: "1,234"
29
+ */
30
+ export declare function formatNumber(value: number | null | undefined, options?: NumberFormatOptions): string | null;
31
+ /**
32
+ * 添加千分位分隔符
33
+ *
34
+ * @param value 数值
35
+ * @returns 添加千分位后的字符串
36
+ *
37
+ * @example
38
+ * addThousandsSeparator(1234567)
39
+ * // 返回: "1,234,567"
40
+ */
41
+ export declare function addThousandsSeparator(value: number): string;
42
+ /**
43
+ * 格式化精度
44
+ *
45
+ * @param value 数值
46
+ * @param precision 小数位数
47
+ * @returns 格式化后的数值
48
+ *
49
+ * @example
50
+ * formatPrecision(1.2345, 2)
51
+ * // 返回: 1.23
52
+ */
53
+ export declare function formatPrecision(value: number, precision: number): number;
@@ -0,0 +1,82 @@
1
+ /**
2
+ * 数值格式化工具函数
3
+ */
4
+
5
+ /**
6
+ * 格式化选项
7
+ */
8
+
9
+ /**
10
+ * 格式化数值
11
+ *
12
+ * @param value 数值
13
+ * @param options 格式化选项
14
+ * @returns 格式化后的字符串,null 时返回 null
15
+ *
16
+ * @example
17
+ * formatNumber(1234.567, { precision: 2, useGrouping: true })
18
+ * // 返回: "1,234.57"
19
+ *
20
+ * @example
21
+ * formatNumber(1234, { precision: 0, useGrouping: true })
22
+ * // 返回: "1,234"
23
+ */
24
+ export function formatNumber(value) {
25
+ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
26
+ // 处理空值
27
+ if (value === null || value === undefined) {
28
+ return null;
29
+ }
30
+ var precision = options.precision,
31
+ _options$useGrouping = options.useGrouping,
32
+ useGrouping = _options$useGrouping === void 0 ? false : _options$useGrouping,
33
+ formatter = options.formatter;
34
+
35
+ // 优先使用自定义格式化函数
36
+ if (formatter) {
37
+ return formatter(value);
38
+ }
39
+
40
+ // 使用 toLocaleString 进行格式化
41
+ if (useGrouping || precision !== undefined) {
42
+ return value.toLocaleString('zh-CN', {
43
+ minimumFractionDigits: precision,
44
+ maximumFractionDigits: precision,
45
+ useGrouping: useGrouping
46
+ });
47
+ }
48
+
49
+ // 默认转字符串
50
+ return String(value);
51
+ }
52
+
53
+ /**
54
+ * 添加千分位分隔符
55
+ *
56
+ * @param value 数值
57
+ * @returns 添加千分位后的字符串
58
+ *
59
+ * @example
60
+ * addThousandsSeparator(1234567)
61
+ * // 返回: "1,234,567"
62
+ */
63
+ export function addThousandsSeparator(value) {
64
+ return value.toLocaleString('zh-CN', {
65
+ useGrouping: true
66
+ });
67
+ }
68
+
69
+ /**
70
+ * 格式化精度
71
+ *
72
+ * @param value 数值
73
+ * @param precision 小数位数
74
+ * @returns 格式化后的数值
75
+ *
76
+ * @example
77
+ * formatPrecision(1.2345, 2)
78
+ * // 返回: 1.23
79
+ */
80
+ export function formatPrecision(value, precision) {
81
+ return Number(value.toFixed(precision));
82
+ }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * 数值输入工具函数导出
3
+ */
4
+ export { validateValue } from './validation';
5
+ export { getDisplayState } from '../../common';
6
+ export { formatNumber, addThousandsSeparator, formatPrecision } from './formatter';
7
+ export type { NumberFormatOptions } from './formatter';
@@ -0,0 +1,8 @@
1
+ /**
2
+ * 数值输入工具函数导出
3
+ */
4
+
5
+ export { validateValue } from "./validation";
6
+ // getDisplayState 已移到公共模块,从 common 重新导出
7
+ export { getDisplayState } from "../../common";
8
+ export { formatNumber, addThousandsSeparator, formatPrecision } from "./formatter";
@@ -0,0 +1,27 @@
1
+ import type { BaseNumberInputProps, ValidationResult } from '../types';
2
+ /**
3
+ * 校验数值输入
4
+ *
5
+ * 按照优先级执行校验规则:
6
+ * 1. 必填校验(required)
7
+ * 2. 最小值校验(min)
8
+ * 3. 最大值校验(max)
9
+ * 4. 自定义校验函数(validator)
10
+ *
11
+ * @param value 待校验的值
12
+ * @param props 校验配置
13
+ * @returns 校验结果对象,包含 isValid 和 errors
14
+ *
15
+ * @example
16
+ * validateValue(10, { required: true, min: 0, max: 100 })
17
+ * // { isValid: true, errors: [] }
18
+ *
19
+ * @example
20
+ * validateValue(null, { required: true })
21
+ * // { isValid: false, errors: ['此字段为必填项'] }
22
+ *
23
+ * @example
24
+ * validateValue(150, { min: 0, max: 100 })
25
+ * // { isValid: false, errors: ['不能大于 100'] }
26
+ */
27
+ export declare function validateValue(value: number | null | undefined, props: BaseNumberInputProps): ValidationResult;
@@ -0,0 +1,67 @@
1
+ /**
2
+ * 校验数值输入
3
+ *
4
+ * 按照优先级执行校验规则:
5
+ * 1. 必填校验(required)
6
+ * 2. 最小值校验(min)
7
+ * 3. 最大值校验(max)
8
+ * 4. 自定义校验函数(validator)
9
+ *
10
+ * @param value 待校验的值
11
+ * @param props 校验配置
12
+ * @returns 校验结果对象,包含 isValid 和 errors
13
+ *
14
+ * @example
15
+ * validateValue(10, { required: true, min: 0, max: 100 })
16
+ * // { isValid: true, errors: [] }
17
+ *
18
+ * @example
19
+ * validateValue(null, { required: true })
20
+ * // { isValid: false, errors: ['此字段为必填项'] }
21
+ *
22
+ * @example
23
+ * validateValue(150, { min: 0, max: 100 })
24
+ * // { isValid: false, errors: ['不能大于 100'] }
25
+ */
26
+ export function validateValue(value, props) {
27
+ var errors = [];
28
+
29
+ // 1. 必填校验
30
+ if (props.required && (value === null || value === undefined)) {
31
+ errors.push(props.errorMessage || '此字段为必填项');
32
+ return {
33
+ isValid: false,
34
+ errors: errors
35
+ };
36
+ }
37
+
38
+ // 如果值为空且非必填,直接返回有效
39
+ if (value === null || value === undefined) {
40
+ return {
41
+ isValid: true,
42
+ errors: errors
43
+ };
44
+ }
45
+
46
+ // 2. 最小值校验
47
+ if (props.min !== undefined && value < props.min) {
48
+ errors.push("\u4E0D\u80FD\u5C0F\u4E8E ".concat(props.min));
49
+ }
50
+
51
+ // 3. 最大值校验
52
+ if (props.max !== undefined && value > props.max) {
53
+ errors.push("\u4E0D\u80FD\u5927\u4E8E ".concat(props.max));
54
+ }
55
+
56
+ // 4. 自定义校验函数
57
+ if (props.validator) {
58
+ var result = props.validator(value);
59
+ if (result !== true) {
60
+ errors.push(typeof result === 'string' ? result : '校验失败');
61
+ }
62
+ }
63
+ return {
64
+ isValid: errors.length === 0,
65
+ errors: errors
66
+ };
67
+ }
@@ -0,0 +1,48 @@
1
+ /**
2
+ * @title: 判断是否无痕模式
3
+ * @description:
4
+ * @return: *
5
+ * @Author: shengjie.zuo
6
+ * @Date: 2024-09-4 09:58:22
7
+ */
8
+ export declare const detectIncognito: () => Promise<{
9
+ isPrivate: boolean;
10
+ browserName: string;
11
+ } | {
12
+ isPrivate: boolean;
13
+ }>;
14
+ /**
15
+ * @Description: 生成唯一id
16
+ * @Author: wzw
17
+ * @Date: 2020-12-01 14:20:29
18
+ * @param {*}
19
+ * @return {*}
20
+ */
21
+ export declare const getUniqueId: (prefix?: string, maxLength?: number) => string;
22
+ /**
23
+ * @title: 对数组进行增|删
24
+ * @description: 用于多选选中和取消选中的处理
25
+ * @param {any} list
26
+ * @param {any} item
27
+ * @param {string} key
28
+ * @return {*}
29
+ * @Author: zhiwei.Wang
30
+ * @Date: 2023-12-19 16:44
31
+ */
32
+ export declare const changeArray: <T>(list: T[] | undefined, item: T, key?: string) => T[];
33
+ /**
34
+ * @title: 判断某个字段是否在列表内
35
+ * @description:
36
+ * @param {*} T
37
+ * @param {boolean} param2
38
+ * @return {*}
39
+ * @Author: zhiwei.Wang
40
+ * @Date: 2023-12-19 16:54
41
+ */
42
+ export declare const getItemByArray: <T>(list: T[] | undefined, item: T, key?: string) => boolean;
43
+ export declare const createArray: (length: number, returnItem: any) => unknown[];
44
+ /**
45
+ * 根据对象内容生成唯一id
46
+ * @param obj
47
+ */
48
+ export declare const generateUniqueIdByObj: (obj: Record<string, any>) => any;
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Select Hooks 导出
3
+ */
4
+ export { useSelectState, useMultipleSelectState } from './useSelectState';
5
+ export { useDebouncedSearch } from './useDebouncedSearch';
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Select Hooks 导出
3
+ */
4
+
5
+ export { useSelectState, useMultipleSelectState } from "./useSelectState";
6
+ export { useDebouncedSearch } from "./useDebouncedSearch";
@@ -0,0 +1,20 @@
1
+ /**
2
+ * 防抖搜索 Hook
3
+ * 对用户输入进行防抖处理,避免频繁触发搜索
4
+ */
5
+ /**
6
+ * 防抖搜索 Hook
7
+ * @template T 值的类型
8
+ * @param initialValue 初始值
9
+ * @param delay 防抖延迟时间(毫秒)
10
+ * @param callback 防抖后的回调函数
11
+ * @returns [防抖后的值, 设置值函数]
12
+ *
13
+ * @example
14
+ * ```tsx
15
+ * const [debouncedSearch, setSearchInput] = useDebouncedSearch('', 300, (text) => {
16
+ * console.log('搜索关键词:', text);
17
+ * });
18
+ * ```
19
+ */
20
+ export declare function useDebouncedSearch<T = string>(initialValue: T, delay: number, callback?: (value: T) => void): [T, (value: T) => void];
@@ -0,0 +1,55 @@
1
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
2
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
3
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
4
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
5
+ function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
6
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
7
+ /**
8
+ * 防抖搜索 Hook
9
+ * 对用户输入进行防抖处理,避免频繁触发搜索
10
+ */
11
+
12
+ import { useState, useEffect } from 'react';
13
+
14
+ /**
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
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Select 状态管理 Hook
3
+ * 处理受控/非受控模式,适用于单选和多选
4
+ */
5
+ import type { UseSelectStateOptions, UseMultipleSelectStateOptions, SelectState, MultipleSelectState } from '../types';
6
+ /**
7
+ * 单选状态管理 Hook
8
+ * @template T 值的类型
9
+ * @param options Hook 配置选项
10
+ * @returns 状态管理对象
11
+ */
12
+ export declare function useSelectState<T = string | number>(options: UseSelectStateOptions<T>): SelectState<T>;
13
+ /**
14
+ * 多选状态管理 Hook
15
+ * @template T 值的类型
16
+ * @param options Hook 配置选项
17
+ * @returns 状态管理对象
18
+ */
19
+ export declare function useMultipleSelectState<T = string | number>(options: UseMultipleSelectStateOptions<T>): MultipleSelectState<T>;
@@ -0,0 +1,117 @@
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; }
11
+ /**
12
+ * Select 状态管理 Hook
13
+ * 处理受控/非受控模式,适用于单选和多选
14
+ */
15
+
16
+ import { useState, useCallback } from 'react';
17
+ /**
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
+ };
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
+ }
@@ -0,0 +1,8 @@
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";