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