utils.uts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // @ts-nocheck
  2. import { clamp } from '@/uni_modules/lime-shared/clamp'
  3. import { PickerValue, PickerColumn, PickerColumnItem, PickerPickEvent } from '../l-picker/type';
  4. import { KeysType } from './type';
  5. // #ifndef UNI-APP-X
  6. import type { Ref } from '@/uni_modules/lime-shared/vue'
  7. type UTSJSONObject = Record<string, any>
  8. // #endif
  9. /**
  10. * 解析键配置对象,生成用于访问选项属性的键映射。
  11. *
  12. * @param {UTSJSONObject|null} keys - 配置对象,包含 label、value 和 children 的键名。
  13. * @returns {KeysType} 包含 label、value 和 children 键名的映射对象。
  14. */
  15. export function parseKeys(keys : UTSJSONObject | null) : KeysType {
  16. const _labelKey = `${keys?.['label'] ?? 'label'}`
  17. const _valueKey = `${keys?.['value'] ?? 'value'}`
  18. const _childrenKey = `${keys?.['children'] ?? 'children'}`
  19. return {
  20. label: _labelKey,
  21. value: _valueKey,
  22. children: _childrenKey,
  23. } as KeysType
  24. }
  25. /**
  26. * 获取选项数组中第一个未被禁用的选项。
  27. *
  28. * @param {UTSJSONObject[]} options - 选项对象数组。
  29. * @returns {UTSJSONObject|null} 第一个未被禁用的选项对象,如果所有选项都被禁用则返回第一个选项。
  30. */
  31. export const getFirstEnabledOption = (
  32. options : UTSJSONObject[],
  33. ) : UTSJSONObject | null =>
  34. options.find((option) : boolean => option['disabled'] != true) ?? options[0];
  35. /**
  36. * 在选项数组中查找指定索引之后或之前的第一个未被禁用的选项的索引。
  37. *
  38. * @param {UTSJSONObject[]} options - 选项对象数组。
  39. * @param {number} index - 当前索引位置。
  40. * @returns {number} 第一个未被禁用的选项的索引,如果不存在则返回 0。
  41. */
  42. export function findIndexOfEnabledOption(
  43. options : UTSJSONObject[],
  44. index : number,
  45. ) {
  46. index = clamp(index, 0, options.length);
  47. // for (let i = index; i < options.length; i++) {
  48. // if (options[i]['disabled'] != true) return i;
  49. // }
  50. // for (let i = index - 1; i >= 0; i--) {
  51. // if (options[i]['disabled'] != true) return i;
  52. // }
  53. // return 0;
  54. return index
  55. }
  56. /**
  57. * 根据指定的值在选项数组中查找对应的选项对象。
  58. *
  59. * @param {UTSJSONObject[]} options - 选项对象数组。
  60. * @param {PickerValue} value - 要查找的值。
  61. * @param {KeysType} fields - 包含 label、value 和 children 键名的映射对象。
  62. * @returns {UTSJSONObject|null} 找到的选项对象,如果未找到则返回 null。
  63. */
  64. export function findOptionByValue(
  65. options : UTSJSONObject[],
  66. value : PickerValue,
  67. fields : KeysType,
  68. ) : UTSJSONObject | null {
  69. const index = options.findIndex((option) : boolean => option[fields.value] == value);
  70. const enabledIndex = findIndexOfEnabledOption(options, index);
  71. return options[enabledIndex];
  72. }
  73. /**
  74. * 格式化级联选择器的列数据。
  75. *
  76. * @param {UTSJSONObject[]} columns - 初始的列数据数组。
  77. * @param {KeysType} fields - 包含 label、value 和 children 键名的映射对象。
  78. * @param {Ref<PickerValue[]>} selectedValues - 当前选中的值数组。
  79. * @returns {PickerColumn[]} 格式化后的级联列数据数组。
  80. */
  81. export function formatCascadeColumns(
  82. columns : UTSJSONObject[],
  83. fields : KeysType,
  84. selectedValues : Ref<PickerValue[]>
  85. ) {
  86. const formatted : PickerColumn[] = [];
  87. let cursor : UTSJSONObject | null = {};
  88. cursor![fields.children] = columns;
  89. let columnIndex = 0;
  90. while (cursor != null && cursor[fields.children] != null) {
  91. const options : UTSJSONObject[] = cursor[fields.children]! as UTSJSONObject[];
  92. const value = selectedValues.value.length > columnIndex ? selectedValues.value[columnIndex] : null;
  93. cursor = value != null
  94. ? findOptionByValue(options, value, fields)
  95. : null;
  96. if (cursor == null && options.length > 0) {
  97. const firstValue = getFirstEnabledOption(options)![fields.value]!;
  98. cursor = findOptionByValue(options, firstValue, fields);
  99. }
  100. columnIndex++;
  101. formatted.push(
  102. options.map((option : UTSJSONObject) : PickerColumnItem => ({
  103. label: `${option['label']}`,
  104. value: `${option['value']}`,
  105. } as PickerColumnItem))
  106. );
  107. }
  108. return formatted;
  109. }