stk-table-vue 0.8.8 → 0.8.9

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stk-table-vue",
3
- "version": "0.8.8",
3
+ "version": "0.8.9",
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",
@@ -20,7 +20,7 @@
20
20
  'cell-hover': props.cellHover,
21
21
  'cell-active': props.cellActive,
22
22
  'row-hover': props.rowHover,
23
- 'row-active': props.rowActive,
23
+ 'row-active': rowActiveProp.enabled,
24
24
  'text-overflow': props.showOverflow,
25
25
  'header-text-overflow': props.showHeaderOverflow,
26
26
  'fixed-relative-mode': isRelativeMode,
@@ -253,7 +253,14 @@ import { CSSProperties, computed, nextTick, onMounted, ref, shallowRef, toRaw, w
253
253
  import DragHandle from './components/DragHandle.vue';
254
254
  import SortIcon from './components/SortIcon.vue';
255
255
  import TriangleIcon from './components/TriangleIcon.vue';
256
- import { CELL_KEY_SEPARATE, DEFAULT_ROW_HEIGHT, DEFAULT_SMOOTH_SCROLL, DEFAULT_SORT_CONFIG, IS_LEGACY_MODE } from './const';
256
+ import {
257
+ CELL_KEY_SEPARATE,
258
+ DEFAULT_ROW_ACTIVE_CONFIG,
259
+ DEFAULT_ROW_HEIGHT,
260
+ DEFAULT_SMOOTH_SCROLL,
261
+ DEFAULT_SORT_CONFIG,
262
+ IS_LEGACY_MODE,
263
+ } from './const';
257
264
  import {
258
265
  AutoRowHeightConfig,
259
266
  ColResizableConfig,
@@ -264,6 +271,7 @@ import {
264
271
  Order,
265
272
  PrivateRowDT,
266
273
  PrivateStkTableColumn,
274
+ RowActiveOption,
267
275
  SeqConfig,
268
276
  SortConfig,
269
277
  SortOption,
@@ -328,8 +336,10 @@ const props = withDefaults(
328
336
  /** 是否高亮鼠标悬浮的行 */
329
337
  rowHover?: boolean;
330
338
  /** 是否高亮选中的行 */
331
- rowActive?: boolean;
332
- /** 当前行再次点击否可以取消 (rowActive=true)*/
339
+ rowActive?: boolean | RowActiveOption<DT>;
340
+ /**
341
+ * @deprecated
342
+ */
333
343
  rowCurrentRevokable?: boolean;
334
344
  /** 表头行高。default = rowHeight */
335
345
  headerRowHeight?: number | string | null;
@@ -706,6 +716,19 @@ const isTreeData = computed(() => {
706
716
  return props.columns.some(col => col.type === 'tree-node');
707
717
  });
708
718
 
719
+ const rowActiveProp = computed<Required<RowActiveOption<DT>>>(() => {
720
+ const { rowActive } = props;
721
+ if (typeof rowActive === 'boolean') {
722
+ return {
723
+ ...DEFAULT_ROW_ACTIVE_CONFIG,
724
+ enabled: rowActive,
725
+ revokable: Boolean(props.rowCurrentRevokable),
726
+ };
727
+ } else {
728
+ return { ...DEFAULT_ROW_ACTIVE_CONFIG, ...rowActive };
729
+ }
730
+ });
731
+
709
732
  const dataSourceCopy = shallowRef<DT[]>([]);
710
733
 
711
734
  const rowKeyGenComputed = computed(() => {
@@ -786,7 +809,7 @@ const {
786
809
  updateHoverMergedCells,
787
810
  activeMergedCells,
788
811
  updateActiveMergedCells,
789
- } = useMergeCells({ props, tableHeaderLast, rowKeyGen, colKeyGen, virtual_dataSourcePart });
812
+ } = useMergeCells({ rowActiveProp, tableHeaderLast, rowKeyGen, colKeyGen, virtual_dataSourcePart });
790
813
 
791
814
  const getFixedColPosition = useGetFixedColPosition({ colKeyGen, tableHeadersForCalc });
792
815
 
@@ -1164,7 +1187,7 @@ function onColumnSort(col: StkTableColumn<DT> | undefined | null, click = true,
1164
1187
  dataSourceCopy.value = isTreeData.value ? flatTreeData(dataSourceTemp) : dataSourceTemp;
1165
1188
  }
1166
1189
  }
1167
- // 只有点击才触发事件 en: only emit sort-change event when click
1190
+ // only emit sort-change event when click
1168
1191
  if (click || options.emit) {
1169
1192
  emits('sort-change', col, order, toRaw(dataSourceTemp), sortConfig);
1170
1193
  }
@@ -1172,20 +1195,15 @@ function onColumnSort(col: StkTableColumn<DT> | undefined | null, click = true,
1172
1195
 
1173
1196
  function onRowClick(e: MouseEvent, row: DT, rowIndex: number) {
1174
1197
  emits('row-click', e, row, { rowIndex });
1198
+ if (rowActiveProp.value.disabled?.(row)) return;
1175
1199
  const isCurrentRow = props.rowKey ? currentRowKey.value === rowKeyGen(row) : currentRow.value === row;
1176
1200
  if (isCurrentRow) {
1177
- if (!props.rowCurrentRevokable) {
1178
- // 不可取消
1201
+ if (!rowActiveProp.value.revokable) {
1179
1202
  return;
1180
1203
  }
1181
- // 点击同一行,取消当前选中行。
1182
- currentRow.value = void 0;
1183
- currentRowKey.value = void 0;
1184
- updateActiveMergedCells(true);
1204
+ setCurrentRow(void 0, { silent: true });
1185
1205
  } else {
1186
- currentRow.value = row;
1187
- currentRowKey.value = rowKeyGen(row);
1188
- updateActiveMergedCells();
1206
+ setCurrentRow(row, { silent: true });
1189
1207
  }
1190
1208
  emits('current-change', e, row, { select: !isCurrentRow });
1191
1209
  }
@@ -1393,12 +1411,13 @@ function onTrMouseLeave(e: MouseEvent) {
1393
1411
  * @param {boolean} option.silent if set true not emit `current-change`. default:false
1394
1412
  * @param {boolean} option.deep if set true, deep search in children. default:false
1395
1413
  */
1396
- function setCurrentRow(rowKeyOrRow: string | undefined | DT, option = { silent: false, deep: false }) {
1397
- if (!dataSourceCopy.value.length) return;
1414
+ function setCurrentRow(rowKeyOrRow: string | undefined | DT, option: { silent?: boolean; deep?: boolean } = { silent: false, deep: false }) {
1398
1415
  const select = rowKeyOrRow !== void 0;
1416
+ const currentRowTemp = currentRow.value;
1399
1417
  if (!select) {
1400
1418
  currentRow.value = void 0;
1401
1419
  currentRowKey.value = void 0;
1420
+ updateActiveMergedCells(true);
1402
1421
  } else if (typeof rowKeyOrRow === 'string') {
1403
1422
  const findRowByKey = (data: DT[], key: string): DT | null => {
1404
1423
  for (let i = 0; i < data.length; i++) {
@@ -1416,19 +1435,21 @@ function setCurrentRow(rowKeyOrRow: string | undefined | DT, option = { silent:
1416
1435
  return null;
1417
1436
  };
1418
1437
 
1419
- const row = findRowByKey(dataSourceCopy.value, rowKeyOrRow);
1438
+ currentRowKey.value = rowKeyOrRow;
1439
+ updateActiveMergedCells(false, currentRowKey.value);
1440
+ const row = findRowByKey(dataSourceCopy.value || [], rowKeyOrRow);
1420
1441
  if (!row) {
1421
1442
  console.warn('setCurrentRow failed.rowKey:', rowKeyOrRow);
1422
1443
  return;
1423
1444
  }
1424
1445
  currentRow.value = row;
1425
- currentRowKey.value = rowKeyOrRow;
1426
1446
  } else {
1427
1447
  currentRow.value = rowKeyOrRow;
1428
1448
  currentRowKey.value = rowKeyGen(rowKeyOrRow);
1449
+ updateActiveMergedCells(false, currentRowKey.value);
1429
1450
  }
1430
1451
  if (!option.silent) {
1431
- emits('current-change', /** no Event */ null, currentRow.value, { select });
1452
+ emits('current-change', /** no Event */ null, select ? currentRow.value : currentRowTemp, { select });
1432
1453
  }
1433
1454
  }
1434
1455
 
@@ -1,4 +1,4 @@
1
- import { SortConfig } from './types';
1
+ import { RowActiveOption, SortConfig } from './types';
2
2
  import { getBrowsersVersion } from './utils';
3
3
 
4
4
  export const DEFAULT_COL_WIDTH = '100';
@@ -42,3 +42,9 @@ export const DEFAULT_SORT_CONFIG = {
42
42
  stringLocaleCompare: false,
43
43
  sortChildren: false,
44
44
  } satisfies SortConfig<any>;
45
+
46
+ export const DEFAULT_ROW_ACTIVE_CONFIG: Required<RowActiveOption<any>> = {
47
+ enabled: false,
48
+ disabled: () => false,
49
+ revokable: false,
50
+ };
@@ -284,3 +284,11 @@ export type RowKeyGen = (row: any) => UniqKey;
284
284
  export type ColKeyGen = ComputedRef<(col: StkTableColumn<any>) => UniqKey>;
285
285
 
286
286
  export type CellKeyGen = (row: any, col: StkTableColumn<any>) => string;
287
+
288
+ export type RowActiveOption<DT> = {
289
+ enabled?: boolean;
290
+ /** disabled row active */
291
+ disabled?: (row: DT) => boolean;
292
+ /** current row again click can revoke active */
293
+ revokable?: boolean;
294
+ };
@@ -1,20 +1,14 @@
1
- import { ref, ShallowRef, watch } from 'vue';
2
- import { ColKeyGen, MergeCellsParam, PrivateStkTableColumn, RowKeyGen, UniqKey } from './types';
1
+ import { Ref, ref, ShallowRef, watch } from 'vue';
2
+ import { ColKeyGen, MergeCellsParam, PrivateStkTableColumn, RowActiveOption, RowKeyGen, UniqKey } from './types';
3
3
  import { pureCellKeyGen } from './utils';
4
4
  type Options = {
5
- props: any;
5
+ rowActiveProp: Ref<RowActiveOption<any>>;
6
6
  tableHeaderLast: ShallowRef<PrivateStkTableColumn<any>[]>;
7
7
  rowKeyGen: RowKeyGen;
8
8
  colKeyGen: ColKeyGen;
9
9
  virtual_dataSourcePart: ShallowRef<any[]>;
10
- }
11
- export function useMergeCells({
12
- props,
13
- tableHeaderLast,
14
- rowKeyGen,
15
- colKeyGen,
16
- virtual_dataSourcePart,
17
- }:Options ) {
10
+ };
11
+ export function useMergeCells({ rowActiveProp, tableHeaderLast, rowKeyGen, colKeyGen, virtual_dataSourcePart }: Options) {
18
12
  /**
19
13
  * which cell need be hidden
20
14
  * - key: rowKey
@@ -33,13 +27,12 @@ export function useMergeCells({
33
27
  /** click current row , which rowspan cells should be highlight */
34
28
  const activeMergedCells = ref(new Set<string>());
35
29
 
36
-
37
30
  watch([virtual_dataSourcePart, tableHeaderLast], () => {
38
31
  hiddenCellMap.value = {};
39
32
  hoverRowMap.value = {};
40
33
  });
41
34
 
42
- /**
35
+ /**
43
36
  * abstract the logic of hiding cells
44
37
  */
45
38
  function hideCells(rowKey: UniqKey, startIndex: number, count: number, isSelfRow = false, mergeCellKey: string) {
@@ -84,14 +77,14 @@ export function useMergeCells({
84
77
  if (colspan === 1 && rowspan === 1) return;
85
78
 
86
79
  const rowKey = rowKeyGen(row);
87
-
80
+
88
81
  const colKey = colKeyGen.value(col);
89
82
  const curColIndex = tableHeaderLast.value.findIndex(item => colKeyGen.value(item) === colKey);
90
83
  const curRowIndex = virtual_dataSourcePart.value.findIndex(item => rowKeyGen(item) === rowKey);
91
84
  const mergedCellKey = pureCellKeyGen(rowKey, colKey);
92
85
 
93
86
  if (curRowIndex === -1) return;
94
-
87
+
95
88
  for (let i = curRowIndex; i < curRowIndex + rowspan; i++) {
96
89
  const row = virtual_dataSourcePart.value[i];
97
90
  if (!row) break;
@@ -106,13 +99,13 @@ export function useMergeCells({
106
99
  hoverMergedCells.value = set || new Set();
107
100
  }
108
101
 
109
- function updateActiveMergedCells(clear?: boolean) {
110
- if (!props.rowActive) return;
102
+ function updateActiveMergedCells(clear?: boolean, rowKey?: UniqKey) {
103
+ if (!rowActiveProp.value.enabled) return;
111
104
  if (clear) {
112
105
  activeMergedCells.value.clear();
113
- } else {
114
- activeMergedCells.value = new Set(hoverMergedCells.value);
106
+ return;
115
107
  }
108
+ activeMergedCells.value = (rowKey !== void 0 && hoverRowMap.value[rowKey]) || new Set(hoverMergedCells.value);
116
109
  }
117
110
 
118
111
  return {