use-vxe-grid.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import type { ExtendedVxeGridApi, VxeGridProps } from './types';
  2. import { defineComponent, h, onBeforeUnmount } from 'vue';
  3. import { useStore } from '@vben-core/shared/store';
  4. import { VxeGridApi } from './api';
  5. import VxeGrid from './use-vxe-grid.vue';
  6. export function useVbenVxeGrid(options: VxeGridProps) {
  7. // const IS_REACTIVE = isReactive(options);
  8. const api = new VxeGridApi(options);
  9. const extendedApi: ExtendedVxeGridApi = api as ExtendedVxeGridApi;
  10. extendedApi.useStore = (selector) => {
  11. return useStore(api.store, selector);
  12. };
  13. const Grid = defineComponent(
  14. (props: VxeGridProps, { attrs, slots }) => {
  15. onBeforeUnmount(() => {
  16. api.unmount();
  17. });
  18. api.setState({ ...props, ...attrs });
  19. return () => h(VxeGrid, { ...props, ...attrs, api: extendedApi }, slots);
  20. },
  21. {
  22. name: 'VbenVxeGrid',
  23. inheritAttrs: false,
  24. },
  25. );
  26. // Add reactivity support
  27. // if (IS_REACTIVE) {
  28. // watch(
  29. // () => options,
  30. // () => {
  31. // api.setState(options);
  32. // },
  33. // { immediate: true },
  34. // );
  35. // }
  36. return [Grid, extendedApi] as const;
  37. }
  38. export type UseVbenVxeGrid = typeof useVbenVxeGrid;