import { TableData } from '@arco-design/web-vue'
|
import { useVModels } from '@vueuse/core'
|
import { computed, inject, provide, ref, SetupContext } from 'vue'
|
import { useUtils } from './useUtils'
|
import { TablePropsItemType } from './BaseTable.d'
|
|
const KEY = Symbol('BaseTable')
|
|
const useStore = (props: TablePropsItemType, ctx: SetupContext) => {
|
const currentRow = ref<TableData>()
|
|
const { dataSource } = useVModels(props, ctx.emit)
|
const { columns, columnSeq } = useUtils(props, ctx)
|
const rowId = computed(() => props.id || 'id')
|
const baseTableRef = ref()
|
const currentHeight = ref<number>(0)
|
|
const store = {
|
currentRow,
|
dataSource,
|
columns,
|
columnSeq,
|
rowId,
|
baseTableRef,
|
currentHeight,
|
}
|
return store
|
}
|
|
export type StoreType = ReturnType<typeof useStore>
|
|
let store = {}
|
|
export const createState = (props: TablePropsItemType, ctx: SetupContext) => {
|
store = useStore(props, ctx)
|
provide(KEY, store)
|
return store as StoreType
|
}
|
|
export const injectState = () => {
|
const injectStore = inject(KEY, '')
|
if (!injectStore) {
|
return store as StoreType
|
}
|
return inject(KEY) as StoreType
|
}
|