stk-table-vue 0.4.10 → 0.4.11

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/README.md CHANGED
@@ -244,6 +244,13 @@ export type StkProps = {
244
244
  * 低版本浏览器只能为'relative',
245
245
  */
246
246
  cellFixedMode?: 'sticky' | 'relative';
247
+ /**
248
+ * 是否平滑滚动
249
+ * - default: chrome < 85 ? true : false
250
+ * - false: 使用 wheel 事件滚动。为了防止滚动过快导致白屏。
251
+ * - true: 不使用 wheel 事件滚动。滚轮滚动时更加平滑。滚动过快时会白屏。
252
+ */
253
+ smoothScroll?: boolean;
247
254
  };
248
255
  ```
249
256
 
@@ -5,7 +5,7 @@ type DT = any;
5
5
  /**
6
6
  * 选中一行,
7
7
  * @param {string} rowKey selected rowKey, undefined to unselect
8
- * @param {boolean} option.silent 是否触发回调
8
+ * @param {boolean} option.silent if emit current-change. default:false(not emit `current-change`)
9
9
  */
10
10
  declare function setCurrentRow(rowKey: string | undefined, option?: {
11
11
  silent: boolean;
@@ -143,6 +143,12 @@ declare const _default: __VLS_WithTemplateSlots<import('vue').DefineComponent<__
143
143
  * 低版本浏览器强制为'relative',
144
144
  */
145
145
  cellFixedMode?: "sticky" | "relative" | undefined;
146
+ /**
147
+ * 是否平滑滚动。default: chrome < 85 ? true : false
148
+ * - false: 使用 onwheel 滚动。为了防止滚动过快导致白屏。
149
+ * - true: 不使用 onwheel 滚动。鼠标滚轮滚动时更加平滑。滚动过快时会白屏。
150
+ */
151
+ smoothScroll?: boolean | undefined;
146
152
  }>, {
147
153
  width: string;
148
154
  fixedMode: boolean;
@@ -186,6 +192,7 @@ declare const _default: __VLS_WithTemplateSlots<import('vue').DefineComponent<__
186
192
  highlightConfig: () => {};
187
193
  seqConfig: () => {};
188
194
  cellFixedMode: string;
195
+ smoothScroll: boolean;
189
196
  }>, {
190
197
  /** 初始化横向纵向虚拟滚动 */
191
198
  initVirtualScroll: (height?: number | undefined) => void;
@@ -196,11 +203,8 @@ declare const _default: __VLS_WithTemplateSlots<import('vue').DefineComponent<__
196
203
  /** 设置当前选中行 */
197
204
  setCurrentRow: typeof setCurrentRow;
198
205
  /** 设置高亮渐暗单元格 */
199
- setHighlightDimCell: (rowKeyValue: string, dataIndex: string, option?: {
200
- className?: string | undefined; /**
201
- * 排序变更触发。defaultSort.dataIndex 找不到时,col 将返回null。
202
- * ```(col: StkTableColumn<DT> | null, order: Order, data: DT[])```
203
- */
206
+ setHighlightDimCell: (rowKeyValue: import('./types/index').UniqKey, dataIndex: string, option?: {
207
+ className?: string | undefined;
204
208
  method?: "animation" | "css" | undefined;
205
209
  keyframe?: Keyframe[] | PropertyIndexedKeyframes | null | undefined;
206
210
  duration?: number | undefined;
@@ -354,6 +358,12 @@ declare const _default: __VLS_WithTemplateSlots<import('vue').DefineComponent<__
354
358
  * 低版本浏览器强制为'relative',
355
359
  */
356
360
  cellFixedMode?: "sticky" | "relative" | undefined;
361
+ /**
362
+ * 是否平滑滚动。default: chrome < 85 ? true : false
363
+ * - false: 使用 onwheel 滚动。为了防止滚动过快导致白屏。
364
+ * - true: 不使用 onwheel 滚动。鼠标滚轮滚动时更加平滑。滚动过快时会白屏。
365
+ */
366
+ smoothScroll?: boolean | undefined;
357
367
  }>, {
358
368
  width: string;
359
369
  fixedMode: boolean;
@@ -397,6 +407,7 @@ declare const _default: __VLS_WithTemplateSlots<import('vue').DefineComponent<__
397
407
  highlightConfig: () => {};
398
408
  seqConfig: () => {};
399
409
  cellFixedMode: string;
410
+ smoothScroll: boolean;
400
411
  }>>> & {
401
412
  onScroll?: ((ev: Event, data: {
402
413
  startIndex: number;
@@ -463,6 +474,7 @@ declare const _default: __VLS_WithTemplateSlots<import('vue').DefineComponent<__
463
474
  highlightConfig: HighlightConfig;
464
475
  seqConfig: SeqConfig;
465
476
  cellFixedMode: "sticky" | "relative";
477
+ smoothScroll: boolean;
466
478
  }, {}>, {
467
479
  tableHeader?(_: {
468
480
  col: StkTableColumn<any>;
@@ -23,4 +23,6 @@ export declare const HIGHLIGHT_ROW_CLASS = "highlight-row";
23
23
  export declare const HIGHLIGHT_CELL_CLASS = "highlight-cell";
24
24
  /** 低版本sticky兼容模式 */
25
25
  export declare const IS_LEGACY_MODE: boolean;
26
+ /** 默认props.smoothDefault */
27
+ export declare const DEFAULT_SMOOTH_SCROLL: boolean;
26
28
  export declare const STK_ID_PREFIX = "stk";
@@ -1,3 +1,3 @@
1
1
  export { default as StkTable } from './StkTable.vue';
2
- export { tableSort, insertToOrderedArray } from './utils';
2
+ export { tableSort, insertToOrderedArray, strCompare } from './utils';
3
3
  export type { StkTableColumn } from './types/index';
@@ -105,7 +105,7 @@ export type SortOption<T extends Record<string, any>> = Pick<StkTableColumn<T>,
105
105
  /** 排序状态 */
106
106
  export type SortState<T> = {
107
107
  dataIndex: keyof T;
108
- order: null | 'asc' | 'desc';
108
+ order: Order;
109
109
  sortType?: 'number' | 'string';
110
110
  };
111
111
  /** 唯一键 */
@@ -19,7 +19,7 @@ export declare function useHighlight({ props, stkTableId, tableContainerRef }: P
19
19
  keyframe?: Parameters<Animatable['animate']>['0'];
20
20
  duration?: number;
21
21
  }) => void;
22
- setHighlightDimCell: (rowKeyValue: string, dataIndex: string, option?: {
22
+ setHighlightDimCell: (rowKeyValue: UniqKey, dataIndex: string, option?: {
23
23
  className?: string;
24
24
  method?: 'animation' | 'css';
25
25
  keyframe?: Parameters<Animatable['animate']>['0'];
@@ -2,6 +2,8 @@ import { Order, SortConfig, SortOption, SortState, StkTableColumn } from '../typ
2
2
 
3
3
  /**
4
4
  * 对有序数组插入新数据
5
+ *
6
+ * 注意:不会改变原数组,返回新数组
5
7
  * @param sortState
6
8
  * @param sortState.dataIndex 排序的列
7
9
  * @param sortState.order 排序顺序
@@ -10,7 +12,17 @@ import { Order, SortConfig, SortOption, SortState, StkTableColumn } from '../typ
10
12
  * @param targetArray 表格数据
11
13
  * @return targetArray 的浅拷贝
12
14
  */
13
- export declare function insertToOrderedArray<T extends object>(sortState: SortState<T>, newItem: any, targetArray: T[], sortConfig?: SortConfig<T>): T[];
15
+ export declare function insertToOrderedArray<T extends object>(sortState: SortState<T>, newItem: T, targetArray: T[], sortConfig?: SortConfig<T>): T[];
16
+ /**
17
+ * 字符串比较
18
+ * @param a
19
+ * @param b
20
+ * @param type 类型
21
+ * @param isNumber 是否是数字类型
22
+ * @param localeCompare 是否 使用Array.prototyshpe.localeCompare
23
+ * @return {-1|0|1}
24
+ */
25
+ export declare function strCompare(a: string, b: string, isNumber: boolean, localeCompare?: boolean): number;
14
26
  /**
15
27
  * 表格排序抽离
16
28
  * 可以在组件外部自己实现表格排序,组件配置remote,使表格不排序。
@@ -1,4 +1,4 @@
1
- import { onMounted, onBeforeUnmount, watch, ref, shallowRef, computed, defineComponent, nextTick, toRaw, openBlock, createElementBlock, normalizeClass, unref, normalizeStyle, createCommentVNode, createElementVNode, Fragment, renderList, createBlock, resolveDynamicComponent, toDisplayString, renderSlot, createTextVNode } from "vue";
1
+ import { watch, onMounted, onBeforeUnmount, ref, shallowRef, computed, defineComponent, nextTick, toRaw, openBlock, createElementBlock, normalizeClass, unref, normalizeStyle, createCommentVNode, createElementVNode, Fragment, renderList, createBlock, resolveDynamicComponent, toDisplayString, renderSlot, createTextVNode } from "vue";
2
2
  import { interpolateRgb } from "d3-interpolate";
3
3
  function isEmptyValue(val, isNumber) {
4
4
  let isEmpty = val === null || val === "" || val === void 0;
@@ -15,11 +15,11 @@ function insertToOrderedArray(sortState, newItem, targetArray, sortConfig = {})
15
15
  sortType = typeof newItem[dataIndex];
16
16
  const isNumber = sortType === "number";
17
17
  const data = [...targetArray];
18
- if (!order) {
18
+ if (!order || !data.length) {
19
19
  data.unshift(newItem);
20
20
  return data;
21
21
  }
22
- if (sortConfig.emptyToBottom && isEmptyValue(data)) {
22
+ if (sortConfig.emptyToBottom && isEmptyValue(newItem)) {
23
23
  data.push(newItem);
24
24
  }
25
25
  let sIndex = 0;
@@ -172,6 +172,7 @@ const HIGHLIGHT_CELL_CLASS = "highlight-cell";
172
172
  const _chromeVersion = getBrowsersVersion("chrome");
173
173
  const _firefoxVersion = getBrowsersVersion("firefox");
174
174
  const IS_LEGACY_MODE = _chromeVersion < 56 || _firefoxVersion < 59;
175
+ const DEFAULT_SMOOTH_SCROLL = _chromeVersion < 85;
175
176
  const STK_ID_PREFIX = "stk";
176
177
  var TagType = /* @__PURE__ */ ((TagType2) => {
177
178
  TagType2[TagType2["TH"] = 0] = "TH";
@@ -180,13 +181,37 @@ var TagType = /* @__PURE__ */ ((TagType2) => {
180
181
  })(TagType || {});
181
182
  function useAutoResize({ tableContainerRef, initVirtualScroll, props, debounceMs }) {
182
183
  let resizeObserver = null;
184
+ let isObserved = false;
185
+ watch(
186
+ () => props.virtual,
187
+ (v) => {
188
+ if (v)
189
+ initResizeObserver();
190
+ else
191
+ removeResizeObserver();
192
+ }
193
+ );
194
+ watch(
195
+ () => props.virtualX,
196
+ (v) => {
197
+ if (v)
198
+ initResizeObserver();
199
+ else
200
+ removeResizeObserver();
201
+ }
202
+ );
183
203
  onMounted(() => {
184
- initResizeObserver();
204
+ if (props.virtual || props.virtualX) {
205
+ initResizeObserver();
206
+ }
185
207
  });
186
208
  onBeforeUnmount(() => {
187
209
  removeResizeObserver();
188
210
  });
189
211
  function initResizeObserver() {
212
+ if (isObserved) {
213
+ removeResizeObserver();
214
+ }
190
215
  if (window.ResizeObserver) {
191
216
  if (!tableContainerRef.value) {
192
217
  const watchDom = watch(
@@ -203,14 +228,18 @@ function useAutoResize({ tableContainerRef, initVirtualScroll, props, debounceMs
203
228
  } else {
204
229
  window.addEventListener("resize", resizeCallback);
205
230
  }
231
+ isObserved = true;
206
232
  }
207
233
  function removeResizeObserver() {
234
+ if (!isObserved)
235
+ return;
208
236
  if (resizeObserver) {
209
237
  resizeObserver.disconnect();
210
238
  resizeObserver = null;
211
239
  } else {
212
240
  window.removeEventListener("resize", resizeCallback);
213
241
  }
242
+ isObserved = false;
214
243
  }
215
244
  let debounceTime = 0;
216
245
  function resizeCallback() {
@@ -1194,7 +1223,8 @@ const _sfc_main = /* @__PURE__ */ defineComponent({
1194
1223
  hideHeaderTitle: { type: [Boolean, Array], default: false },
1195
1224
  highlightConfig: { default: () => ({}) },
1196
1225
  seqConfig: { default: () => ({}) },
1197
- cellFixedMode: { default: "sticky" }
1226
+ cellFixedMode: { default: "sticky" },
1227
+ smoothScroll: { type: Boolean, default: DEFAULT_SMOOTH_SCROLL }
1198
1228
  },
1199
1229
  emits: ["sort-change", "row-click", "current-change", "row-dblclick", "header-row-menu", "row-menu", "cell-click", "cell-mouseenter", "cell-mouseleave", "cell-mouseover", "header-cell-click", "scroll", "scroll-x", "col-order-change", "th-drag-start", "th-drop", "update:columns"],
1200
1230
  setup(__props, { expose: __expose, emit: __emit }) {
@@ -1546,6 +1576,9 @@ const _sfc_main = /* @__PURE__ */ defineComponent({
1546
1576
  emits("cell-mouseover", e, row, col);
1547
1577
  }
1548
1578
  function onTableWheel(e) {
1579
+ if (props.smoothScroll) {
1580
+ return;
1581
+ }
1549
1582
  if (isColResizing.value) {
1550
1583
  e.stopPropagation();
1551
1584
  return;
@@ -1611,14 +1644,15 @@ const _sfc_main = /* @__PURE__ */ defineComponent({
1611
1644
  if (rowKey === void 0) {
1612
1645
  currentRow.value = void 0;
1613
1646
  currentRowKey.value = void 0;
1647
+ } else {
1648
+ const row = dataSourceCopy.value.find((it) => rowKeyGen(it) === rowKey);
1649
+ if (!row) {
1650
+ console.warn("setCurrentRow failed.rowKey:", rowKey);
1651
+ return;
1652
+ }
1653
+ currentRow.value = row;
1654
+ currentRowKey.value = rowKey;
1614
1655
  }
1615
- const row = dataSourceCopy.value.find((it) => rowKeyGen(it) === rowKey);
1616
- if (!row) {
1617
- console.warn("setCurrentRow failed.rowKey:", rowKey);
1618
- return;
1619
- }
1620
- currentRow.value = row;
1621
- currentRowKey.value = rowKey;
1622
1656
  if (!option.silent) {
1623
1657
  emits(
1624
1658
  "current-change",
@@ -1898,5 +1932,6 @@ const _sfc_main = /* @__PURE__ */ defineComponent({
1898
1932
  export {
1899
1933
  _sfc_main as StkTable,
1900
1934
  insertToOrderedArray,
1935
+ strCompare,
1901
1936
  tableSort
1902
1937
  };
package/lib/style.css CHANGED
@@ -170,7 +170,6 @@
170
170
  }
171
171
  .stk-table th.sorter-desc .table-header-sorter{
172
172
  display:inline;
173
- display:initial;
174
173
  }
175
174
  .stk-table th.sorter-desc .table-header-sorter .arrow-up{
176
175
  fill:var(--sort-arrow-active-sub-color);
@@ -180,7 +179,6 @@
180
179
  }
181
180
  .stk-table th.sorter-asc .table-header-sorter{
182
181
  display:inline;
183
- display:initial;
184
182
  }
185
183
  .stk-table th.sorter-asc .table-header-sorter .arrow-up{
186
184
  fill:var(--sort-arrow-active-color);
@@ -270,7 +268,6 @@
270
268
  }
271
269
  .stk-table .table-header-title{
272
270
  overflow:hidden;
273
- align-self:flex-start;
274
271
  }
275
272
  .stk-table .table-header-sorter{
276
273
  flex-shrink:0;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stk-table-vue",
3
- "version": "0.4.10",
3
+ "version": "0.4.11",
4
4
  "description": "Simple realtime virtual table for vue3 and vue2.7",
5
5
  "main": "./lib/stk-table-vue.js",
6
6
  "types": "./lib/src/StkTable/index.d.ts",
@@ -193,7 +193,7 @@
193
193
  * [] column.dataIndex 作为唯一键,不能重复
194
194
  */
195
195
  import { CSSProperties, computed, nextTick, onMounted, ref, shallowRef, toRaw, watch } from 'vue';
196
- import { DEFAULT_ROW_HEIGHT, IS_LEGACY_MODE } from './const';
196
+ import { DEFAULT_ROW_HEIGHT, IS_LEGACY_MODE, DEFAULT_SMOOTH_SCROLL } from './const';
197
197
  import { HighlightConfig, Order, SeqConfig, SortConfig, SortOption, SortState, StkTableColumn, TagType, UniqKeyProp } from './types/index';
198
198
  import { useAutoResize } from './useAutoResize';
199
199
  import { useColResize } from './useColResize';
@@ -317,6 +317,12 @@ const props = withDefaults(
317
317
  * 低版本浏览器强制为'relative',
318
318
  */
319
319
  cellFixedMode?: 'sticky' | 'relative';
320
+ /**
321
+ * 是否平滑滚动。default: chrome < 85 ? true : false
322
+ * - false: 使用 onwheel 滚动。为了防止滚动过快导致白屏。
323
+ * - true: 不使用 onwheel 滚动。鼠标滚轮滚动时更加平滑。滚动过快时会白屏。
324
+ */
325
+ smoothScroll?: boolean;
320
326
  }>(),
321
327
  {
322
328
  width: '',
@@ -361,6 +367,7 @@ const props = withDefaults(
361
367
  highlightConfig: () => ({}),
362
368
  seqConfig: () => ({}),
363
369
  cellFixedMode: 'sticky',
370
+ smoothScroll: DEFAULT_SMOOTH_SCROLL,
364
371
  },
365
372
  );
366
373
 
@@ -944,6 +951,9 @@ function onCellMouseOver(e: MouseEvent, row: DT, col: StkTableColumn<DT>) {
944
951
  * @param e
945
952
  */
946
953
  function onTableWheel(e: WheelEvent) {
954
+ if (props.smoothScroll) {
955
+ return;
956
+ }
947
957
  if (isColResizing.value) {
948
958
  // 正在调整列宽时,不允许用户滚动
949
959
  e.stopPropagation();
@@ -1028,21 +1038,22 @@ function onTrMouseOver(_e: MouseEvent, row: DT) {
1028
1038
  /**
1029
1039
  * 选中一行,
1030
1040
  * @param {string} rowKey selected rowKey, undefined to unselect
1031
- * @param {boolean} option.silent 是否触发回调
1041
+ * @param {boolean} option.silent if emit current-change. default:false(not emit `current-change`)
1032
1042
  */
1033
1043
  function setCurrentRow(rowKey: string | undefined, option = { silent: false }) {
1034
1044
  if (!dataSourceCopy.value.length) return;
1035
1045
  if (rowKey === void 0) {
1036
1046
  currentRow.value = void 0;
1037
1047
  currentRowKey.value = void 0;
1048
+ } else {
1049
+ const row = dataSourceCopy.value.find(it => rowKeyGen(it) === rowKey);
1050
+ if (!row) {
1051
+ console.warn('setCurrentRow failed.rowKey:', rowKey);
1052
+ return;
1053
+ }
1054
+ currentRow.value = row;
1055
+ currentRowKey.value = rowKey;
1038
1056
  }
1039
- const row = dataSourceCopy.value.find(it => rowKeyGen(it) === rowKey);
1040
- if (!row) {
1041
- console.warn('setCurrentRow failed.rowKey:', rowKey);
1042
- return;
1043
- }
1044
- currentRow.value = row;
1045
- currentRowKey.value = rowKey;
1046
1057
  if (!option.silent) {
1047
1058
  emits('current-change', /** no Event */ null, currentRow.value, { select: true });
1048
1059
  }
@@ -28,4 +28,7 @@ const _firefoxVersion = getBrowsersVersion('firefox');
28
28
  /** 低版本sticky兼容模式 */
29
29
  export const IS_LEGACY_MODE = _chromeVersion < 56 || _firefoxVersion < 59;
30
30
 
31
+ /** 默认props.smoothDefault */
32
+ export const DEFAULT_SMOOTH_SCROLL = _chromeVersion < 85;
33
+
31
34
  export const STK_ID_PREFIX = 'stk';
@@ -1,4 +1,4 @@
1
1
  export { default as StkTable } from './StkTable.vue';
2
- export { tableSort, insertToOrderedArray } from './utils';
2
+ export { tableSort, insertToOrderedArray, strCompare } from './utils';
3
3
  export type { StkTableColumn } from './types/index';
4
4
  import './style.less';
@@ -254,7 +254,7 @@
254
254
  }
255
255
 
256
256
  &.sorter-desc .table-header-sorter {
257
- display: initial;
257
+ display: inline;
258
258
 
259
259
  .arrow-up {
260
260
  fill: var(--sort-arrow-active-sub-color);
@@ -266,7 +266,7 @@
266
266
  }
267
267
 
268
268
  &.sorter-asc .table-header-sorter {
269
- display: initial;
269
+ display: inline;
270
270
 
271
271
  .arrow-up {
272
272
  fill: var(--sort-arrow-active-color);
@@ -388,7 +388,7 @@
388
388
 
389
389
  .table-header-title {
390
390
  overflow: hidden;
391
- align-self: flex-start;
391
+ // align-self: flex-start;
392
392
  }
393
393
 
394
394
  .table-header-sorter {
@@ -114,7 +114,7 @@ export type SortOption<T extends Record<string, any>> = Pick<StkTableColumn<T>,
114
114
  /** 排序状态 */
115
115
  export type SortState<T> = {
116
116
  dataIndex: keyof T;
117
- order: null | 'asc' | 'desc';
117
+ order: Order;
118
118
  sortType?: 'number' | 'string';
119
119
  };
120
120
 
@@ -13,9 +13,26 @@ type Options = {
13
13
  */
14
14
  export function useAutoResize({ tableContainerRef, initVirtualScroll, props, debounceMs }: Options) {
15
15
  let resizeObserver: ResizeObserver | null = null;
16
+ let isObserved = false;
17
+ watch(
18
+ () => props.virtual,
19
+ v => {
20
+ if (v) initResizeObserver();
21
+ else removeResizeObserver();
22
+ },
23
+ );
24
+ watch(
25
+ () => props.virtualX,
26
+ v => {
27
+ if (v) initResizeObserver();
28
+ else removeResizeObserver();
29
+ },
30
+ );
16
31
 
17
32
  onMounted(() => {
18
- initResizeObserver();
33
+ if (props.virtual || props.virtualX) {
34
+ initResizeObserver();
35
+ }
19
36
  });
20
37
 
21
38
  onBeforeUnmount(() => {
@@ -23,6 +40,9 @@ export function useAutoResize({ tableContainerRef, initVirtualScroll, props, deb
23
40
  });
24
41
 
25
42
  function initResizeObserver() {
43
+ if (isObserved) {
44
+ removeResizeObserver();
45
+ }
26
46
  if (window.ResizeObserver) {
27
47
  if (!tableContainerRef.value) {
28
48
  const watchDom = watch(
@@ -39,15 +59,18 @@ export function useAutoResize({ tableContainerRef, initVirtualScroll, props, deb
39
59
  } else {
40
60
  window.addEventListener('resize', resizeCallback);
41
61
  }
62
+ isObserved = true;
42
63
  }
43
64
 
44
65
  function removeResizeObserver() {
66
+ if (!isObserved) return;
45
67
  if (resizeObserver) {
46
68
  resizeObserver.disconnect();
47
69
  resizeObserver = null;
48
70
  } else {
49
71
  window.removeEventListener('resize', resizeCallback);
50
72
  }
73
+ isObserved = false;
51
74
  }
52
75
 
53
76
  let debounceTime = 0;
@@ -153,7 +153,7 @@ export function useHighlight({ props, stkTableId, tableContainerRef }: Params) {
153
153
  * @param option.duration 动画时长。method='css'状态下,用于移除class,如果传入了className则需要与自定义的动画时间一致。
154
154
  */
155
155
  function setHighlightDimCell(
156
- rowKeyValue: string,
156
+ rowKeyValue: UniqKey,
157
157
  dataIndex: string,
158
158
  option: { className?: string; method?: 'animation' | 'css'; keyframe?: Parameters<Animatable['animate']>['0']; duration?: number } = {},
159
159
  ) {
@@ -12,6 +12,8 @@ function isEmptyValue(val: any, isNumber?: boolean) {
12
12
 
13
13
  /**
14
14
  * 对有序数组插入新数据
15
+ *
16
+ * 注意:不会改变原数组,返回新数组
15
17
  * @param sortState
16
18
  * @param sortState.dataIndex 排序的列
17
19
  * @param sortState.order 排序顺序
@@ -20,7 +22,7 @@ function isEmptyValue(val: any, isNumber?: boolean) {
20
22
  * @param targetArray 表格数据
21
23
  * @return targetArray 的浅拷贝
22
24
  */
23
- export function insertToOrderedArray<T extends object>(sortState: SortState<T>, newItem: any, targetArray: T[], sortConfig: SortConfig<T> = {}) {
25
+ export function insertToOrderedArray<T extends object>(sortState: SortState<T>, newItem: T, targetArray: T[], sortConfig: SortConfig<T> = {}) {
24
26
  const { dataIndex, order } = sortState;
25
27
  sortConfig = { emptyToBottom: false, ...sortConfig };
26
28
  let { sortType } = sortState;
@@ -28,13 +30,13 @@ export function insertToOrderedArray<T extends object>(sortState: SortState<T>,
28
30
  const isNumber = sortType === 'number';
29
31
  const data = [...targetArray];
30
32
 
31
- if (!order) {
33
+ if (!order || !data.length) {
32
34
  // 没有排序的情况,插入在最上方
33
35
  data.unshift(newItem);
34
36
  return data;
35
37
  }
36
38
 
37
- if (sortConfig.emptyToBottom && isEmptyValue(data)) {
39
+ if (sortConfig.emptyToBottom && isEmptyValue(newItem)) {
38
40
  // 空值排在最下方
39
41
  data.push(newItem);
40
42
  }
@@ -42,7 +44,7 @@ export function insertToOrderedArray<T extends object>(sortState: SortState<T>,
42
44
  // 二分插入
43
45
  let sIndex = 0;
44
46
  let eIndex = data.length - 1;
45
- const targetVal = newItem[dataIndex];
47
+ const targetVal: any = newItem[dataIndex];
46
48
  while (sIndex <= eIndex) {
47
49
  // console.log(sIndex, eIndex);
48
50
  const midIndex = Math.floor((sIndex + eIndex) / 2);
@@ -71,10 +73,10 @@ export function insertToOrderedArray<T extends object>(sortState: SortState<T>,
71
73
  * @param b
72
74
  * @param type 类型
73
75
  * @param isNumber 是否是数字类型
74
- * @param localeCompare 是否 使用Array.prototype.localeCompare
76
+ * @param localeCompare 是否 使用Array.prototyshpe.localeCompare
75
77
  * @return {-1|0|1}
76
78
  */
77
- function strCompare(a: string, b: string, isNumber: boolean, localeCompare = false): number {
79
+ export function strCompare(a: string, b: string, isNumber: boolean, localeCompare = false): number {
78
80
  let _a: number | string = a;
79
81
  let _b: number | string = b;
80
82
  if (isNumber) {