From df0f00085c1113eddd7a15954818ccece3538068 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=97=A0=E6=9C=A8?= Date: Tue, 29 Jun 2021 16:39:47 +0800 Subject: [PATCH] fix(table): fix rowSelection.onChange not work MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复为table提供rowSelection.onChange时,无法手动变更table的选中项的问题 fixed: #825 --- src/components/Table/src/hooks/useRowSelection.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/components/Table/src/hooks/useRowSelection.ts b/src/components/Table/src/hooks/useRowSelection.ts index af374dc8f..63f90edb4 100644 --- a/src/components/Table/src/hooks/useRowSelection.ts +++ b/src/components/Table/src/hooks/useRowSelection.ts @@ -1,6 +1,8 @@ +import { isFunction } from '/@/utils/is'; import type { BasicTableProps, TableRowSelection } from '../types/table'; -import { computed, ref, unref, ComputedRef, Ref, toRaw } from 'vue'; +import { computed, ref, unref, ComputedRef, Ref, toRaw, watch } from 'vue'; import { ROW_KEY } from '../const'; +import { omit } from 'lodash-es'; export function useRowSelection( propsRef: ComputedRef, @@ -22,15 +24,24 @@ export function useRowSelection( onChange: (selectedRowKeys: string[], selectedRows: Recordable[]) => { selectedRowKeysRef.value = selectedRowKeys; selectedRowRef.value = selectedRows; + const { onChange } = rowSelection; + if (onChange && isFunction(onChange)) onChange(selectedRowKeys, selectedRows); emit('selection-change', { keys: selectedRowKeys, rows: selectedRows, }); }, - ...(rowSelection === undefined ? {} : rowSelection), + ...omit(rowSelection === undefined ? {} : rowSelection, ['onChange']), }; }); + watch( + () => unref(propsRef).rowSelection?.selectedRowKeys, + (v: string[]) => { + selectedRowKeysRef.value = v; + } + ); + const getAutoCreateKey = computed(() => { return unref(propsRef).autoCreateKey && !unref(propsRef).rowKey; });