gaoluyang
2026-06-24 c0cb161bb52ce0fbdce5c66ec391d107c75e2452
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import type { VxeGridSlots, VxeGridSlotTypes } from 'vxe-table';
 
import type { SlotsType } from 'vue';
 
import type { BaseFormComponentType } from '..\..\..\..\@core\ui-kit\form-ui\src';
 
import type { ExtendedVxeGridApi, VxeGridProps } from './types';
 
import { defineComponent, h, onBeforeUnmount } from 'vue';
 
import { useStore } from '..\..\..\..\@core\base\shared\src\store';
 
import { VxeGridApi } from './api';
import VxeGrid from './use-vxe-grid.vue';
 
type FilteredSlots<T> = {
  [K in keyof VxeGridSlots<T> as K extends 'form'
    ? never
    : K]: VxeGridSlots<T>[K];
};
 
export function useVbenVxeGrid<
  T extends Record<string, any> = any,
  D extends BaseFormComponentType = BaseFormComponentType,
  P extends Record<string, any> = Record<never, never>,
>(options: VxeGridProps<T, D, P>) {
  // const IS_REACTIVE = isReactive(options);
  const api = new VxeGridApi<T, D, P>(options);
  const extendedApi: ExtendedVxeGridApi<T, D, P> = api as ExtendedVxeGridApi<
    T,
    D,
    P
  >;
  extendedApi.useStore = (selector) => {
    return useStore(api.store, selector);
  };
 
  const Grid = defineComponent(
    (props: VxeGridProps<T, D, P>, { attrs, slots }) => {
      onBeforeUnmount(() => {
        api.unmount();
      });
      api.setState({ ...props, ...attrs } as Partial<VxeGridProps<T, D, P>>);
      return () =>
        h(
          VxeGrid,
          {
            ...props,
            ...attrs,
            api: extendedApi as ExtendedVxeGridApi,
          },
          slots,
        );
    },
    {
      name: 'VbenVxeGrid',
      inheritAttrs: false,
      slots: Object as SlotsType<
        {
          // 表格标题
          'table-title': undefined;
          // 工具栏左侧部分
          'toolbar-actions': VxeGridSlotTypes.DefaultSlotParams<T>;
          // 工具栏右侧部分
          'toolbar-tools': VxeGridSlotTypes.DefaultSlotParams<T>;
        } & FilteredSlots<T>
      >,
    },
  );
  // Add reactivity support
  // if (IS_REACTIVE) {
  //   watch(
  //     () => options,
  //     () => {
  //       api.setState(options);
  //     },
  //     { immediate: true },
  //   );
  // }
 
  return [Grid, extendedApi] as const;
}
 
export type UseVbenVxeGrid = typeof useVbenVxeGrid;