@pisell/utils 1.0.56 → 1.0.57

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 (117) hide show
  1. package/es/common/index.d.ts +5 -0
  2. package/es/common/index.js +5 -0
  3. package/es/common/stateHelper.d.ts +18 -0
  4. package/es/common/stateHelper.js +25 -0
  5. package/es/common/types.d.ts +28 -0
  6. package/es/common/types.js +1 -0
  7. package/es/index.d.ts +24 -0
  8. package/es/index.js +4 -0
  9. package/es/number-input/hooks/index.d.ts +4 -0
  10. package/es/number-input/hooks/index.js +5 -0
  11. package/es/number-input/hooks/useNumberInputState.d.ts +25 -0
  12. package/es/number-input/hooks/useNumberInputState.js +129 -0
  13. package/es/number-input/index.d.ts +14 -0
  14. package/es/number-input/index.js +21 -0
  15. package/es/number-input/types.d.ts +96 -0
  16. package/es/number-input/types.js +1 -0
  17. package/es/number-input/utils/formatter.d.ts +53 -0
  18. package/es/number-input/utils/formatter.js +82 -0
  19. package/es/number-input/utils/index.d.ts +7 -0
  20. package/es/number-input/utils/index.js +8 -0
  21. package/es/number-input/utils/validation.d.ts +27 -0
  22. package/es/number-input/utils/validation.js +67 -0
  23. package/es/select/hooks/index.d.ts +5 -0
  24. package/es/select/hooks/index.js +6 -0
  25. package/es/select/hooks/useDebouncedSearch.d.ts +20 -0
  26. package/es/select/hooks/useDebouncedSearch.js +55 -0
  27. package/es/select/hooks/useSelectState.d.ts +19 -0
  28. package/es/select/hooks/useSelectState.js +117 -0
  29. package/es/select/index.d.ts +8 -0
  30. package/es/select/index.js +13 -0
  31. package/es/select/types.d.ts +256 -0
  32. package/es/select/types.js +1 -0
  33. package/es/select/utils/filterOptions.d.ts +22 -0
  34. package/es/select/utils/filterOptions.js +34 -0
  35. package/es/select/utils/helpers.d.ts +42 -0
  36. package/es/select/utils/helpers.js +61 -0
  37. package/es/select/utils/index.d.ts +7 -0
  38. package/es/select/utils/index.js +8 -0
  39. package/es/select/utils/sortOptions.d.ts +12 -0
  40. package/es/select/utils/sortOptions.js +32 -0
  41. package/es/select/utils/tagAggregation.d.ts +25 -0
  42. package/es/select/utils/tagAggregation.js +48 -0
  43. package/es/text-input/hooks/index.d.ts +4 -0
  44. package/es/text-input/hooks/index.js +5 -0
  45. package/es/text-input/hooks/useTextInputState.d.ts +24 -0
  46. package/es/text-input/hooks/useTextInputState.js +127 -0
  47. package/es/text-input/index.d.ts +14 -0
  48. package/es/text-input/index.js +20 -0
  49. package/es/text-input/types.d.ts +56 -0
  50. package/es/text-input/types.js +1 -0
  51. package/es/text-input/utils/index.d.ts +5 -0
  52. package/es/text-input/utils/index.js +7 -0
  53. package/es/text-input/utils/validation.d.ts +23 -0
  54. package/es/text-input/utils/validation.js +71 -0
  55. package/lib/common/index.d.ts +5 -0
  56. package/lib/common/index.js +29 -0
  57. package/lib/common/stateHelper.d.ts +18 -0
  58. package/lib/common/stateHelper.js +34 -0
  59. package/lib/common/types.d.ts +28 -0
  60. package/lib/common/types.js +17 -0
  61. package/lib/index.d.ts +24 -0
  62. package/lib/index.js +9 -1
  63. package/lib/number-input/hooks/index.d.ts +4 -0
  64. package/lib/number-input/hooks/index.js +29 -0
  65. package/lib/number-input/hooks/useNumberInputState.d.ts +25 -0
  66. package/lib/number-input/hooks/useNumberInputState.js +83 -0
  67. package/lib/number-input/index.d.ts +14 -0
  68. package/lib/number-input/index.js +41 -0
  69. package/lib/number-input/types.d.ts +96 -0
  70. package/lib/number-input/types.js +17 -0
  71. package/lib/number-input/utils/formatter.d.ts +53 -0
  72. package/lib/number-input/utils/formatter.js +55 -0
  73. package/lib/number-input/utils/index.d.ts +7 -0
  74. package/lib/number-input/utils/index.js +39 -0
  75. package/lib/number-input/utils/validation.d.ts +27 -0
  76. package/lib/number-input/utils/validation.js +54 -0
  77. package/lib/select/hooks/index.d.ts +5 -0
  78. package/lib/select/hooks/index.js +34 -0
  79. package/lib/select/hooks/useDebouncedSearch.d.ts +20 -0
  80. package/lib/select/hooks/useDebouncedSearch.js +43 -0
  81. package/lib/select/hooks/useSelectState.d.ts +19 -0
  82. package/lib/select/hooks/useSelectState.js +97 -0
  83. package/lib/select/index.d.ts +8 -0
  84. package/lib/select/index.js +53 -0
  85. package/lib/select/types.d.ts +256 -0
  86. package/lib/select/types.js +17 -0
  87. package/lib/select/utils/filterOptions.d.ts +22 -0
  88. package/lib/select/utils/filterOptions.js +41 -0
  89. package/lib/select/utils/helpers.d.ts +42 -0
  90. package/lib/select/utils/helpers.js +51 -0
  91. package/lib/select/utils/index.d.ts +7 -0
  92. package/lib/select/utils/index.js +48 -0
  93. package/lib/select/utils/sortOptions.d.ts +12 -0
  94. package/lib/select/utils/sortOptions.js +41 -0
  95. package/lib/select/utils/tagAggregation.d.ts +25 -0
  96. package/lib/select/utils/tagAggregation.js +43 -0
  97. package/lib/text-input/hooks/index.d.ts +4 -0
  98. package/lib/text-input/hooks/index.js +29 -0
  99. package/lib/text-input/hooks/useTextInputState.d.ts +24 -0
  100. package/lib/text-input/hooks/useTextInputState.js +81 -0
  101. package/lib/text-input/index.d.ts +14 -0
  102. package/lib/text-input/index.js +35 -0
  103. package/lib/text-input/types.d.ts +56 -0
  104. package/lib/text-input/types.js +17 -0
  105. package/lib/text-input/utils/index.d.ts +5 -0
  106. package/lib/text-input/utils/index.js +32 -0
  107. package/lib/text-input/utils/validation.d.ts +23 -0
  108. package/lib/text-input/utils/validation.js +60 -0
  109. package/package.json +1 -1
  110. package/es/format.d.ts +0 -28
  111. package/es/jsBridge/index.d.ts +0 -22
  112. package/es/miniRedux.d.ts +0 -16
  113. package/es/otherUtils.d.ts +0 -48
  114. package/lib/format.d.ts +0 -28
  115. package/lib/jsBridge/index.d.ts +0 -22
  116. package/lib/miniRedux.d.ts +0 -16
  117. package/lib/otherUtils.d.ts +0 -48
