schangxiang@126.com
2025-05-07 cace264ad9d86a7831099810b079da1141957add
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
import { TableData } from '@arco-design/web-vue'
import { computed, nextTick, onMounted, SetupContext } from 'vue'
import styles from './BaseTable.module.scss'
import { injectState } from './useState'
import { ColumnType, ScopeType, TablePropsItemType } from './BaseTable.d'
import { debounce, isFunction } from 'lodash'
 
export const useHook = (props: TablePropsItemType, ctx: SetupContext) => {
  const { currentRow, dataSource, rowId, currentHeight, baseTableRef } =
    injectState()
 
  /**
   * 获取行的样式名称
   * @param record
   * @param rowIndex
   * @returns
   */
  const getRowClassName = (record: TableData, rowIndex: number) => {
    if (record === currentRow.value) {
      return styles.baseTableRow
    }
  }
  /**
   * 获取style
   */
  const getTableStyle = () => {
    return {
      ...props.style,
      '--base-row-background': props.hightLightRow,
    }
  }
  /**
   * 设置当前行高亮
   * @param key id
   */
  const setCurrentRow = (key: string) => {
    const row = dataSource.value.find((item) => item[rowId.value] === key)
    if (row) {
      currentRow.value = row
    }
  }
 
  onMounted(() => {
    if (baseTableRef.value) {
      const fn = debounce((entries: any) => {
        const entry = entries[0]
        const borderBoxSize = entry.borderBoxSize[0] || {}
        currentHeight.value = borderBoxSize.blockSize
      }, 150)
      const resizeObserver = new ResizeObserver(fn)
      resizeObserver.observe(baseTableRef.value)
    }
  })
 
  const autoHeight = computed(() => {
    const height = currentHeight.value
    if (baseTableRef.value) {
      const rect = baseTableRef.value.getBoundingClientRect()
      const rectHeight = height || rect.height
      return rectHeight > 42 ? rectHeight - 42 : rectHeight
    }
  })
 
  ctx.expose({
    setCurrentRow,
  })
  return {
    getRowClassName,
    getTableStyle,
    autoHeight,
  }
}