@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.
Files changed (200) hide show
  1. package/es/arrayUtils.d.ts +23 -26
  2. package/es/arrayUtils.js +157 -97
  3. package/es/common/index.d.ts +5 -0
  4. package/es/common/index.js +5 -0
  5. package/es/common/stateHelper.d.ts +2 -6
  6. package/es/common/stateHelper.js +24 -22
  7. package/es/common/types.d.ts +10 -13
  8. package/es/common/types.js +1 -0
  9. package/es/componentStorage.d.ts +25 -27
  10. package/es/componentStorage.js +130 -94
  11. package/es/constants/index.js +9 -7
  12. package/es/date.js +53 -36
  13. package/es/document.js +67 -43
  14. package/es/escPosPrinter/EscPosPrinter.js +490 -353
  15. package/es/escPosPrinter/index.d.ts +9 -9
  16. package/es/escPosPrinter/index.js +52 -37
  17. package/es/firebase/config/index.js +54 -0
  18. package/es/firebase/index.js +232 -0
  19. package/es/format.d.ts +15 -12
  20. package/es/format.js +156 -89
  21. package/es/image.js +60 -48
  22. package/es/index.d.ts +24 -39
  23. package/es/index.js +35 -40
  24. package/es/jsBridge/index.d.ts +4 -8
  25. package/es/jsBridge/index.js +55 -56
  26. package/es/jsBridge/regDeviceApi.js +126 -87
  27. package/es/jsBridge/types.d.ts +115 -95
  28. package/es/jsBridge/types.js +1 -0
  29. package/es/locales.d.ts +8 -11
  30. package/es/locales.js +96 -77
  31. package/es/log.d.ts +6 -11
  32. package/es/log.js +59 -37
  33. package/es/miniRedux.d.ts +13 -20
  34. package/es/miniRedux.js +76 -42
  35. package/es/number-input/hooks/index.d.ts +4 -0
  36. package/es/number-input/hooks/index.js +5 -0
  37. package/es/number-input/hooks/useNumberInputState.d.ts +2 -6
  38. package/es/number-input/hooks/useNumberInputState.js +128 -71
  39. package/es/number-input/index.d.ts +14 -0
  40. package/es/number-input/index.js +21 -0
  41. package/es/number-input/types.d.ts +88 -88
  42. package/es/number-input/types.js +1 -0
  43. package/es/number-input/utils/formatter.d.ts +10 -13
  44. package/es/number-input/utils/formatter.js +77 -51
  45. package/es/number-input/utils/index.d.ts +7 -0
  46. package/es/number-input/utils/index.js +8 -0
  47. package/es/number-input/utils/validation.d.ts +2 -7
  48. package/es/number-input/utils/validation.js +66 -51
  49. package/es/otherUtils.d.ts +9 -12
  50. package/es/otherUtils.js +129 -71
  51. package/es/platform.d.ts +9 -12
  52. package/es/platform.js +85 -61
  53. package/es/select/hooks/index.d.ts +5 -0
  54. package/es/select/hooks/index.js +6 -0
  55. package/es/select/hooks/useDebouncedSearch.d.ts +1 -4
  56. package/es/select/hooks/useDebouncedSearch.js +53 -39
  57. package/es/select/hooks/useSelectState.d.ts +7 -7
  58. package/es/select/hooks/useSelectState.js +113 -56
  59. package/es/select/index.d.ts +8 -7
  60. package/es/select/index.js +13 -0
  61. package/es/select/types.d.ts +211 -211
  62. package/es/select/types.js +1 -0
  63. package/es/select/utils/filterOptions.d.ts +6 -7
  64. package/es/select/utils/filterOptions.js +31 -26
  65. package/es/select/utils/helpers.d.ts +9 -10
  66. package/es/select/utils/helpers.js +52 -42
  67. package/es/select/utils/index.d.ts +7 -4
  68. package/es/select/utils/index.js +8 -0
  69. package/es/select/utils/sortOptions.d.ts +5 -6
  70. package/es/select/utils/sortOptions.js +31 -19
  71. package/es/select/utils/tagAggregation.d.ts +6 -6
  72. package/es/select/utils/tagAggregation.js +47 -34
  73. package/es/text-input/hooks/index.d.ts +4 -0
  74. package/es/text-input/hooks/index.js +5 -0
  75. package/es/text-input/hooks/useTextInputState.d.ts +2 -6
  76. package/es/text-input/hooks/useTextInputState.js +126 -69
  77. package/es/text-input/index.d.ts +14 -0
  78. package/es/text-input/index.js +20 -0
  79. package/es/text-input/types.d.ts +48 -48
  80. package/es/text-input/types.js +1 -0
  81. package/es/text-input/utils/index.d.ts +5 -0
  82. package/es/text-input/utils/index.js +7 -0
  83. package/es/text-input/utils/validation.d.ts +2 -7
  84. package/es/text-input/utils/validation.js +70 -50
  85. package/es/typeUtils.js +84 -61
  86. package/es/walletValidity.d.ts +42 -44
  87. package/es/walletValidity.js +557 -396
  88. package/es/weighing.d.ts +45 -48
  89. package/es/weighing.js +106 -83
  90. package/lib/arrayUtils.d.ts +23 -26
  91. package/lib/arrayUtils.js +111 -101
  92. package/lib/common/index.d.ts +5 -0
  93. package/lib/common/index.js +29 -0
  94. package/lib/common/stateHelper.d.ts +2 -6
  95. package/lib/common/stateHelper.js +32 -21
  96. package/lib/common/types.d.ts +10 -13
  97. package/lib/common/types.js +17 -0
  98. package/lib/componentStorage.d.ts +25 -27
  99. package/lib/componentStorage.js +126 -96
  100. package/lib/constants/index.js +36 -7
  101. package/lib/date.js +65 -43
  102. package/lib/document.js +62 -46
  103. package/lib/escPosPrinter/EscPosPrinter.js +403 -327
  104. package/lib/escPosPrinter/index.d.ts +9 -9
  105. package/lib/escPosPrinter/index.js +84 -38
  106. package/lib/firebase/config/index.js +79 -0
  107. package/lib/firebase/index.js +180 -0
  108. package/lib/format.d.ts +15 -12
  109. package/lib/format.js +133 -91
  110. package/lib/image.js +74 -48
  111. package/lib/index.d.ts +24 -39
  112. package/lib/index.js +80 -137
  113. package/lib/jsBridge/index.d.ts +4 -8
  114. package/lib/jsBridge/index.js +76 -57
  115. package/lib/jsBridge/regDeviceApi.js +133 -86
  116. package/lib/jsBridge/types.d.ts +115 -95
  117. package/lib/jsBridge/types.js +17 -0
  118. package/lib/locales.d.ts +8 -11
  119. package/lib/locales.js +90 -79
  120. package/lib/log.d.ts +6 -11
  121. package/lib/log.js +61 -37
  122. package/lib/miniRedux.d.ts +13 -20
  123. package/lib/miniRedux.js +89 -43
  124. package/lib/number-input/hooks/index.d.ts +4 -0
  125. package/lib/number-input/hooks/index.js +29 -0
  126. package/lib/number-input/hooks/useNumberInputState.d.ts +2 -6
  127. package/lib/number-input/hooks/useNumberInputState.js +81 -71
  128. package/lib/number-input/index.d.ts +14 -0
  129. package/lib/number-input/index.js +41 -0
  130. package/lib/number-input/types.d.ts +88 -88
  131. package/lib/number-input/types.js +17 -0
  132. package/lib/number-input/utils/formatter.d.ts +10 -13
  133. package/lib/number-input/utils/formatter.js +49 -52
  134. package/lib/number-input/utils/index.d.ts +7 -0
  135. package/lib/number-input/utils/index.js +39 -0
  136. package/lib/number-input/utils/validation.d.ts +2 -7
  137. package/lib/number-input/utils/validation.js +52 -50
  138. package/lib/otherUtils.d.ts +9 -12
  139. package/lib/otherUtils.js +98 -86
  140. package/lib/platform.d.ts +9 -12
  141. package/lib/platform.js +123 -68
  142. package/lib/select/hooks/index.d.ts +5 -0
  143. package/lib/select/hooks/index.js +34 -0
  144. package/lib/select/hooks/useDebouncedSearch.d.ts +1 -4
  145. package/lib/select/hooks/useDebouncedSearch.js +41 -40
  146. package/lib/select/hooks/useSelectState.d.ts +7 -7
  147. package/lib/select/hooks/useSelectState.js +93 -58
  148. package/lib/select/index.d.ts +8 -7
  149. package/lib/select/index.js +53 -0
  150. package/lib/select/types.d.ts +211 -211
  151. package/lib/select/types.js +17 -0
  152. package/lib/select/utils/filterOptions.d.ts +6 -7
  153. package/lib/select/utils/filterOptions.js +37 -26
  154. package/lib/select/utils/helpers.d.ts +9 -10
  155. package/lib/select/utils/helpers.js +41 -45
  156. package/lib/select/utils/index.d.ts +7 -4
  157. package/lib/select/utils/index.js +48 -0
  158. package/lib/select/utils/sortOptions.d.ts +5 -6
  159. package/lib/select/utils/sortOptions.js +39 -18
  160. package/lib/select/utils/tagAggregation.d.ts +6 -6
  161. package/lib/select/utils/tagAggregation.js +41 -33
  162. package/lib/text-input/hooks/index.d.ts +4 -0
  163. package/lib/text-input/hooks/index.js +29 -0
  164. package/lib/text-input/hooks/useTextInputState.d.ts +2 -6
  165. package/lib/text-input/hooks/useTextInputState.js +79 -69
  166. package/lib/text-input/index.d.ts +14 -0
  167. package/lib/text-input/index.js +35 -0
  168. package/lib/text-input/types.d.ts +48 -48
  169. package/lib/text-input/types.js +17 -0
  170. package/lib/text-input/utils/index.d.ts +5 -0
  171. package/lib/text-input/utils/index.js +32 -0
  172. package/lib/text-input/utils/validation.d.ts +2 -7
  173. package/lib/text-input/utils/validation.js +58 -49
  174. package/lib/typeUtils.js +73 -68
  175. package/lib/walletValidity.d.ts +42 -44
  176. package/lib/walletValidity.js +393 -410
  177. package/lib/weighing.d.ts +45 -48
  178. package/lib/weighing.js +149 -104
  179. package/package.json +1 -1
  180. package/es/_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js +0 -27
  181. package/es/_virtual/_@oxc-project_runtime@0.122.0/helpers/defineProperty.js +0 -12
  182. package/es/_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js +0 -25
  183. package/es/_virtual/_@oxc-project_runtime@0.122.0/helpers/toPrimitive.js +0 -14
  184. package/es/_virtual/_@oxc-project_runtime@0.122.0/helpers/toPropertyKey.js +0 -9
  185. package/es/_virtual/_@oxc-project_runtime@0.122.0/helpers/typeof.js +0 -11
  186. package/es/date.d.ts +0 -31
  187. package/es/document.d.ts +0 -16
  188. package/es/image.d.ts +0 -14
  189. package/es/typeUtils.d.ts +0 -45
  190. package/lib/_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js +0 -27
  191. package/lib/_virtual/_@oxc-project_runtime@0.122.0/helpers/defineProperty.js +0 -12
  192. package/lib/_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js +0 -25
  193. package/lib/_virtual/_@oxc-project_runtime@0.122.0/helpers/toPrimitive.js +0 -14
  194. package/lib/_virtual/_@oxc-project_runtime@0.122.0/helpers/toPropertyKey.js +0 -9
  195. package/lib/_virtual/_@oxc-project_runtime@0.122.0/helpers/typeof.js +0 -16
  196. package/lib/_virtual/_rolldown/runtime.js +0 -23
  197. package/lib/date.d.ts +0 -31
  198. package/lib/document.d.ts +0 -16
  199. package/lib/image.d.ts +0 -14
  200. package/lib/typeUtils.d.ts +0 -45
