use-vxe-grid.ts 2.2 KB

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