fix(table): selection-change not triggered on row click

修复selection-change事件在点击行来改变勾选时不会被触发的问题
This commit is contained in:
无木 2021-06-30 15:43:32 +08:00
parent d509e897be
commit 6f845b53bd

View File

@ -1,6 +1,6 @@
import { isFunction } from '/@/utils/is'; import { isFunction } from '/@/utils/is';
import type { BasicTableProps, TableRowSelection } from '../types/table'; import type { BasicTableProps, TableRowSelection } from '../types/table';
import { computed, ref, unref, ComputedRef, Ref, toRaw, watch } from 'vue'; import { computed, ref, unref, ComputedRef, Ref, toRaw, watch, nextTick } from 'vue';
import { ROW_KEY } from '../const'; import { ROW_KEY } from '../const';
import { omit } from 'lodash-es'; import { omit } from 'lodash-es';
@ -24,12 +24,6 @@ export function useRowSelection(
onChange: (selectedRowKeys: string[], selectedRows: Recordable[]) => { onChange: (selectedRowKeys: string[], selectedRows: Recordable[]) => {
selectedRowKeysRef.value = selectedRowKeys; selectedRowKeysRef.value = selectedRowKeys;
selectedRowRef.value = selectedRows; selectedRowRef.value = selectedRows;
const { onChange } = rowSelection;
if (onChange && isFunction(onChange)) onChange(selectedRowKeys, selectedRows);
emit('selection-change', {
keys: selectedRowKeys,
rows: selectedRows,
});
}, },
...omit(rowSelection === undefined ? {} : rowSelection, ['onChange']), ...omit(rowSelection === undefined ? {} : rowSelection, ['onChange']),
}; };
@ -38,7 +32,24 @@ export function useRowSelection(
watch( watch(
() => unref(propsRef).rowSelection?.selectedRowKeys, () => unref(propsRef).rowSelection?.selectedRowKeys,
(v: string[]) => { (v: string[]) => {
selectedRowKeysRef.value = v; setSelectedRowKeys(v);
}
);
watch(
() => unref(selectedRowKeysRef),
() => {
nextTick(() => {
const { rowSelection } = unref(propsRef);
if (rowSelection) {
const { onChange } = rowSelection;
if (onChange && isFunction(onChange)) onChange(getSelectRowKeys(), getSelectRows());
}
emit('selection-change', {
keys: getSelectRowKeys(),
rows: getSelectRows(),
});
});
} }
); );