|
|
|
@ -1,13 +1,18 @@
|
|
|
|
|
import type { ComputedRef, Ref } from 'vue' |
|
|
|
|
import { computed, nextTick, ref, toRaw, unref, watch } from 'vue' |
|
|
|
|
import { omit } from 'lodash-es' |
|
|
|
|
import type { Key } from 'ant-design-vue/lib/table/interface' |
|
|
|
|
import type { BasicTableProps, TableRowSelection } from '../types/table' |
|
|
|
|
import { ROW_KEY } from '../const' |
|
|
|
|
import { isFunction } from '@/utils/is' |
|
|
|
|
import { findNodeAll } from '@/utils/helper/treeHelper' |
|
|
|
|
import { isFunction } from '@/utils/is' |
|
|
|
|
|
|
|
|
|
export function useRowSelection(propsRef: ComputedRef<BasicTableProps>, tableData: Ref<Recordable[]>, emit: EmitType) { |
|
|
|
|
const selectedRowKeysRef = ref<string[]>([]) |
|
|
|
|
export function useRowSelection( |
|
|
|
|
propsRef: ComputedRef<BasicTableProps>, |
|
|
|
|
tableData: Ref<Recordable[]>, |
|
|
|
|
emit: EmitType, |
|
|
|
|
) { |
|
|
|
|
const selectedRowKeysRef = ref<Key[]>([]) |
|
|
|
|
const selectedRowRef = ref<Recordable[]>([]) |
|
|
|
|
|
|
|
|
|
const getRowSelectionRef = computed((): TableRowSelection | null => { |
|
|
|
@ -17,7 +22,7 @@ export function useRowSelection(propsRef: ComputedRef<BasicTableProps>, tableDat
|
|
|
|
|
|
|
|
|
|
return { |
|
|
|
|
selectedRowKeys: unref(selectedRowKeysRef), |
|
|
|
|
onChange: (selectedRowKeys: string[]) => { |
|
|
|
|
onChange: (selectedRowKeys: Key[]) => { |
|
|
|
|
setSelectedRowKeys(selectedRowKeys) |
|
|
|
|
}, |
|
|
|
|
...omit(rowSelection, ['onChange']), |
|
|
|
@ -26,7 +31,7 @@ export function useRowSelection(propsRef: ComputedRef<BasicTableProps>, tableDat
|
|
|
|
|
|
|
|
|
|
watch( |
|
|
|
|
() => unref(propsRef).rowSelection?.selectedRowKeys, |
|
|
|
|
(v: string[]) => { |
|
|
|
|
(v?: Key[]) => { |
|
|
|
|
setSelectedRowKeys(v) |
|
|
|
|
}, |
|
|
|
|
) |
|
|
|
@ -59,8 +64,8 @@ export function useRowSelection(propsRef: ComputedRef<BasicTableProps>, tableDat
|
|
|
|
|
return unref(getAutoCreateKey) ? ROW_KEY : rowKey |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
function setSelectedRowKeys(rowKeys: string[]) { |
|
|
|
|
selectedRowKeysRef.value = rowKeys |
|
|
|
|
function setSelectedRowKeys(rowKeys?: Key[]) { |
|
|
|
|
selectedRowKeysRef.value = rowKeys || [] |
|
|
|
|
const allSelectedRows = findNodeAll( |
|
|
|
|
toRaw(unref(tableData)).concat(toRaw(unref(selectedRowRef))), |
|
|
|
|
item => rowKeys?.includes(item[unref(getRowKey) as string]), |
|
|
|
@ -69,7 +74,7 @@ export function useRowSelection(propsRef: ComputedRef<BasicTableProps>, tableDat
|
|
|
|
|
}, |
|
|
|
|
) |
|
|
|
|
const trueSelectedRows: any[] = [] |
|
|
|
|
rowKeys?.forEach((key: string) => { |
|
|
|
|
rowKeys?.forEach((key: Key) => { |
|
|
|
|
const found = allSelectedRows.find(item => item[unref(getRowKey) as string] === key) |
|
|
|
|
found && trueSelectedRows.push(found) |
|
|
|
|
}) |
|
|
|
|