@@ -1,72 +1,129 @@
1
- import { validateValue } from "../utils/validation.js";
2
- import { useCallback, useState } from "react";
3
- //#region src/number-input/hooks/useNumberInputState.ts
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";
4
9
  /**
5
- * 数值输入状态管理 Hook
6
- *
7
- * 管理组件的内部状态,包括:
8
- * - 值管理(受控/非受控模式)
9
- * - 校验状态管理
10
- * - 事件处理函数封装
11
- *
12
- * 支持受控和非受控两种模式:
13
- * - 受控模式:value 由外部传入,组件不维护内部状态
14
- * - 非受控模式:使用 defaultValue,组件维护内部状态
15
- *
16
- * @param props 组件 Props(包含校验配置)
17
- * @returns 数值输入状态对象
18
- *
19
- * @example
20
- * // 受控模式
21
- * const state = useNumberInputState({ value: 10, onChange: (v) => console.log(v) });
22
- *
23
- * @example
24
- * // 非受控模式
25
- * const state = useNumberInputState({ defaultValue: 10, min: 0, max: 100 });
26
- */
27
- function useNumberInputState(props) {
28
- var _props$defaultValue;
29
- const isControlled = props.value !== void 0;
30
- const [innerValue, setInnerValue] = useState((_props$defaultValue = props.defaultValue) !== null && _props$defaultValue !== void 0 ? _props$defaultValue : null);
31
- const [isTouched, setIsTouched] = useState(false);
32
- const [isValid, setIsValid] = useState(true);
33
- const [errors, setErrors] = useState([]);
34
- const currentValue = isControlled ? props.value : innerValue;
35
- return {
36
- value: currentValue,
37
- isTouched,
38
- isValid,
39
- errors,
40
- handleChange: useCallback((newValue) => {
41
- var _props$onChange;
42
- if (!isControlled) setInnerValue(newValue);
43
- if (props.validateTrigger === "onChange" || props.validateTrigger === "both") {
44
- var _props$onValidate;
45
- const validation = validateValue(newValue, props);
46
- setIsValid(validation.isValid);
47
- setErrors(validation.errors);
48
- (_props$onValidate = props.onValidate) === null || _props$onValidate === void 0 || _props$onValidate.call(props, validation.isValid, validation.errors);
49
- }
50
- (_props$onChange = props.onChange) === null || _props$onChange === void 0 || _props$onChange.call(props, newValue);
51
- }, [isControlled, props]),
52
- handleBlur: useCallback(() => {
53
- var _props$onBlur;
54
- setIsTouched(true);
55
- if (props.validateTrigger !== "onChange") {
56
- var _props$onValidate2;
57
- const validation = validateValue(currentValue, props);
58
- setIsValid(validation.isValid);
59
- setErrors(validation.errors);
60
- (_props$onValidate2 = props.onValidate) === null || _props$onValidate2 === void 0 || _props$onValidate2.call(props, validation.isValid, validation.errors);
61
- }
62
- (_props$onBlur = props.onBlur) === null || _props$onBlur === void 0 || _props$onBlur.call(props, currentValue !== null && currentValue !== void 0 ? currentValue : null);
63
- }, [currentValue, props]),
64
- handleFocus: useCallback(() => {
65
- var _props$onFocus;
66
- setIsTouched(true);
67
- (_props$onFocus = props.onFocus) === null || _props$onFocus === void 0 || _props$onFocus.call(props);
68
- }, [props])
69
- };
70
- }
71
- //#endregion
72
- export { useNumberInputState };
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
+ // 格式化相关类型
@@ -1,96 +1,96 @@
1
- import { DisplayState, ValidationResult } from "../common/types.js";
2
-
3
- //#region src/number-input/types.d.ts
1
+ /**
2
+ * 数值输入组件公共类型定义
3
+ * 供 PisellNumber、PisellCurrency、PisellPercent 等组件使用
4
+ */
5
+ export type { DisplayState, ValidationResult } from '../common';
4
6
  /**
5
7
  * 数值输入状态 Hook 返回值
6
8
  */