@@ -0,0 +1,5 @@
1
+ /**
2
+ * 公共工具和类型导出
3
+ */
4
+ export type { DisplayState, ValidationResult } from './types';
5
+ export { getDisplayState } from './stateHelper';
@@ -0,0 +1,5 @@
1
+ /**
2
+ * 公共工具和类型导出
3
+ */
4
+
5
+ export { getDisplayState } from "./stateHelper";
@@ -0,0 +1,18 @@
1
+ import type { DisplayState } from './types';
2
+ /**
3
+ * 根据 mode 和 disabled 计算组件的显示状态
4
+ *
5
+ * 状态优先级:disabled > mode
6
+ * 当 disabled=true 时,无论 mode 是什么,都显示为禁用态
7
+ *
8
+ * @param mode 状态模式('read' | 'edit')
9
+ * @param disabled 是否禁用
10
+ * @returns 显示状态('read' | 'edit' | 'disabled')
11
+ *
12
+ * @example
13
+ * getDisplayState('edit', false) // 'edit'
14
+ * getDisplayState('read', false) // 'read'
15
+ * getDisplayState('edit', true) // 'disabled'
16
+ * getDisplayState('read', true) // 'disabled'
17
+ */
18
+ export declare function getDisplayState(mode?: 'read' | 'edit', disabled?: boolean): DisplayState;
@@ -0,0 +1,25 @@
1
+ /**
2
+ * 根据 mode 和 disabled 计算组件的显示状态
3
+ *
4
+ * 状态优先级:disabled > mode
5
+ * 当 disabled=true 时,无论 mode 是什么,都显示为禁用态
6
+ *
7
+ * @param mode 状态模式('read' | 'edit')
8
+ * @param disabled 是否禁用
9
+ * @returns 显示状态('read' | 'edit' | 'disabled')
10
+ *
11
+ * @example
12
+ * getDisplayState('edit', false) // 'edit'
13
+ * getDisplayState('read', false) // 'read'
14
+ * getDisplayState('edit', true) // 'disabled'
15
+ * getDisplayState('read', true) // 'disabled'
16
+ */
17
+ export function getDisplayState(mode, disabled) {
18
+ // disabled 优先级最高
19
+ if (disabled) {
20
+ return 'disabled';
21
+ }
22
+
23
+ // 返回基础模式,默认为 edit
24
+ return mode || 'edit';
25
+ }
@@ -0,0 +1,28 @@
1
+ /**
2
+ * 公共类型定义
3
+ * 供所有输入组件使用
4
+ */
5
+ /**
6
+ * 显示状态类型
7
+ *
8
+ * 所有输入组件的三种状态:
9
+ * - read: 只读态,纯文本展示
10
+ * - edit: 编辑态,显示输入控件
11
+ * - disabled: 禁用态,禁用的输入控件
12
+ */
13
+ export declare type DisplayState = 'read' | 'edit' | 'disabled';
14
+ /**
15
+ * 校验结果类型
16
+ *
17
+ * 所有输入组件的校验结果统一格式
18
+ */
19
+ export interface ValidationResult {
20
+ /**
21
+ * 是否校验通过
22
+ */
23
+ isValid: boolean;
24
+ /**
25
+ * 错误信息数组
26
+ */
27
+ errors: string[];
28
+ }
@@ -0,0 +1 @@
1
+ export {};
package/es/index.d.ts ADDED
@@ -0,0 +1,24 @@
1
+ export * from './otherUtils';
2
+ export * from './typeUtils';
3
+ export * from './document';
4
+ export * from './date';
5
+ export * from './platform';
6
+ export * from './log';
7
+ export * from './format';
8
+ export * from './escPosPrinter';
9
+ export * from './miniRedux';
10
+ export * from './jsBridge';
11
+ export * from './image';
12
+ export * from './locales';
13
+ export * from './arrayUtils';
14
+ export * from './walletValidity';
15
+ export * from './common';
16
+ export * from './text-input';
17
+ export * from './number-input';
18
+ export * from './select';
19
+ export declare const setPisellUtilsConfig: (config: {
20
+ [key: string]: any;
21
+ }) => void;
22
+ export declare const getPisellUtilsConfig: () => {
23
+ [key: string]: any;
24
+ };
package/es/index.js CHANGED
@@ -18,6 +18,10 @@ export * from "./image";
18
18
  export * from "./locales";
