@pisell/utils 1.0.55 → 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.
- package/es/common/index.d.ts +5 -0
- package/es/common/index.js +5 -0
- package/es/common/stateHelper.d.ts +18 -0
- package/es/common/stateHelper.js +25 -0
- package/es/common/types.d.ts +28 -0
- package/es/common/types.js +1 -0
- package/es/format.js +6 -2
- package/es/index.d.ts +4 -0
- package/es/index.js +4 -0
- package/es/miniRedux.js +3 -1
- 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 +25 -0
- package/es/number-input/hooks/useNumberInputState.js +129 -0
- package/es/number-input/index.d.ts +14 -0
- package/es/number-input/index.js +21 -0
- package/es/number-input/types.d.ts +96 -0
- package/es/number-input/types.js +1 -0
- package/es/number-input/utils/formatter.d.ts +53 -0
- package/es/number-input/utils/formatter.js +82 -0
- 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 +27 -0
- package/es/number-input/utils/validation.js +67 -0
- package/es/otherUtils.js +15 -6
- package/es/select/hooks/index.d.ts +5 -0
- package/es/select/hooks/index.js +6 -0
- package/es/select/hooks/useDebouncedSearch.d.ts +20 -0
- package/es/select/hooks/useDebouncedSearch.js +55 -0
- package/es/select/hooks/useSelectState.d.ts +19 -0
- package/es/select/hooks/useSelectState.js +117 -0
- package/es/select/index.d.ts +8 -0
- package/es/select/index.js +13 -0
- package/es/select/types.d.ts +256 -0
- package/es/select/types.js +1 -0
- package/es/select/utils/filterOptions.d.ts +22 -0
- package/es/select/utils/filterOptions.js +34 -0
- package/es/select/utils/helpers.d.ts +42 -0
- package/es/select/utils/helpers.js +61 -0
- package/es/select/utils/index.d.ts +7 -0
- package/es/select/utils/index.js +8 -0
- package/es/select/utils/sortOptions.d.ts +12 -0
- package/es/select/utils/sortOptions.js +32 -0
- package/es/select/utils/tagAggregation.d.ts +25 -0
- package/es/select/utils/tagAggregation.js +48 -0
- 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 +24 -0
- package/es/text-input/hooks/useTextInputState.js +127 -0
- package/es/text-input/index.d.ts +14 -0
- package/es/text-input/index.js +20 -0
- package/es/text-input/types.d.ts +56 -0
- 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 +23 -0
- package/es/text-input/utils/validation.js +71 -0
- package/lib/common/index.d.ts +5 -0
- package/lib/common/index.js +29 -0
- package/lib/common/stateHelper.d.ts +18 -0
- package/lib/common/stateHelper.js +34 -0
- package/lib/common/types.d.ts +28 -0
- package/lib/common/types.js +17 -0
- package/lib/format.js +8 -2
- package/lib/index.d.ts +4 -0
- package/lib/index.js +9 -1
- package/lib/miniRedux.js +7 -4
- 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 +25 -0
- package/lib/number-input/hooks/useNumberInputState.js +83 -0
- package/lib/number-input/index.d.ts +14 -0
- package/lib/number-input/index.js +41 -0
- package/lib/number-input/types.d.ts +96 -0
- package/lib/number-input/types.js +17 -0
- package/lib/number-input/utils/formatter.d.ts +53 -0
- package/lib/number-input/utils/formatter.js +55 -0
- 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 +27 -0
- package/lib/number-input/utils/validation.js +54 -0
- package/lib/otherUtils.js +9 -2
- package/lib/select/hooks/index.d.ts +5 -0
- package/lib/select/hooks/index.js +34 -0
- package/lib/select/hooks/useDebouncedSearch.d.ts +20 -0
- package/lib/select/hooks/useDebouncedSearch.js +43 -0
- package/lib/select/hooks/useSelectState.d.ts +19 -0
- package/lib/select/hooks/useSelectState.js +97 -0
- package/lib/select/index.d.ts +8 -0
- package/lib/select/index.js +53 -0
- package/lib/select/types.d.ts +256 -0
- package/lib/select/types.js +17 -0
- package/lib/select/utils/filterOptions.d.ts +22 -0
- package/lib/select/utils/filterOptions.js +41 -0
- package/lib/select/utils/helpers.d.ts +42 -0
- package/lib/select/utils/helpers.js +51 -0
- package/lib/select/utils/index.d.ts +7 -0
- package/lib/select/utils/index.js +48 -0
- package/lib/select/utils/sortOptions.d.ts +12 -0
- package/lib/select/utils/sortOptions.js +41 -0
- package/lib/select/utils/tagAggregation.d.ts +25 -0
- package/lib/select/utils/tagAggregation.js +43 -0
- 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 +24 -0
- package/lib/text-input/hooks/useTextInputState.js +81 -0
- package/lib/text-input/index.d.ts +14 -0
- package/lib/text-input/index.js +35 -0
- package/lib/text-input/types.d.ts +56 -0
- 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 +23 -0
- package/lib/text-input/utils/validation.js +60 -0
- package/package.json +2 -1
- package/es/constants/index.d.ts +0 -7
- package/es/format.d.ts +0 -28
- package/es/jsBridge/index.d.ts +0 -22
- package/es/jsBridge/types.d.ts +0 -149
- package/es/locales.d.ts +0 -12
- package/es/miniRedux.d.ts +0 -16
- package/es/otherUtils.d.ts +0 -48
- package/lib/constants/index.d.ts +0 -7
- package/lib/format.d.ts +0 -28
- package/lib/jsBridge/index.d.ts +0 -22
- package/lib/jsBridge/types.d.ts +0 -149
- package/lib/locales.d.ts +0 -12
- package/lib/miniRedux.d.ts +0 -16
- package/lib/otherUtils.d.ts +0 -48
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Select 组件辅助函数
|
|
3
|
+
*/
|
|
4
|
+
import type { SelectOption } from '../types';
|
|
5
|
+
/**
|
|
6
|
+
* 根据值查找选项对象
|
|
7
|
+
* @template T 选项值的类型
|
|
8
|
+
* @param value 选项值
|
|
9
|
+
* @param options 选项列表
|
|
10
|
+
* @returns 找到的选项对象,如果没找到返回 undefined
|
|
11
|
+
*/
|
|
12
|
+
export declare function findOptionByValue<T = string | number>(value: T, options: SelectOption<T>[]): SelectOption<T> | undefined;
|
|
13
|
+
/**
|
|
14
|
+
* 根据值数组查找选项对象数组
|
|
15
|
+
* @template T 选项值的类型
|
|
16
|
+
* @param values 选项值数组
|
|
17
|
+
* @param options 选项列表
|
|
18
|
+
* @returns 找到的选项对象数组
|
|
19
|
+
*/
|
|
20
|
+
export declare function findOptionsByValues<T = string | number>(values: T[], options: SelectOption<T>[]): SelectOption<T>[];
|
|
21
|
+
/**
|
|
22
|
+
* 判断选项是否被选中(单选)
|
|
23
|
+
* @template T 选项值的类型
|
|
24
|
+
* @param optionValue 选项值
|
|
25
|
+
* @param selectedValue 当前选中值
|
|
26
|
+
* @returns 是否选中
|
|
27
|
+
*/
|
|
28
|
+
export declare function isOptionSelected<T = string | number>(optionValue: T, selectedValue: T | null | undefined): boolean;
|
|
29
|
+
/**
|
|
30
|
+
* 判断选项是否被选中(多选)
|
|
31
|
+
* @template T 选项值的类型
|
|
32
|
+
* @param optionValue 选项值
|
|
33
|
+
* @param selectedValues 当前选中值数组
|
|
34
|
+
* @returns 是否选中
|
|
35
|
+
*/
|
|
36
|
+
export declare function isOptionSelectedInMultiple<T = string | number>(optionValue: T, selectedValues: T[]): boolean;
|
|
37
|
+
/**
|
|
38
|
+
* 生成唯一 ID
|
|
39
|
+
* @param prefix ID 前缀
|
|
40
|
+
* @returns 唯一 ID
|
|
41
|
+
*/
|
|
42
|
+
export declare function generateId(prefix?: string): string;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Select 组件辅助函数
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 根据值查找选项对象
|
|
7
|
+
* @template T 选项值的类型
|
|
8
|
+
* @param value 选项值
|
|
9
|
+
* @param options 选项列表
|
|
10
|
+
* @returns 找到的选项对象,如果没找到返回 undefined
|
|
11
|
+
*/
|
|
12
|
+
export function findOptionByValue(value, options) {
|
|
13
|
+
return options.find(function (opt) {
|
|
14
|
+
return opt.value === value;
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* 根据值数组查找选项对象数组
|
|
20
|
+
* @template T 选项值的类型
|
|
21
|
+
* @param values 选项值数组
|
|
22
|
+
* @param options 选项列表
|
|
23
|
+
* @returns 找到的选项对象数组
|
|
24
|
+
*/
|
|
25
|
+
export function findOptionsByValues(values, options) {
|
|
26
|
+
return values.map(function (val) {
|
|
27
|
+
return findOptionByValue(val, options);
|
|
28
|
+
}).filter(Boolean);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 判断选项是否被选中(单选)
|
|
33
|
+
* @template T 选项值的类型
|
|
34
|
+
* @param optionValue 选项值
|
|
35
|
+
* @param selectedValue 当前选中值
|
|
36
|
+
* @returns 是否选中
|
|
37
|
+
*/
|
|
38
|
+
export function isOptionSelected(optionValue, selectedValue) {
|
|
39
|
+
return selectedValue !== null && selectedValue !== undefined && optionValue === selectedValue;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* 判断选项是否被选中(多选)
|
|
44
|
+
* @template T 选项值的类型
|
|
45
|
+
* @param optionValue 选项值
|
|
46
|
+
* @param selectedValues 当前选中值数组
|
|
47
|
+
* @returns 是否选中
|
|
48
|
+
*/
|
|
49
|
+
export function isOptionSelectedInMultiple(optionValue, selectedValues) {
|
|
50
|
+
return selectedValues.includes(optionValue);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* 生成唯一 ID
|
|
55
|
+
* @param prefix ID 前缀
|
|
56
|
+
* @returns 唯一 ID
|
|
57
|
+
*/
|
|
58
|
+
export function generateId() {
|
|
59
|
+
var prefix = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'select';
|
|
60
|
+
return "".concat(prefix, "-").concat(Math.random().toString(36).substr(2, 9));
|
|
61
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Select 工具函数导出
|
|
3
|
+
*/
|
|
4
|
+
export { calculateTagDisplay } from './tagAggregation';
|
|
5
|
+
export { filterOptions, defaultFilterOption } from './filterOptions';
|
|
6
|
+
export { sortOptions } from './sortOptions';
|
|
7
|
+
export { findOptionByValue, findOptionsByValues, isOptionSelected, isOptionSelectedInMultiple, generateId, } from './helpers';
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Select 工具函数导出
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export { calculateTagDisplay } from "./tagAggregation";
|
|
6
|
+
export { filterOptions, defaultFilterOption } from "./filterOptions";
|
|
7
|
+
export { sortOptions } from "./sortOptions";
|
|
8
|
+
export { findOptionByValue, findOptionsByValues, isOptionSelected, isOptionSelectedInMultiple, generateId } from "./helpers";
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 选项排序工具函数
|
|
3
|
+
*/
|
|
4
|
+
import type { SelectOption, SortDirection } from '../types';
|
|
5
|
+
/**
|
|
6
|
+
* 排序选项列表
|
|
7
|
+
* @template T 选项值的类型
|
|
8
|
+
* @param options 原始选项列表
|
|
9
|
+
* @param direction 排序方向('asc' 升序 | 'desc' 降序)
|
|
10
|
+
* @returns 排序后的选项列表(新数组)
|
|
11
|
+
*/
|
|
12
|
+
export declare function sortOptions<T = string | number>(options: SelectOption<T>[], direction?: SortDirection): SelectOption<T>[];
|
|
@@ -0,0 +1,32 @@
|
|
|
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 _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 _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
5
|
+
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
6
|
+
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; }
|
|
7
|
+
/**
|
|
8
|
+
* 选项排序工具函数
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 排序选项列表
|
|
13
|
+
* @template T 选项值的类型
|
|
14
|
+
* @param options 原始选项列表
|
|
15
|
+
* @param direction 排序方向('asc' 升序 | 'desc' 降序)
|
|
16
|
+
* @returns 排序后的选项列表(新数组)
|
|
17
|
+
*/
|
|
18
|
+
export function sortOptions(options) {
|
|
19
|
+
var direction = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'asc';
|
|
20
|
+
// 创建副本避免修改原数组
|
|
21
|
+
var sortedOptions = _toConsumableArray(options);
|
|
22
|
+
sortedOptions.sort(function (a, b) {
|
|
23
|
+
var labelA = a.label.toLowerCase();
|
|
24
|
+
var labelB = b.label.toLowerCase();
|
|
25
|
+
if (direction === 'asc') {
|
|
26
|
+
return labelA.localeCompare(labelB);
|
|
27
|
+
} else {
|
|
28
|
+
return labelB.localeCompare(labelA);
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
return sortedOptions;
|
|
32
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 标签聚合工具函数
|
|
3
|
+
* 用于多选下拉组件的标签显示逻辑
|
|
4
|
+
*/
|
|
5
|
+
import type { SelectOption, TagDisplayInfo } from '../types';
|
|
6
|
+
/**
|
|
7
|
+
* 计算需要显示的标签和聚合信息
|
|
8
|
+
* @template T 选项值的类型
|
|
9
|
+
* @param selectedValues 当前选中的值数组
|
|
10
|
+
* @param options 所有选项列表
|
|
11
|
+
* @param maxTagCount 最多显示的标签数量
|
|
12
|
+
* @returns 标签显示信息
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```tsx
|
|
16
|
+
* const tagInfo = calculateTagDisplay(['a', 'b', 'c', 'd'], options, 2);
|
|
17
|
+
* // 返回:
|
|
18
|
+
* // {
|
|
19
|
+
* // visibleTags: [option_a, option_b],
|
|
20
|
+
* // hiddenTags: [option_c, option_d],
|
|
21
|
+
* // hiddenCount: 2
|
|
22
|
+
* // }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export declare function calculateTagDisplay<T = string | number>(selectedValues: T[], options: SelectOption<T>[], maxTagCount: number): TagDisplayInfo<T>;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 标签聚合工具函数
|
|
3
|
+
* 用于多选下拉组件的标签显示逻辑
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 计算需要显示的标签和聚合信息
|
|
8
|
+
* @template T 选项值的类型
|
|
9
|
+
* @param selectedValues 当前选中的值数组
|
|
10
|
+
* @param options 所有选项列表
|
|
11
|
+
* @param maxTagCount 最多显示的标签数量
|
|
12
|
+
* @returns 标签显示信息
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```tsx
|
|
16
|
+
* const tagInfo = calculateTagDisplay(['a', 'b', 'c', 'd'], options, 2);
|
|
17
|
+
* // 返回:
|
|
18
|
+
* // {
|
|
19
|
+
* // visibleTags: [option_a, option_b],
|
|
20
|
+
* // hiddenTags: [option_c, option_d],
|
|
21
|
+
* // hiddenCount: 2
|
|
22
|
+
* // }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export function calculateTagDisplay(selectedValues, options, maxTagCount) {
|
|
26
|
+
// 根据选中值找到对应的选项对象
|
|
27
|
+
var selectedOptions = selectedValues.map(function (v) {
|
|
28
|
+
return options.find(function (opt) {
|
|
29
|
+
return opt.value === v;
|
|
30
|
+
});
|
|
31
|
+
}).filter(Boolean);
|
|
32
|
+
|
|
33
|
+
// 如果选中数量不超过 maxTagCount,全部显示
|
|
34
|
+
if (selectedOptions.length <= maxTagCount) {
|
|
35
|
+
return {
|
|
36
|
+
visibleTags: selectedOptions,
|
|
37
|
+
hiddenTags: [],
|
|
38
|
+
hiddenCount: 0
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// 否则进行聚合
|
|
43
|
+
return {
|
|
44
|
+
visibleTags: selectedOptions.slice(0, maxTagCount),
|
|
45
|
+
hiddenTags: selectedOptions.slice(maxTagCount),
|
|
46
|
+
hiddenCount: selectedOptions.length - maxTagCount
|
|
47
|
+
};
|
|
48
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { BaseTextInputProps, TextInputState } 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 = useTextInputState({ value: 'hello', onChange: (v) => console.log(v) });
|
|
20
|
+
*
|
|
21
|
+
* // 非受控模式
|
|
22
|
+
* const state = useTextInputState({ defaultValue: 'hello' });
|
|
23
|
+
*/
|
|
24
|
+
export declare function useTextInputState(props: BaseTextInputProps): TextInputState;
|
|
@@ -0,0 +1,127 @@
|
|
|
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 = useTextInputState({ value: 'hello', onChange: (v) => console.log(v) });
|
|
27
|
+
*
|
|
28
|
+
* // 非受控模式
|
|
29
|
+
* const state = useTextInputState({ defaultValue: 'hello' });
|
|
30
|
+
*/
|
|
31
|
+
export function useTextInputState(props) {
|
|
32
|
+
// 判断是否为受控组件
|
|
33
|
+
var isControlled = props.value !== undefined;
|
|
34
|
+
|
|
35
|
+
// 内部状态(仅非受控模式使用)
|
|
36
|
+
var _useState = useState(props.defaultValue || ''),
|
|
37
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
38
|
+
innerValue = _useState2[0],
|
|
39
|
+
setInnerValue = _useState2[1];
|
|
40
|
+
var _useState3 = useState(false),
|
|
41
|
+
_useState4 = _slicedToArray(_useState3, 2),
|
|
42
|
+
isTouched = _useState4[0],
|
|
43
|
+
setIsTouched = _useState4[1];
|
|
44
|
+
var _useState5 = useState(true),
|
|
45
|
+
_useState6 = _slicedToArray(_useState5, 2),
|
|
46
|
+
isValid = _useState6[0],
|
|
47
|
+
setIsValid = _useState6[1];
|
|
48
|
+
var _useState7 = useState([]),
|
|
49
|
+
_useState8 = _slicedToArray(_useState7, 2),
|
|
50
|
+
errors = _useState8[0],
|
|
51
|
+
setErrors = _useState8[1];
|
|
52
|
+
|
|
53
|
+
// 当前值(受控模式优先使用 props.value)
|
|
54
|
+
var currentValue = isControlled ? props.value : innerValue;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 值变更处理函数
|
|
58
|
+
*
|
|
59
|
+
* 1. 更新内部状态(非受控模式)
|
|
60
|
+
* 2. 根据 validateTrigger 决定是否立即校验
|
|
61
|
+
* 3. 触发外部 onChange 回调
|
|
62
|
+
*/
|
|
63
|
+
var handleChange = useCallback(function (newValue) {
|
|
64
|
+
var _props$onChange;
|
|
65
|
+
// 更新内部状态(非受控模式)
|
|
66
|
+
if (!isControlled) {
|
|
67
|
+
setInnerValue(newValue);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// 根据配置决定是否立即校验
|
|
71
|
+
var shouldValidate = props.validateTrigger === 'onChange' || props.validateTrigger === 'both';
|
|
72
|
+
if (shouldValidate) {
|
|
73
|
+
var _props$onValidate;
|
|
74
|
+
var validation = validateValue(newValue, props);
|
|
75
|
+
setIsValid(validation.isValid);
|
|
76
|
+
setErrors(validation.errors);
|
|
77
|
+
(_props$onValidate = props.onValidate) === null || _props$onValidate === void 0 || _props$onValidate.call(props, validation.isValid, validation.errors);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// 触发外部回调
|
|
81
|
+
(_props$onChange = props.onChange) === null || _props$onChange === void 0 || _props$onChange.call(props, newValue);
|
|
82
|
+
}, [isControlled, props]);
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* 失焦处理函数
|
|
86
|
+
*
|
|
87
|
+
* 1. 标记为已触摸
|
|
88
|
+
* 2. 执行失焦时的校验(根据 validateTrigger)
|
|
89
|
+
* 3. 触发外部 onBlur 回调
|
|
90
|
+
*/
|
|
91
|
+
var handleBlur = useCallback(function () {
|
|
92
|
+
var _props$onBlur;
|
|
93
|
+
setIsTouched(true);
|
|
94
|
+
|
|
95
|
+
// 失焦时执行校验(如果 validateTrigger 不是 'onChange')
|
|
96
|
+
var shouldValidate = props.validateTrigger !== 'onChange';
|
|
97
|
+
if (shouldValidate) {
|
|
98
|
+
var _props$onValidate2;
|
|
99
|
+
var validation = validateValue(currentValue || '', props);
|
|
100
|
+
setIsValid(validation.isValid);
|
|
101
|
+
setErrors(validation.errors);
|
|
102
|
+
(_props$onValidate2 = props.onValidate) === null || _props$onValidate2 === void 0 || _props$onValidate2.call(props, validation.isValid, validation.errors);
|
|
103
|
+
}
|
|
104
|
+
(_props$onBlur = props.onBlur) === null || _props$onBlur === void 0 || _props$onBlur.call(props, currentValue || '');
|
|
105
|
+
}, [currentValue, props]);
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* 聚焦处理函数
|
|
109
|
+
*
|
|
110
|
+
* 1. 标记为已触摸
|
|
111
|
+
* 2. 触发外部 onFocus 回调
|
|
112
|
+
*/
|
|
113
|
+
var handleFocus = useCallback(function () {
|
|
114
|
+
var _props$onFocus;
|
|
115
|
+
setIsTouched(true);
|
|
116
|
+
(_props$onFocus = props.onFocus) === null || _props$onFocus === void 0 || _props$onFocus.call(props);
|
|
117
|
+
}, [props]);
|
|
118
|
+
return {
|
|
119
|
+
value: currentValue,
|
|
120
|
+
isTouched: isTouched,
|
|
121
|
+
isValid: isValid,
|
|
122
|
+
errors: errors,
|
|
123
|
+
handleChange: handleChange,
|
|
124
|
+
handleBlur: handleBlur,
|
|
125
|
+
handleFocus: handleFocus
|
|
126
|
+
};
|
|
127
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 文本输入组件公共逻辑导出
|
|
3
|
+
*
|
|
4
|
+
* 供以下组件使用:
|
|
5
|
+
* - PisellSingleLineText
|
|
6
|
+
* - PisellLongText
|
|
7
|
+
* - PisellPhone
|
|
8
|
+
* - PisellEmail
|
|
9
|
+
* - PisellUrl
|
|
10
|
+
*/
|
|
11
|
+
export type { DisplayState, ValidationResult, TextInputState, BaseTextInputProps, } from './types';
|
|
12
|
+
export { useTextInputState } from './hooks';
|
|
13
|
+
export { getDisplayState } from './utils';
|
|
14
|
+
export { validateValue as validateTextValue } from './utils';
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 文本输入组件公共逻辑导出
|
|
3
|
+
*
|
|
4
|
+
* 供以下组件使用:
|
|
5
|
+
* - PisellSingleLineText
|
|
6
|
+
* - PisellLongText
|
|
7
|
+
* - PisellPhone
|
|
8
|
+
* - PisellEmail
|
|
9
|
+
* - PisellUrl
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
// 类型
|
|
13
|
+
|
|
14
|
+
// Hooks
|
|
15
|
+
export { useTextInputState } from "./hooks";
|
|
16
|
+
|
|
17
|
+
// 工具函数
|
|
18
|
+
export { getDisplayState } from "./utils";
|
|
19
|
+
// 使用别名导出避免与 number-input 的 validateValue 重名
|
|
20
|
+
export { validateValue as validateTextValue } from "./utils";
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 文本输入组件公共类型定义
|
|
3
|
+
* 供 PisellSingleLineText、PisellLongText、PisellPhone、PisellEmail、PisellUrl 等组件使用
|
|
4
|
+
*/
|
|
5
|
+
export type { DisplayState, ValidationResult } from '../common';
|
|
6
|
+
/**
|
|
7
|
+
* 文本输入状态 Hook 返回值
|
|
8
|
+
*/
|
|
9
|
+
export interface TextInputState {
|
|
10
|
+
/**
|
|
11
|
+
* 当前值
|
|
12
|
+
*/
|
|
13
|
+
value: string | 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: string) => void;
|
|
30
|
+
/**
|
|
31
|
+
* 失焦处理函数
|
|
32
|
+
*/
|
|
33
|
+
handleBlur: () => void;
|
|
34
|
+
/**
|
|
35
|
+
* 聚焦处理函数
|
|
36
|
+
*/
|
|
37
|
+
handleFocus: () => void;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* 基础文本输入 Props(供校验和状态管理使用)
|
|
41
|
+
*/
|
|
42
|
+
export interface BaseTextInputProps {
|
|
43
|
+
value?: string;
|
|
44
|
+
defaultValue?: string;
|
|
45
|
+
required?: boolean;
|
|
46
|
+
minLength?: number;
|
|
47
|
+
maxLength?: number;
|
|
48
|
+
pattern?: string | RegExp;
|
|
49
|
+
validator?: (value: string) => boolean | string;
|
|
50
|
+
validateTrigger?: 'onChange' | 'onBlur' | 'both';
|
|
51
|
+
errorMessage?: string;
|
|
52
|
+
onChange?: (value: string) => void;
|
|
53
|
+
onBlur?: (value: string) => void;
|
|
54
|
+
onFocus?: () => void;
|
|
55
|
+
onValidate?: (isValid: boolean, errors: string[]) => void;
|
|
56
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { BaseTextInputProps, ValidationResult } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* 校验文本输入值
|
|
4
|
+
*
|
|
5
|
+
* 按照优先级执行校验规则:
|
|
6
|
+
* 1. 必填校验(required)
|
|
7
|
+
* 2. 最小长度校验(minLength)
|
|
8
|
+
* 3. 最大长度校验(maxLength)
|
|
9
|
+
* 4. 正则校验(pattern)
|
|
10
|
+
* 5. 自定义校验函数(validator)
|
|
11
|
+
*
|
|
12
|
+
* @param value 待校验的值
|
|
13
|
+
* @param props 校验配置
|
|
14
|
+
* @returns 校验结果对象,包含 isValid 和 errors
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* validateValue('hello', { required: true, minLength: 3 })
|
|
18
|
+
* // { isValid: true, errors: [] }
|
|
19
|
+
*
|
|
20
|
+
* validateValue('', { required: true })
|
|
21
|
+
* // { isValid: false, errors: ['此字段为必填项'] }
|
|
22
|
+
*/
|
|
23
|
+
export declare function validateValue(value: string, props: BaseTextInputProps): ValidationResult;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 校验文本输入值
|
|
3
|
+
*
|
|
4
|
+
* 按照优先级执行校验规则:
|
|
5
|
+
* 1. 必填校验(required)
|
|
6
|
+
* 2. 最小长度校验(minLength)
|
|
7
|
+
* 3. 最大长度校验(maxLength)
|
|
8
|
+
* 4. 正则校验(pattern)
|
|
9
|
+
* 5. 自定义校验函数(validator)
|
|
10
|
+
*
|
|
11
|
+
* @param value 待校验的值
|
|
12
|
+
* @param props 校验配置
|
|
13
|
+
* @returns 校验结果对象,包含 isValid 和 errors
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* validateValue('hello', { required: true, minLength: 3 })
|
|
17
|
+
* // { isValid: true, errors: [] }
|
|
18
|
+
*
|
|
19
|
+
* validateValue('', { required: true })
|
|
20
|
+
* // { isValid: false, errors: ['此字段为必填项'] }
|
|
21
|
+
*/
|
|
22
|
+
export function validateValue(value, props) {
|
|
23
|
+
var errors = [];
|
|
24
|
+
|
|
25
|
+
// 1. 必填校验
|
|
26
|
+
if (props.required && !(value !== null && value !== void 0 && value.trim())) {
|
|
27
|
+
errors.push(props.errorMessage || '此字段为必填项');
|
|
28
|
+
return {
|
|
29
|
+
isValid: false,
|
|
30
|
+
errors: errors
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// 如果值为空且非必填,直接返回有效
|
|
35
|
+
if (!value) {
|
|
36
|
+
return {
|
|
37
|
+
isValid: true,
|
|
38
|
+
errors: errors
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// 2. 最小长度校验
|
|
43
|
+
if (props.minLength !== undefined && value.length < props.minLength) {
|
|
44
|
+
errors.push("\u6700\u5C11\u9700\u8981 ".concat(props.minLength, " \u4E2A\u5B57\u7B26"));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// 3. 最大长度校验(通常在输入时已被 Input 组件限制,这里是兜底校验)
|
|
48
|
+
if (props.maxLength !== undefined && value.length > props.maxLength) {
|
|
49
|
+
errors.push("\u6700\u591A\u5141\u8BB8 ".concat(props.maxLength, " \u4E2A\u5B57\u7B26"));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// 4. 正则校验
|
|
53
|
+
if (props.pattern) {
|
|
54
|
+
var regex = typeof props.pattern === 'string' ? new RegExp(props.pattern) : props.pattern;
|
|
55
|
+
if (!regex.test(value)) {
|
|
56
|
+
errors.push(props.errorMessage || '格式不正确');
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// 5. 自定义校验函数
|
|
61
|
+
if (props.validator) {
|
|
62
|
+
var result = props.validator(value);
|
|
63
|
+
if (result !== true) {
|
|
64
|
+
errors.push(typeof result === 'string' ? result : '校验失败');
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return {
|
|
68
|
+
isValid: errors.length === 0,
|
|
69
|
+
errors: errors
|
|
70
|
+
};
|
|
71
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
|
|
19
|
+
// src/common/index.ts
|
|
20
|
+
var common_exports = {};
|
|
21
|
+
__export(common_exports, {
|
|
22
|
+
getDisplayState: () => import_stateHelper.getDisplayState
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(common_exports);
|
|
25
|
+
var import_stateHelper = require("./stateHelper");
|
|
26
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
27
|
+
0 && (module.exports = {
|
|
28
|
+
getDisplayState
|
|
29
|
+
});
|
|
@@ -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;
|