7
- interface NumberInputState {
8
- /**
9
- * 当前值
10
- */
11
- value: number | null | undefined;
12
- /**
13
- * 是否已被触摸(交互过)
14
- */
15
- isTouched: boolean;
16
- /**
17
- * 是否校验通过
18
- */
19
- isValid: boolean;
20
- /**
21
- * 错误信息数组
22
- */
23
- errors: string[];
24
- /**
25
- * 值变更处理函数
26
- */
27
- handleChange: (value: number | null) => void;
28
- /**
29
- * 失焦处理函数
30
- */
31
- handleBlur: () => void;
32
- /**
33
- * 聚焦处理函数
34
- */
35
- handleFocus: () => void;
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;
36
38
  }
37
39
  /**
38
40
  * 基础数值输入 Props(供校验和状态管理使用)
39
41
  */
40
- interface BaseNumberInputProps {
41
- /**
42
- * 当前值(受控)
43
- */
44
- value?: number | null;
45
- /**
46
- * 默认值(非受控)
47
- */
48
- defaultValue?: number | null;
49
- /**
50
- * 是否必填
51
- */
52
- required?: boolean;
53
- /**
54
- * 最小值
55
- */
56
- min?: number;
57
- /**
58
- * 最大值
59
- */
60
- max?: number;
61
- /**
62
- * 自定义校验函数
63
- * @param value 当前值
64
- * @returns true 表示通过,false 或错误信息字符串表示不通过
65
- */
66
- validator?: (value: number | null) => boolean | string;
67
- /**
68
- * 校验触发时机
69
- * - onChange: 输入时即时校验
70
- * - onBlur: 失焦时校验
71
- * - both: 输入和失焦时都校验
72
- */
73
- validateTrigger?: 'onChange' | 'onBlur' | 'both';
74
- /**
75
- * 自定义错误提示信息
76
- */
77
- errorMessage?: string;
78
- /**
79
- * 值变更回调
80
- */
81
- onChange?: (value: number | null) => void;
82
- /**
83
- * 失焦回调
84
- */
85
- onBlur?: (value: number | null) => void;
86
- /**
87
- * 聚焦回调
88
- */
89
- onFocus?: () => void;
90
- /**
91
- * 校验回调
92
- */
93
- onValidate?: (isValid: boolean, errors: string[]) => void;
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;
94
96
  }