19
19
  export * from "./arrayUtils";
20
20
  export * from "./walletValidity";
21
+ export * from "./common";
22
+ export * from "./text-input";
23
+ export * from "./number-input";
24
+ export * from "./select";
21
25
  // export { default as firebase } from './firebase';
22
26
 
23
27
  export var setPisellUtilsConfig = function setPisellUtilsConfig(config) {
@@ -0,0 +1,4 @@
1
+ /**
2
+ * 数值输入 Hooks 导出
3
+ */
4
+ export { useNumberInputState } from './useNumberInputState';
@@ -0,0 +1,5 @@
1
+ /**
2
+ * 数值输入 Hooks 导出
3
+ */
4
+
5
+ export { useNumberInputState } from "./useNumberInputState";
@@ -0,0 +1,25 @@
1
+ import type { BaseNumberInputProps, NumberInputState } from '../types';
2
+ /**
3
+ * 数值输入状态管理 Hook
4
+ *
5
+ * 管理组件的内部状态,包括:
6
+ * - 值管理(受控/非受控模式)
7
+ * - 校验状态管理
8
+ * - 事件处理函数封装
9
+ *
10
+ * 支持受控和非受控两种模式:
11
+ * - 受控模式:value 由外部传入,组件不维护内部状态
12
+ * - 非受控模式:使用 defaultValue,组件维护内部状态
13
+ *
14
+ * @param props 组件 Props(包含校验配置)
15
+ * @returns 数值输入状态对象
16
+ *
17
+ * @example
18
+ * // 受控模式
19
+ * const state = useNumberInputState({ value: 10, onChange: (v) => console.log(v) });
20
+ *
21
+ * @example
22
+ * // 非受控模式
23
+ * const state = useNumberInputState({ defaultValue: 10, min: 0, max: 100 });
24
+ */
25
+ export declare function useNumberInputState(props: BaseNumberInputProps): NumberInputState;
@@ -0,0 +1,129 @@
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
+ import { useState, useCallback } from 'react';
8
+ import { validateValue } from "../utils/validation";
9
+ /**
10
+ * 数值输入状态管理 Hook
11
+ *
12
+ * 管理组件的内部状态,包括:
13
+ * - 值管理(受控/非受控模式)
14
+ * - 校验状态管理
15
+ * - 事件处理函数封装
16
+ *
17
+ * 支持受控和非受控两种模式:
18
+ * - 受控模式:value 由外部传入,组件不维护内部状态
19
+ * - 非受控模式:使用 defaultValue,组件维护内部状态
20
+ *
21
+ * @param props 组件 Props(包含校验配置)
22
+ * @returns 数值输入状态对象
23
+ *
24
+ * @example
25
+ * // 受控模式
26
+ * const state = useNumberInputState({ value: 10, onChange: (v) => console.log(v) });
27
+ *
28
+ * @example
29
+ * // 非受控模式
30
+ * const state = useNumberInputState({ defaultValue: 10, min: 0, max: 100 });
31
+ */
32
+ export function useNumberInputState(props) {
33
+ var _props$defaultValue;
34
+ // 判断是否为受控组件
35
+ var isControlled = props.value !== undefined;
36
+
37
+ // 内部状态(仅非受控模式使用)
38
+ var _useState = useState((_props$defaultValue = props.defaultValue) !== null && _props$defaultValue !== void 0 ? _props$defaultValue : null),
39
+ _useState2 = _slicedToArray(_useState, 2),
40
+ innerValue = _useState2[0],
41
+ setInnerValue = _useState2[1];
42
+ var _useState3 = useState(false),
43
+ _useState4 = _slicedToArray(_useState3, 2),
44
+ isTouched = _useState4[0],
45
+ setIsTouched = _useState4[1];
46
+ var _useState5 = useState(true),
47
+ _useState6 = _slicedToArray(_useState5, 2),
48
+ isValid = _useState6[0],
49
+ setIsValid = _useState6[1];
50
+ var _useState7 = useState([]),
51
+ _useState8 = _slicedToArray(_useState7, 2),
52
+ errors = _useState8[0],
53
+ setErrors = _useState8[1];
54
+
55
+ // 当前值(受控模式优先使用 props.value)
56
+ var currentValue = isControlled ? props.value : innerValue;
57
+
58
+ /**
59
+ * 值变更处理函数
60
+ *
61
+ * 1. 更新内部状态(非受控模式)
62
+ * 2. 根据 validateTrigger 决定是否立即校验
63
+ * 3. 触发外部 onChange 回调
64
+ */
65
+ var handleChange = useCallback(function (newValue) {
66
+ var _props$onChange;
67
+ // 更新内部状态(非受控模式)
68
+ if (!isControlled) {
69
+ setInnerValue(newValue);
70
+ }
71
+
72
+ // 根据配置决定是否立即校验
73
+ var shouldValidate = props.validateTrigger === 'onChange' || props.validateTrigger === 'both';
74
+ if (shouldValidate) {
75
+ var _props$onValidate;
76
+ var validation = validateValue(newValue, props);
77
+ setIsValid(validation.isValid);
78
+ setErrors(validation.errors);
79
+ (_props$onValidate = props.onValidate) === null || _props$onValidate === void 0 || _props$onValidate.call(props, validation.isValid, validation.errors);
80
+ }
81
+
82
+ // 触发外部回调
83
+ (_props$onChange = props.onChange) === null || _props$onChange === void 0 || _props$onChange.call(props, newValue);
84
+ }, [isControlled, props]);
85
+
86
+ /**
87
+ * 失焦处理函数
88
+ *
89
+ * 1. 标记为已触摸
90
+ * 2. 执行失焦时的校验(根据 validateTrigger)
91
+ * 3. 触发外部 onBlur 回调
92
+ */
93
+ var handleBlur = useCallback(function () {
94
+ var _props$onBlur;
95
+ setIsTouched(true);
96
+
97
+ // 失焦时执行校验(如果 validateTrigger 不是 'onChange')
98
+ var shouldValidate = props.validateTrigger !== 'onChange';
99
+ if (shouldValidate) {
100
+ var _props$onValidate2;
101
+ var validation = validateValue(currentValue, props);
102
+ setIsValid(validation.isValid);
103
+ setErrors(validation.errors);
104
+ (_props$onValidate2 = props.onValidate) === null || _props$onValidate2 === void 0 || _props$onValidate2.call(props, validation.isValid, validation.errors);
105
+ }
106
+ (_props$onBlur = props.onBlur) === null || _props$onBlur === void 0 || _props$onBlur.call(props, currentValue !== null && currentValue !== void 0 ? currentValue : null);
107
+ }, [currentValue, props]);
108
+
109
+ /**
110
+ * 聚焦处理函数
111
+ *
112
+ * 1. 标记为已触摸
113
+ * 2. 触发外部 onFocus 回调
114
+ */
115
+ var handleFocus = useCallback(function () {
116
+ var _props$onFocus;
117
+ setIsTouched(true);
118
+ (_props$onFocus = props.onFocus) === null || _props$onFocus === void 0 || _props$onFocus.call(props);
119
+ }, [props]);
120
+ return {
121
+ value: currentValue,
122
+ isTouched: isTouched,
123
+ isValid: isValid,
124
+ errors: errors,
125
+ handleChange: handleChange,
126
+ handleBlur: handleBlur,
127
+ handleFocus: handleFocus
128
+ };
129
+ }
@@ -0,0 +1,14 @@
1
+ /**
2
+ * 数值输入组件公共逻辑导出
3
+ *
4
+ * 供以下组件使用:
5
+ * - PisellNumber
6
+ * - PisellCurrency
7
+ * - PisellPercent
8
+ * - 其他数值输入组件
9
+ */
10
+ export type { DisplayState, ValidationResult, NumberInputState, BaseNumberInputProps, } from './types';
11
+ export { useNumberInputState } from './hooks';
12
+ export { getDisplayState, formatNumber, addThousandsSeparator, formatPrecision, } from './utils';
13
+ export { validateValue as validateNumberValue } from './utils';
14
+ export type { NumberFormatOptions } from './utils';
@@ -0,0 +1,21 @@
1
+ /**
2
+ * 数值输入组件公共逻辑导出
3
+ *
4
+ * 供以下组件使用:
5
+ * - PisellNumber
6
+ * - PisellCurrency
7
+ * - PisellPercent
8
+ * - 其他数值输入组件
9
+ */
10
+
11
+ // 类型
12
+
13
+ // Hooks
14
+ export { useNumberInputState } from "./hooks";
15
+
16
+ // 工具函数
17
+ export { getDisplayState, formatNumber, addThousandsSeparator, formatPrecision } from "./utils";
18
+ // 使用别名导出避免与 text-input 的 validateValue 重名
19
+ export { validateValue as validateNumberValue } from "./utils";
20
+
21
+ // 格式化相关类型
@@ -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
+ }