提交 | 用户 | age
|
faf3f4
|
1 |
import type { BasicTableProps, TableActionType, FetchParams, BasicColumn } from '../types/table'; |
陈 |
2 |
import type { PaginationProps } from '../types/pagination'; |
9c2f3f
|
3 |
import type { DynamicProps } from '/@/types/utils'; |
V |
4 |
import { getDynamicProps } from '/@/utils'; |
a1ffb6
|
5 |
|
601368
|
6 |
import { ref, onUnmounted, unref, watch, toRaw } from 'vue'; |
faf3f4
|
7 |
import { isProdMode } from '/@/utils/env'; |
a305e5
|
8 |
import { isInSetup } from '/@/utils/helper/vueHelper'; |
9c2f3f
|
9 |
import { error } from '/@/utils/log'; |
V |
10 |
import type { FormActionType } from '/@/components/Form'; |
|
11 |
|
|
12 |
type Props = Partial<DynamicProps<BasicTableProps>>; |
faf3f4
|
13 |
|
c889fb
|
14 |
type UseTableMethod = TableActionType & { |
V |
15 |
getForm: () => FormActionType; |
|
16 |
}; |
|
17 |
|
faf3f4
|
18 |
export function useTable( |
9c2f3f
|
19 |
tableProps?: Props |
c889fb
|
20 |
): [(instance: TableActionType, formInstance: UseTableMethod) => void, TableActionType] { |
a305e5
|
21 |
isInSetup(); |
faf3f4
|
22 |
|
a305e5
|
23 |
const tableRef = ref<Nullable<TableActionType>>(null); |
V |
24 |
const loadedRef = ref<Nullable<boolean>>(false); |
c889fb
|
25 |
const formRef = ref<Nullable<UseTableMethod>>(null); |
faf3f4
|
26 |
|
c889fb
|
27 |
function register(instance: TableActionType, formInstance: UseTableMethod) { |
354904
|
28 |
isProdMode() && |
V |
29 |
onUnmounted(() => { |
|
30 |
tableRef.value = null; |
|
31 |
loadedRef.value = null; |
|
32 |
}); |
a305e5
|
33 |
|
faf3f4
|
34 |
if (unref(loadedRef) && isProdMode() && instance === unref(tableRef)) { |
陈 |
35 |
return; |
|
36 |
} |
|
37 |
tableRef.value = instance; |
9c2f3f
|
38 |
formRef.value = formInstance; |
af5551
|
39 |
tableProps && instance.setProps(getDynamicProps(tableProps)); |
faf3f4
|
40 |
loadedRef.value = true; |
9c2f3f
|
41 |
|
af5551
|
42 |
watch( |
V |
43 |
() => tableProps, |
|
44 |
() => { |
|
45 |
tableProps && instance.setProps(getDynamicProps(tableProps)); |
|
46 |
}, |
|
47 |
{ |
|
48 |
immediate: true, |
a7a8b8
|
49 |
deep: true, |
af5551
|
50 |
} |
V |
51 |
); |
faf3f4
|
52 |
} |
陈 |
53 |
|
|
54 |
function getTableInstance(): TableActionType { |
|
55 |
const table = unref(tableRef); |
|
56 |
if (!table) { |
9c2f3f
|
57 |
error( |
V |
58 |
'The table instance has not been obtained yet, please make sure the table is presented when performing the table operation!' |
|
59 |
); |
faf3f4
|
60 |
} |
9c2f3f
|
61 |
return table as TableActionType; |
faf3f4
|
62 |
} |
陈 |
63 |
|
9c2f3f
|
64 |
const methods: TableActionType & { |
V |
65 |
getForm: () => FormActionType; |
|
66 |
} = { |
|
67 |
reload: async (opt?: FetchParams) => { |
faf3f4
|
68 |
getTableInstance().reload(opt); |
陈 |
69 |
}, |
|
70 |
setProps: (props: Partial<BasicTableProps>) => { |
|
71 |
getTableInstance().setProps(props); |
|
72 |
}, |
|
73 |
redoHeight: () => { |
|
74 |
getTableInstance().redoHeight(); |
|
75 |
}, |
|
76 |
setLoading: (loading: boolean) => { |
|
77 |
getTableInstance().setLoading(loading); |
|
78 |
}, |
|
79 |
getDataSource: () => { |
601368
|
80 |
return toRaw(getTableInstance().getDataSource()); |
faf3f4
|
81 |
}, |
陈 |
82 |
getColumns: ({ ignoreIndex = false }: { ignoreIndex?: boolean } = {}) => { |
|
83 |
const columns = getTableInstance().getColumns({ ignoreIndex }) || []; |
601368
|
84 |
return toRaw(columns); |
faf3f4
|
85 |
}, |
陈 |
86 |
setColumns: (columns: BasicColumn[]) => { |
|
87 |
getTableInstance().setColumns(columns); |
|
88 |
}, |
|
89 |
setTableData: (values: any[]) => { |
|
90 |
return getTableInstance().setTableData(values); |
|
91 |
}, |
|
92 |
setPagination: (info: Partial<PaginationProps>) => { |
|
93 |
return getTableInstance().setPagination(info); |
|
94 |
}, |
|
95 |
deleteSelectRowByKey: (key: string) => { |
|
96 |
getTableInstance().deleteSelectRowByKey(key); |
|
97 |
}, |
|
98 |
getSelectRowKeys: () => { |
601368
|
99 |
return toRaw(getTableInstance().getSelectRowKeys()); |
faf3f4
|
100 |
}, |
陈 |
101 |
getSelectRows: () => { |
601368
|
102 |
return toRaw(getTableInstance().getSelectRows()); |
faf3f4
|
103 |
}, |
陈 |
104 |
clearSelectedRowKeys: () => { |
|
105 |
getTableInstance().clearSelectedRowKeys(); |
|
106 |
}, |
|
107 |
setSelectedRowKeys: (keys: string[] | number[]) => { |
|
108 |
getTableInstance().setSelectedRowKeys(keys); |
|
109 |
}, |
|
110 |
getPaginationRef: () => { |
|
111 |
return getTableInstance().getPaginationRef(); |
|
112 |
}, |
8b3a4d
|
113 |
getSize: () => { |
601368
|
114 |
return toRaw(getTableInstance().getSize()); |
8b3a4d
|
115 |
}, |
9c2f3f
|
116 |
updateTableData: (index: number, key: string, value: any) => { |
V |
117 |
return getTableInstance().updateTableData(index, key, value); |
|
118 |
}, |
|
119 |
getRowSelection: () => { |
601368
|
120 |
return toRaw(getTableInstance().getRowSelection()); |
9c2f3f
|
121 |
}, |
V |
122 |
getCacheColumns: () => { |
601368
|
123 |
return toRaw(getTableInstance().getCacheColumns()); |
9c2f3f
|
124 |
}, |
V |
125 |
getForm: () => { |
c889fb
|
126 |
return (unref(formRef) as unknown) as FormActionType; |
9c2f3f
|
127 |
}, |
a2c89d
|
128 |
setShowPagination: async (show: boolean) => { |
V |
129 |
getTableInstance().setShowPagination(show); |
|
130 |
}, |
|
131 |
getShowPagination: () => { |
601368
|
132 |
return toRaw(getTableInstance().getShowPagination()); |
a2c89d
|
133 |
}, |
9c2f3f
|
134 |
}; |
faf3f4
|
135 |
|
陈 |
136 |
return [register, methods]; |
|
137 |
} |