95
- //#endregion
96
- export { BaseNumberInputProps, NumberInputState };
@@ -0,0 +1 @@
1
+ export {};
@@ -1,17 +1,16 @@
1
- //#region src/number-input/utils/formatter.d.ts
2
1
  /**
3
2
  * 数值格式化工具函数
4
3
  */
5
4
  /**
6
5
  * 格式化选项
7
6
  */
8
- interface NumberFormatOptions {
9
- /** 小数精度 */
10
- precision?: number;
11
- /** 是否使用千分位分隔符 */
12
- useGrouping?: boolean;
13
- /** 自定义格式化函数 */
14
- formatter?: (value: number | null) => string;
7
+ export interface NumberFormatOptions {
8
+ /** 小数精度 */
9
+ precision?: number;
10
+ /** 是否使用千分位分隔符 */
11
+ useGrouping?: boolean;
12
+ /** 自定义格式化函数 */
13
+ formatter?: (value: number | null) => string;
15
14
  }
16
15
  /**
17
16
  * 格式化数值
@@ -28,7 +27,7 @@ interface NumberFormatOptions {
28
27
  * formatNumber(1234, { precision: 0, useGrouping: true })
29
28
  * // 返回: "1,234"
30
29
  */
31
- declare function formatNumber(value: number | null | undefined, options?: NumberFormatOptions): string | null;
30
+ export declare function formatNumber(value: number | null | undefined, options?: NumberFormatOptions): string | null;
32
31
  /**
33
32
  * 添加千分位分隔符
34
33
  *
@@ -39,7 +38,7 @@ declare function formatNumber(value: number | null | undefined, options?: Number
39
38
  * addThousandsSeparator(1234567)
40
39
  * // 返回: "1,234,567"
41
40
  */
42
- declare function addThousandsSeparator(value: number): string;
41
+ export declare function addThousandsSeparator(value: number): string;
43
42
  /**
44
43
  * 格式化精度
45
44
  *
@@ -51,6 +50,4 @@ declare function addThousandsSeparator(value: number): string;
51
50
  * formatPrecision(1.2345, 2)
52
51
  * // 返回: 1.23
53
52
  */
54
- declare function formatPrecision(value: number, precision: number): number;
55
- //#endregion
56
- export { NumberFormatOptions, addThousandsSeparator, formatNumber, formatPrecision };
53
+ export declare function formatPrecision(value: number, precision: number): number;
@@ -1,56 +1,82 @@
1
- //#region src/number-input/utils/formatter.ts
2
1
  /**
3
- * 格式化数值
4
- *
5
- * @param value 数值
6
- * @param options 格式化选项
7
- * @returns 格式化后的字符串,null 时返回 null
8
- *
9
- * @example
10
- * formatNumber(1234.567, { precision: 2, useGrouping: true })
11
- * // 返回: "1,234.57"
12
- *
13
- * @example
14
- * formatNumber(1234, { precision: 0, useGrouping: true })
15
- * // 返回: "1,234"
16
- */
17
- function formatNumber(value, options = {}) {
18
- if (value === null || value === void 0) return null;
19
- const { precision, useGrouping = false, formatter } = options;
20
- if (formatter) return formatter(value);
21
- if (useGrouping || precision !== void 0) return value.toLocaleString("zh-CN", {
22
- minimumFractionDigits: precision,
23
- maximumFractionDigits: precision,
24
- useGrouping
25
- });
26
- return String(value);
27
- }
2
+ * 数值格式化工具函数
3
+ */
4
+
5
+ /**
6
+ * 格式化选项
7
+ */
8
+
28
9
  /**
29
- * 添加千分位分隔符
30
- *
31
- * @param value 数值
32
- * @returns 添加千分位后的字符串
33
- *
34
- * @example
35
- * addThousandsSeparator(1234567)
36
- * // 返回: "1,234,567"
37
- */
38
- function addThousandsSeparator(value) {
39
- return value.toLocaleString("zh-CN", { useGrouping: true });
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);
40
51
  }
