Skip to content

Commit 467c970

Browse files
authored
refactor(Table): 重构多选事件触发逻辑 (#1244)
* refactor(Table): 重构多选事件触发逻辑
1 parent 744be0d commit 467c970

File tree

4 files changed

+20
-10
lines changed

4 files changed

+20
-10
lines changed

packages/devui-vue/devui/table/src/components/column/config.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const cellMap = {
2121
halfChecked: store.states._halfChecked.value,
2222
onChange: (val: boolean) => {
2323
store.states._checkAll.value = val;
24-
store._table.emit('check-all-change', val, store.getCheckedRows());
24+
store.emitTableEvent('check-all-change', val, store.getCheckedRows());
2525
},
2626
});
2727
},
@@ -30,7 +30,7 @@ export const cellMap = {
3030
modelValue: store.isRowChecked(rowData, rowIndex),
3131
onChange: (val: boolean) => {
3232
store.checkRow(val, rowData, rowIndex);
33-
store._table.emit('check-change', val, store.states._data.value[rowIndex], store.getCheckedRows());
33+
store.emitTableEvent('check-change', val, store.states._data.value[rowIndex], store.getCheckedRows());
3434
},
3535
});
3636
},

packages/devui-vue/devui/table/src/store/index.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { isBoolean } from '../../../shared/utils';
21
import { watch, Ref, ref, computed, unref } from 'vue';
2+
import type { SetupContext } from 'vue';
3+
import { isBoolean } from '../../../shared/utils';
34
import type { Column, LevelColumn } from '../components/column/column-types';
45
import type { DefaultRow, ITable, RowKeyType } from '../table-types';
56
import type { TableStore } from './store-types';
@@ -239,7 +240,11 @@ function createFixedLogic(columns: Ref<Column[]>) {
239240
* @param table 表对象
240241
* @returns TableStore
241242
*/
242-
export function createStore<T extends Record<string, unknown>>(dataSource: Ref<T[]>, table: ITable<DefaultRow>): TableStore<T> {
243+
export function createStore<T extends Record<string, unknown>>(
244+
dataSource: Ref<T[]>,
245+
table: ITable<DefaultRow>,
246+
ctx: SetupContext
247+
): TableStore<T> {
243248
const _data: Ref<T[]> = ref([]);
244249
const { _columns, flatColumns, insertColumn, removeColumn, sortColumn, updateColumns } = createColumnGenerator();
245250
const { flatRows, hiddenRowKeys, rowLevelMap, updateRows, firstDefaultColumn, updateFirstDefaultColumn } = createRowGenerator<T>(
@@ -261,6 +266,10 @@ export function createStore<T extends Record<string, unknown>>(dataSource: Ref<T
261266

262267
const { tableCellModeMap, setCellMode, resetCellMode } = useEditTableCell();
263268

269+
const emitTableEvent = (eventName: string, ...params: unknown[]) => {
270+
ctx.emit.apply(ctx, [eventName, ...params]);
271+
};
272+
264273
watch(
265274
dataSource,
266275
(value: T[]) => {
@@ -271,7 +280,6 @@ export function createStore<T extends Record<string, unknown>>(dataSource: Ref<T
271280
);
272281

273282
return {
274-
_table: table,
275283
states: {
276284
_data,
277285
flatRows,
@@ -304,5 +312,6 @@ export function createStore<T extends Record<string, unknown>>(dataSource: Ref<T
304312
updateFirstDefaultColumn,
305313
setCellMode,
306314
resetCellMode,
315+
emitTableEvent,
307316
};
308317
}

packages/devui-vue/devui/table/src/store/store-types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import type { ComponentInternalInstance, Ref } from 'vue';
22
import { Column, SortMethod, SortDirection } from '../components/column/column-types';
3-
import { DefaultRow, ITable } from '../table-types';
3+
import { DefaultRow } from '../table-types';
44

55
// TableStore 对象
66
// 主要是为了方便维护 Table 中的各种状态
77
export interface TableStore<T = Record<string, unknown>> {
8-
// 内置 table 对象
9-
_table: ITable<DefaultRow>;
108
// 具体存储的数据
119
states: {
1210
// 外部数据源
@@ -68,6 +66,8 @@ export interface TableStore<T = Record<string, unknown>> {
6866
setCellMode: (row: DefaultRow, rowIndex: number, fields: string | string[], cellMode: string) => void;
6967
// 重置所有单元格状态为只读状态
7068
resetCellMode: () => void;
69+
// 触发Table组件上的事件
70+
emitTableEvent: (eventName: string, ...params: unknown[]) => void;
7171
}
7272

7373
export interface UseExpand {

packages/devui-vue/devui/table/src/table.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { provide, defineComponent, getCurrentInstance, computed, toRef, ref, onMounted, nextTick, withModifiers } from 'vue';
2+
import type { SetupContext } from 'vue';
23
import { tableProps, TableProps, TABLE_TOKEN, ITableInstanceAndDefaultRow } from './table-types';
34
import { useTable, useTableLayout, useTableWatcher } from './composables/use-table';
45
import { useHorizontalScroll } from './composables/use-horizontal-scroll';
@@ -18,9 +19,9 @@ export default defineComponent({
1819
},
1920
props: tableProps,
2021
emits: ['sort-change', 'cell-click', 'row-click', 'check-change', 'check-all-change', 'expand-change', 'load-more'],
21-
setup(props: TableProps, ctx) {
22+
setup(props: TableProps, ctx: SetupContext) {
2223
const table = getCurrentInstance() as ITableInstanceAndDefaultRow;
23-
const store = createStore(toRef(props, 'data'), table);
24+
const store = createStore(toRef(props, 'data'), table, ctx);
2425
const tableId = `devui-table_${tableIdInit++}`;
2526
const tableRef = ref();
2627
table.tableId = tableId;

0 commit comments

Comments
 (0)