utils.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. function getColumnValue(columnItem) {
  2. if (typeof columnItem === 'object')
  3. return columnItem.value;
  4. return columnItem;
  5. }
  6. export function getStrictMatchedItemByValue(columns, value, single) {
  7. if (single) {
  8. value = [value];
  9. }
  10. var matchedValues = [];
  11. var matchedColumn = [];
  12. var index = null;
  13. var _loop_1 = function (i) {
  14. var column = columns[i];
  15. var compareValue = value[i];
  16. index = column.findIndex(function (c) {
  17. var columnValue = getColumnValue(c);
  18. return columnValue === compareValue;
  19. });
  20. matchedColumn[i] = column[index];
  21. matchedValues[i] = getColumnValue(column[index]);
  22. };
  23. for (var i = 0; i < columns.length; i++) {
  24. _loop_1(i);
  25. }
  26. return {
  27. matchedColumn: single ? matchedColumn === null || matchedColumn === void 0 ? void 0 : matchedColumn[0] : matchedColumn,
  28. matchedValues: single ? matchedValues === null || matchedValues === void 0 ? void 0 : matchedValues[0] : matchedValues,
  29. };
  30. }
  31. // 如果找不到value对应的item项目,返回第一项
  32. export function getMatchedItemByValue(columns, value, singleRef) {
  33. if (singleRef.current) {
  34. value = [value];
  35. }
  36. var matchedValues = [];
  37. var matchedColumn = [];
  38. var index = null;
  39. var _loop_2 = function (i) {
  40. var column = columns[i];
  41. var compareValue = value[i];
  42. if (compareValue === undefined || compareValue === null) {
  43. index = 0;
  44. }
  45. else {
  46. index = column.findIndex(function (c) {
  47. var columnValue = getColumnValue(c);
  48. return columnValue === compareValue;
  49. });
  50. if (index === -1) {
  51. index = 0;
  52. } // 没有找到, 默认选择第一个
  53. }
  54. matchedColumn[i] = column[index];
  55. matchedValues[i] = getColumnValue(column[index]);
  56. };
  57. for (var i = 0; i < columns.length; i++) {
  58. _loop_2(i);
  59. }
  60. return {
  61. matchedColumn: singleRef.current ? matchedColumn[0] : matchedColumn,
  62. matchedValues: singleRef.current ? matchedValues[0] : matchedValues,
  63. };
  64. }
  65. export function getMatchedItemByIndex(columns, selectedIndex, single) {
  66. var _a;
  67. var matchedValues = [];
  68. var matchedColumn = [];
  69. var index = null;
  70. for (var i = 0; i < columns.length; i++) {
  71. var column = columns[i];
  72. var compareValue = selectedIndex[i];
  73. index = null;
  74. if (compareValue === undefined || compareValue === null) {
  75. index = 0;
  76. }
  77. else {
  78. index = compareValue;
  79. // 当column变化时, picker-view onChange 里抛出来的selectedIndex有可能不正确
  80. if (((_a = columns === null || columns === void 0 ? void 0 : columns[i]) === null || _a === void 0 ? void 0 : _a[compareValue]) === undefined) {
  81. index = 0;
  82. }
  83. if (index === -1) {
  84. index = 0;
  85. } // 没有找到, 默认选择第一个
  86. }
  87. matchedColumn[i] = column[index];
  88. matchedValues[i] = getColumnValue(column[index]);
  89. }
  90. return {
  91. matchedColumn: single.current ? matchedColumn[0] : matchedColumn,
  92. matchedValues: single.current ? matchedValues[0] : matchedValues,
  93. };
  94. }
  95. export function getterColumns(options, singleRef) {
  96. var columns = [];
  97. if (options.length > 0) {
  98. if (options.every(function (item) { return Array.isArray(item); })) {
  99. singleRef.current = false;
  100. columns = options.slice();
  101. }
  102. else {
  103. singleRef.current = true;
  104. columns = [options];
  105. }
  106. }
  107. return columns;
  108. }
  109. export function defaultFormat(value, column) {
  110. if (Array.isArray(column)) {
  111. return column
  112. .filter(function (c) { return c !== undefined; })
  113. .map(function (c) {
  114. if (typeof c === 'object') {
  115. return c.label;
  116. }
  117. return c;
  118. })
  119. .join('-');
  120. }
  121. return (column && column.label) || column || '';
  122. }
  123. export function getterFormatText(columns, realValue, onFormat, singleRef) {
  124. var matchedColumn = getStrictMatchedItemByValue(columns, realValue, singleRef.current).matchedColumn;
  125. if (typeof onFormat === 'function') {
  126. var formatValueByProps = onFormat(realValue, matchedColumn);
  127. if (formatValueByProps !== undefined) {
  128. return formatValueByProps;
  129. }
  130. }
  131. return defaultFormat(realValue, matchedColumn);
  132. }
  133. export function getterSelectedIndex(columns, realValue, sinefileRef) {
  134. var selectedIndex = [];
  135. var value = realValue;
  136. if (sinefileRef.current) {
  137. value = [realValue];
  138. }
  139. var _loop_3 = function (i) {
  140. var column = columns[i];
  141. var compareValue = value[i];
  142. if (compareValue === undefined || compareValue === null) {
  143. selectedIndex[i] = 0;
  144. }
  145. var index = column.findIndex(function (c) {
  146. return c === compareValue || c.value === compareValue;
  147. });
  148. if (index === -1) {
  149. index = 0;
  150. }
  151. selectedIndex[i] = index;
  152. };
  153. for (var i = 0; i < columns.length; i++) {
  154. _loop_3(i);
  155. }
  156. return selectedIndex;
  157. }