52
+
41
53
  /**
42
- * 格式化精度
43
- *
44
- * @param value 数值
45
- * @param precision 小数位数
46
- * @returns 格式化后的数值
47
- *
48
- * @example
49
- * formatPrecision(1.2345, 2)
50
- * // 返回: 1.23
51
- */
52
- function formatPrecision(value, precision) {
53
- return Number(value.toFixed(precision));
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
+ });
54
67
  }
55
- //#endregion
56
- export { addThousandsSeparator, formatNumber, formatPrecision };
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";
@@ -1,7 +1,4 @@
1
- import { ValidationResult } from "../../common/types.js";
2
- import { BaseNumberInputProps } from "../types.js";
3
-
4
- //#region src/number-input/utils/validation.d.ts
1
+ import type { BaseNumberInputProps, ValidationResult } from '../types';
5
2
  /**
6
3
  * 校验数值输入
7
4
  *
@@ -27,6 +24,4 @@ import { BaseNumberInputProps } from "../types.js";
27
24
  * validateValue(150, { min: 0, max: 100 })
28
25
  * // { isValid: false, errors: ['不能大于 100'] }
29
26
  */
30
- declare function validateValue(value: number | null | undefined, props: BaseNumberInputProps): ValidationResult;
31
- //#endregion
32
- export { validateValue };
27
+ export declare function validateValue(value: number | null | undefined, props: BaseNumberInputProps): ValidationResult;