resolver-egretimp-plus 0.1.29 → 0.1.31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/h5/index.js +18 -18
- package/dist/web/index.js +128 -128
- package/dist/web/index.js.LICENSE.txt +15 -0
- package/package.json +5 -1
- package/scripts/webpack.config.js +18 -2
- package/src/components/loading/loading.scss +1 -1
- package/src/components/packages-web/CustomComponentTable.jsx +8 -5
- package/src/components/table/index.ts +29 -0
- package/src/components/table/src/composables/use-scrollbar.ts +30 -0
- package/src/components/table/src/config.ts +256 -0
- package/src/components/table/src/filter-panel.vue +260 -0
- package/src/components/table/src/h-helper.ts +34 -0
- package/src/components/table/src/layout-observer.ts +78 -0
- package/src/components/table/src/store/current.ts +85 -0
- package/src/components/table/src/store/expand.ts +76 -0
- package/src/components/table/src/store/helper.ts +74 -0
- package/src/components/table/src/store/index.ts +246 -0
- package/src/components/table/src/store/tree.ts +230 -0
- package/src/components/table/src/store/watcher.ts +543 -0
- package/src/components/table/src/table/defaults.ts +402 -0
- package/src/components/table/src/table/key-render-helper.ts +27 -0
- package/src/components/table/src/table/style-helper.ts +378 -0
- package/src/components/table/src/table/utils-helper.ts +47 -0
- package/src/components/table/src/table-body/defaults.ts +52 -0
- package/src/components/table/src/table-body/events-helper.ts +203 -0
- package/src/components/table/src/table-body/index.ts +119 -0
- package/src/components/table/src/table-body/render-helper.ts +283 -0
- package/src/components/table/src/table-body/styles-helper.ts +164 -0
- package/src/components/table/src/table-column/defaults.ts +237 -0
- package/src/components/table/src/table-column/index.ts +202 -0
- package/src/components/table/src/table-column/render-helper.ts +214 -0
- package/src/components/table/src/table-column/watcher-helper.ts +88 -0
- package/src/components/table/src/table-footer/index.ts +128 -0
- package/src/components/table/src/table-footer/mapState-helper.ts +33 -0
- package/src/components/table/src/table-footer/style-helper.ts +51 -0
- package/src/components/table/src/table-header/event-helper.ts +213 -0
- package/src/components/table/src/table-header/index.ts +244 -0
- package/src/components/table/src/table-header/style.helper.ts +119 -0
- package/src/components/table/src/table-header/utils-helper.ts +94 -0
- package/src/components/table/src/table-layout.ts +259 -0
- package/src/components/table/src/table.vue +389 -0
- package/src/components/table/src/tableColumn.ts +3 -0
- package/src/components/table/src/tokens.ts +5 -0
- package/src/components/table/src/util.ts +521 -0
- package/src/components/table/style/css.ts +5 -0
- package/src/components/table/style/index.ts +5 -0
- package/src/rules/parseCondition.js +3 -0
- package/src/rules/rulesDriver.js +2 -2
- package/tsconfig.json +19 -0
- package/vue-shims.d.ts +4 -0
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
import {
|
|
3
|
+
defineComponent,
|
|
4
|
+
getCurrentInstance,
|
|
5
|
+
h,
|
|
6
|
+
inject,
|
|
7
|
+
onUnmounted,
|
|
8
|
+
watch,
|
|
9
|
+
} from 'vue'
|
|
10
|
+
import { addClass, isClient, rAF, removeClass } from 'element-plus/es/utils/index.mjs'
|
|
11
|
+
import { useNamespace } from 'element-plus/es/hooks/index.mjs'
|
|
12
|
+
import useLayoutObserver from '../layout-observer'
|
|
13
|
+
import { removePopper } from '../util'
|
|
14
|
+
import { TABLE_INJECTION_KEY } from '../tokens'
|
|
15
|
+
import useRender from './render-helper'
|
|
16
|
+
import defaultProps from './defaults'
|
|
17
|
+
|
|
18
|
+
import type { VNode } from 'vue'
|
|
19
|
+
|
|
20
|
+
export default defineComponent({
|
|
21
|
+
name: 'ElTableBody',
|
|
22
|
+
props: defaultProps,
|
|
23
|
+
setup(props) {
|
|
24
|
+
const instance = getCurrentInstance()
|
|
25
|
+
const parent = inject(TABLE_INJECTION_KEY)
|
|
26
|
+
const ns = useNamespace('table')
|
|
27
|
+
const { wrappedRowRender, tooltipContent, tooltipTrigger } =
|
|
28
|
+
useRender(props)
|
|
29
|
+
const { onColumnsChange, onScrollableChange } = useLayoutObserver(parent!)
|
|
30
|
+
|
|
31
|
+
const hoveredCellList = []
|
|
32
|
+
watch(props.store.states.hoverRow, (newVal: any, oldVal: any) => {
|
|
33
|
+
const el = instance?.vnode.el as HTMLElement
|
|
34
|
+
const rows = Array.from(el?.children || []).filter((e) =>
|
|
35
|
+
e?.classList.contains(`${ns.e('row')}`)
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
// hover rowSpan > 1 choose the whole row
|
|
39
|
+
let rowNum = newVal
|
|
40
|
+
const childNodes = rows[rowNum]?.childNodes
|
|
41
|
+
if (childNodes?.length) {
|
|
42
|
+
let control = 0
|
|
43
|
+
const indexes = Array.from(childNodes).reduce((acc, item, index) => {
|
|
44
|
+
// drop colsSpan
|
|
45
|
+
if (childNodes[index]?.colSpan > 1) {
|
|
46
|
+
control = childNodes[index]?.colSpan
|
|
47
|
+
}
|
|
48
|
+
if (item.nodeName !== 'TD' && control === 0) {
|
|
49
|
+
acc.push(index)
|
|
50
|
+
}
|
|
51
|
+
control > 0 && control--
|
|
52
|
+
return acc
|
|
53
|
+
}, [])
|
|
54
|
+
|
|
55
|
+
indexes.forEach((rowIndex) => {
|
|
56
|
+
rowNum = newVal
|
|
57
|
+
while (rowNum > 0) {
|
|
58
|
+
// find from previous
|
|
59
|
+
const preChildNodes = rows[rowNum - 1]?.childNodes
|
|
60
|
+
if (
|
|
61
|
+
preChildNodes[rowIndex] &&
|
|
62
|
+
preChildNodes[rowIndex].nodeName === 'TD' &&
|
|
63
|
+
preChildNodes[rowIndex].rowSpan > 1
|
|
64
|
+
) {
|
|
65
|
+
addClass(preChildNodes[rowIndex], 'hover-cell')
|
|
66
|
+
hoveredCellList.push(preChildNodes[rowIndex])
|
|
67
|
+
break
|
|
68
|
+
}
|
|
69
|
+
rowNum--
|
|
70
|
+
}
|
|
71
|
+
})
|
|
72
|
+
} else {
|
|
73
|
+
hoveredCellList.forEach((item) => removeClass(item, 'hover-cell'))
|
|
74
|
+
hoveredCellList.length = 0
|
|
75
|
+
}
|
|
76
|
+
if (!props.store.states.isComplex.value || !isClient) return
|
|
77
|
+
|
|
78
|
+
rAF(() => {
|
|
79
|
+
// just get first level children; fix #9723
|
|
80
|
+
const oldRow = rows[oldVal]
|
|
81
|
+
const newRow = rows[newVal]
|
|
82
|
+
// when there is fixed row, hover on rowSpan > 1 should not clear the class
|
|
83
|
+
if (oldRow && !oldRow.classList.contains('hover-fixed-row')) {
|
|
84
|
+
removeClass(oldRow, 'hover-row')
|
|
85
|
+
}
|
|
86
|
+
if (newRow) {
|
|
87
|
+
addClass(newRow, 'hover-row')
|
|
88
|
+
}
|
|
89
|
+
})
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
onUnmounted(() => {
|
|
93
|
+
removePopper?.()
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
return {
|
|
97
|
+
ns,
|
|
98
|
+
onColumnsChange,
|
|
99
|
+
onScrollableChange,
|
|
100
|
+
wrappedRowRender,
|
|
101
|
+
tooltipContent,
|
|
102
|
+
tooltipTrigger,
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
render() {
|
|
106
|
+
const { wrappedRowRender, store } = this
|
|
107
|
+
const data = store.states.data.value || []
|
|
108
|
+
// Why do we need tabIndex: -1 ?
|
|
109
|
+
// If you set the tabindex attribute on an element ,
|
|
110
|
+
// then its child content cannot be scrolled with the arrow keys,
|
|
111
|
+
// unless you set tabindex on the content too
|
|
112
|
+
// See https://github.com/facebook/react/issues/25462#issuecomment-1274775248 or https://developer.mozilla.org/zh-CN/docs/Web/HTML/Global_attributes/tabindex
|
|
113
|
+
return h('tbody', { tabIndex: -1 }, [
|
|
114
|
+
data.reduce((acc: VNode[], row) => {
|
|
115
|
+
return acc.concat(wrappedRowRender(row, acc.length))
|
|
116
|
+
}, []),
|
|
117
|
+
])
|
|
118
|
+
},
|
|
119
|
+
})
|
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
import { computed, h, inject } from 'vue'
|
|
3
|
+
import { merge } from 'lodash-unified'
|
|
4
|
+
import { useNamespace } from 'element-plus/es/hooks/index.mjs'
|
|
5
|
+
import { getRowIdentity } from '../util'
|
|
6
|
+
import { TABLE_INJECTION_KEY } from '../tokens'
|
|
7
|
+
import useEvents from './events-helper'
|
|
8
|
+
import useStyles from './styles-helper'
|
|
9
|
+
import type { TableBodyProps } from './defaults'
|
|
10
|
+
import type { RenderRowData, TableProps, TreeNode } from '../table/defaults'
|
|
11
|
+
|
|
12
|
+
function useRender<T>(props: Partial<TableBodyProps<T>>) {
|
|
13
|
+
const parent = inject(TABLE_INJECTION_KEY)
|
|
14
|
+
const ns = useNamespace('table')
|
|
15
|
+
const {
|
|
16
|
+
handleDoubleClick,
|
|
17
|
+
handleClick,
|
|
18
|
+
handleContextMenu,
|
|
19
|
+
handleMouseEnter,
|
|
20
|
+
handleMouseLeave,
|
|
21
|
+
handleCellMouseEnter,
|
|
22
|
+
handleCellMouseLeave,
|
|
23
|
+
tooltipContent,
|
|
24
|
+
tooltipTrigger,
|
|
25
|
+
} = useEvents(props)
|
|
26
|
+
const {
|
|
27
|
+
getRowStyle,
|
|
28
|
+
getRowClass,
|
|
29
|
+
getCellStyle,
|
|
30
|
+
getCellClass,
|
|
31
|
+
getSpan,
|
|
32
|
+
getColspanRealWidth,
|
|
33
|
+
} = useStyles(props)
|
|
34
|
+
const firstDefaultColumnIndex = computed(() => {
|
|
35
|
+
return props.store.states.columns.value.findIndex(
|
|
36
|
+
({ type }) => type === 'default'
|
|
37
|
+
)
|
|
38
|
+
})
|
|
39
|
+
const getKeyOfRow = (row: T, index: number) => {
|
|
40
|
+
const rowKey = (parent.props as Partial<TableProps<T>>).rowKey
|
|
41
|
+
if (rowKey) {
|
|
42
|
+
return getRowIdentity(row, rowKey)
|
|
43
|
+
}
|
|
44
|
+
return index
|
|
45
|
+
}
|
|
46
|
+
const rowRender = (
|
|
47
|
+
row: T,
|
|
48
|
+
$index: number,
|
|
49
|
+
treeRowData?: TreeNode,
|
|
50
|
+
expanded = false
|
|
51
|
+
) => {
|
|
52
|
+
const { tooltipEffect, tooltipOptions, store } = props
|
|
53
|
+
const { indent, columns } = store.states
|
|
54
|
+
const rowClasses = getRowClass(row, $index)
|
|
55
|
+
let display = true
|
|
56
|
+
if (treeRowData) {
|
|
57
|
+
rowClasses.push(ns.em('row', `level-${treeRowData.level}`))
|
|
58
|
+
display = treeRowData.display
|
|
59
|
+
}
|
|
60
|
+
const displayStyle = display
|
|
61
|
+
? null
|
|
62
|
+
: {
|
|
63
|
+
display: 'none',
|
|
64
|
+
}
|
|
65
|
+
return h(
|
|
66
|
+
'tr',
|
|
67
|
+
{
|
|
68
|
+
style: [displayStyle, getRowStyle(row, $index)],
|
|
69
|
+
class: rowClasses,
|
|
70
|
+
key: getKeyOfRow(row, $index),
|
|
71
|
+
onDblclick: ($event) => handleDoubleClick($event, row),
|
|
72
|
+
onClick: ($event) => handleClick($event, row),
|
|
73
|
+
onContextmenu: ($event) => handleContextMenu($event, row),
|
|
74
|
+
onMouseenter: () => handleMouseEnter($index),
|
|
75
|
+
onMouseleave: handleMouseLeave,
|
|
76
|
+
},
|
|
77
|
+
columns.value.map((column, cellIndex) => {
|
|
78
|
+
const { rowspan, colspan } = getSpan(row, column, $index, cellIndex)
|
|
79
|
+
if (!rowspan || !colspan) {
|
|
80
|
+
return null
|
|
81
|
+
}
|
|
82
|
+
const columnData = Object.assign({}, column)
|
|
83
|
+
columnData.realWidth = getColspanRealWidth(
|
|
84
|
+
columns.value,
|
|
85
|
+
colspan,
|
|
86
|
+
cellIndex
|
|
87
|
+
)
|
|
88
|
+
const data: RenderRowData<T> = {
|
|
89
|
+
store: props.store,
|
|
90
|
+
_self: props.context || parent,
|
|
91
|
+
column: columnData,
|
|
92
|
+
row,
|
|
93
|
+
$index,
|
|
94
|
+
cellIndex,
|
|
95
|
+
expanded,
|
|
96
|
+
}
|
|
97
|
+
if (cellIndex === firstDefaultColumnIndex.value && treeRowData) {
|
|
98
|
+
data.treeNode = {
|
|
99
|
+
indent: treeRowData.level * indent.value,
|
|
100
|
+
level: treeRowData.level,
|
|
101
|
+
}
|
|
102
|
+
if (typeof treeRowData.expanded === 'boolean') {
|
|
103
|
+
data.treeNode.expanded = treeRowData.expanded
|
|
104
|
+
// 表明是懒加载
|
|
105
|
+
if ('loading' in treeRowData) {
|
|
106
|
+
data.treeNode.loading = treeRowData.loading
|
|
107
|
+
}
|
|
108
|
+
if ('noLazyChildren' in treeRowData) {
|
|
109
|
+
data.treeNode.noLazyChildren = treeRowData.noLazyChildren
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
const baseKey = `${getKeyOfRow(row, $index)},${cellIndex}`
|
|
114
|
+
const patchKey = columnData.columnKey || columnData.rawColumnKey || ''
|
|
115
|
+
const tdChildren = cellChildren(cellIndex, column, data)
|
|
116
|
+
|
|
117
|
+
let mergedTooltipOptions =
|
|
118
|
+
column.showOverflowTooltip &&
|
|
119
|
+
merge(
|
|
120
|
+
{
|
|
121
|
+
effect: tooltipEffect,
|
|
122
|
+
},
|
|
123
|
+
tooltipOptions,
|
|
124
|
+
column.showOverflowTooltip
|
|
125
|
+
)
|
|
126
|
+
if (typeof column.showOverflowTooltip === 'function') {
|
|
127
|
+
mergedTooltipOptions.showOverflowTooltip = () => {
|
|
128
|
+
return column.showOverflowTooltip(data)
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return h(
|
|
132
|
+
'td',
|
|
133
|
+
{
|
|
134
|
+
style: getCellStyle($index, cellIndex, row, column),
|
|
135
|
+
class: getCellClass($index, cellIndex, row, column, colspan - 1),
|
|
136
|
+
key: `${patchKey}${baseKey}`,
|
|
137
|
+
rowspan,
|
|
138
|
+
colspan,
|
|
139
|
+
onMouseenter: ($event) =>
|
|
140
|
+
handleCellMouseEnter($event, row, mergedTooltipOptions),
|
|
141
|
+
onMouseleave: handleCellMouseLeave,
|
|
142
|
+
},
|
|
143
|
+
[tdChildren]
|
|
144
|
+
)
|
|
145
|
+
})
|
|
146
|
+
)
|
|
147
|
+
}
|
|
148
|
+
const cellChildren = (cellIndex, column, data) => {
|
|
149
|
+
return column.renderCell(data)
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const wrappedRowRender = (row: T, $index: number) => {
|
|
153
|
+
const store = props.store
|
|
154
|
+
const { isRowExpanded, assertRowKey } = store
|
|
155
|
+
const { treeData, lazyTreeNodeMap, childrenColumnName, rowKey } =
|
|
156
|
+
store.states
|
|
157
|
+
const columns = store.states.columns.value
|
|
158
|
+
const hasExpandColumn = columns.some(({ type }) => type === 'expand')
|
|
159
|
+
if (hasExpandColumn) {
|
|
160
|
+
const expanded = isRowExpanded(row)
|
|
161
|
+
const tr = rowRender(row, $index, undefined, expanded)
|
|
162
|
+
const renderExpanded = parent.renderExpanded
|
|
163
|
+
if (expanded) {
|
|
164
|
+
if (!renderExpanded) {
|
|
165
|
+
console.error('[Element Error]renderExpanded is required.')
|
|
166
|
+
return tr
|
|
167
|
+
}
|
|
168
|
+
// 使用二维数组,避免修改 $index
|
|
169
|
+
// Use a matrix to avoid modifying $index
|
|
170
|
+
return [
|
|
171
|
+
[
|
|
172
|
+
tr,
|
|
173
|
+
h(
|
|
174
|
+
'tr',
|
|
175
|
+
{
|
|
176
|
+
key: `expanded-row__${tr.key as string}`,
|
|
177
|
+
},
|
|
178
|
+
[
|
|
179
|
+
h(
|
|
180
|
+
'td',
|
|
181
|
+
{
|
|
182
|
+
colspan: columns.length,
|
|
183
|
+
class: `${ns.e('cell')} ${ns.e('expanded-cell')}`,
|
|
184
|
+
},
|
|
185
|
+
[renderExpanded({ row, $index, store, expanded })]
|
|
186
|
+
),
|
|
187
|
+
]
|
|
188
|
+
),
|
|
189
|
+
],
|
|
190
|
+
]
|
|
191
|
+
} else {
|
|
192
|
+
// 使用二维数组,避免修改 $index
|
|
193
|
+
// Use a two dimensional array avoid modifying $index
|
|
194
|
+
return [[tr]]
|
|
195
|
+
}
|
|
196
|
+
} else if (Object.keys(treeData.value).length) {
|
|
197
|
+
assertRowKey()
|
|
198
|
+
// TreeTable 时,rowKey 必须由用户设定,不使用 getKeyOfRow 计算
|
|
199
|
+
// 在调用 rowRender 函数时,仍然会计算 rowKey,不太好的操作
|
|
200
|
+
const key = getRowIdentity(row, rowKey.value)
|
|
201
|
+
let cur = treeData.value[key]
|
|
202
|
+
let treeRowData = null
|
|
203
|
+
if (cur) {
|
|
204
|
+
treeRowData = {
|
|
205
|
+
expanded: cur.expanded,
|
|
206
|
+
level: cur.level,
|
|
207
|
+
display: true,
|
|
208
|
+
}
|
|
209
|
+
if (typeof cur.lazy === 'boolean') {
|
|
210
|
+
if (typeof cur.loaded === 'boolean' && cur.loaded) {
|
|
211
|
+
treeRowData.noLazyChildren = !(cur.children && cur.children.length)
|
|
212
|
+
}
|
|
213
|
+
treeRowData.loading = cur.loading
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
const tmp = [rowRender(row, $index, treeRowData)]
|
|
217
|
+
// 渲染嵌套数据
|
|
218
|
+
if (cur) {
|
|
219
|
+
// currentRow 记录的是 index,所以还需主动增加 TreeTable 的 index
|
|
220
|
+
let i = 0
|
|
221
|
+
const traverse = (children, parent) => {
|
|
222
|
+
if (!(children && children.length && parent)) return
|
|
223
|
+
children.forEach((node) => {
|
|
224
|
+
// 父节点的 display 状态影响子节点的显示状态
|
|
225
|
+
const innerTreeRowData = {
|
|
226
|
+
display: parent.display && parent.expanded,
|
|
227
|
+
level: parent.level + 1,
|
|
228
|
+
expanded: false,
|
|
229
|
+
noLazyChildren: false,
|
|
230
|
+
loading: false,
|
|
231
|
+
}
|
|
232
|
+
const childKey = getRowIdentity(node, rowKey.value)
|
|
233
|
+
if (childKey === undefined || childKey === null) {
|
|
234
|
+
throw new Error('For nested data item, row-key is required.')
|
|
235
|
+
}
|
|
236
|
+
cur = { ...treeData.value[childKey] }
|
|
237
|
+
// 对于当前节点,分成有无子节点两种情况。
|
|
238
|
+
// 如果包含子节点的,设置 expanded 属性。
|
|
239
|
+
// 对于它子节点的 display 属性由它本身的 expanded 与 display 共同决定。
|
|
240
|
+
if (cur) {
|
|
241
|
+
innerTreeRowData.expanded = cur.expanded
|
|
242
|
+
// 懒加载的某些节点,level 未知
|
|
243
|
+
cur.level = cur.level || innerTreeRowData.level
|
|
244
|
+
cur.display = !!(cur.expanded && innerTreeRowData.display)
|
|
245
|
+
if (typeof cur.lazy === 'boolean') {
|
|
246
|
+
if (typeof cur.loaded === 'boolean' && cur.loaded) {
|
|
247
|
+
innerTreeRowData.noLazyChildren = !(
|
|
248
|
+
cur.children && cur.children.length
|
|
249
|
+
)
|
|
250
|
+
}
|
|
251
|
+
innerTreeRowData.loading = cur.loading
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
i++
|
|
255
|
+
tmp.push(rowRender(node, $index + i, innerTreeRowData))
|
|
256
|
+
if (cur) {
|
|
257
|
+
const nodes =
|
|
258
|
+
lazyTreeNodeMap.value[childKey] ||
|
|
259
|
+
node[childrenColumnName.value]
|
|
260
|
+
traverse(nodes, cur)
|
|
261
|
+
}
|
|
262
|
+
})
|
|
263
|
+
}
|
|
264
|
+
// 对于 root 节点,display 一定为 true
|
|
265
|
+
cur.display = true
|
|
266
|
+
const nodes =
|
|
267
|
+
lazyTreeNodeMap.value[key] || row[childrenColumnName.value]
|
|
268
|
+
traverse(nodes, cur)
|
|
269
|
+
}
|
|
270
|
+
return tmp
|
|
271
|
+
} else {
|
|
272
|
+
return rowRender(row, $index, undefined)
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
return {
|
|
277
|
+
wrappedRowRender,
|
|
278
|
+
tooltipContent,
|
|
279
|
+
tooltipTrigger,
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
export default useRender
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
import { inject } from 'vue'
|
|
3
|
+
import { useNamespace } from 'element-plus/es/hooks/index.mjs'
|
|
4
|
+
import {
|
|
5
|
+
ensurePosition,
|
|
6
|
+
getFixedColumnOffset,
|
|
7
|
+
getFixedColumnsClass,
|
|
8
|
+
} from '../util'
|
|
9
|
+
import { TABLE_INJECTION_KEY } from '../tokens'
|
|
10
|
+
import type { TableColumnCtx } from '../table-column/defaults'
|
|
11
|
+
import type { TableBodyProps } from './defaults'
|
|
12
|
+
|
|
13
|
+
function useStyles<T>(props: Partial<TableBodyProps<T>>) {
|
|
14
|
+
const parent = inject(TABLE_INJECTION_KEY)
|
|
15
|
+
const ns = useNamespace('table')
|
|
16
|
+
|
|
17
|
+
const getRowStyle = (row: T, rowIndex: number) => {
|
|
18
|
+
const rowStyle = parent?.props.rowStyle
|
|
19
|
+
if (typeof rowStyle === 'function') {
|
|
20
|
+
return rowStyle.call(null, {
|
|
21
|
+
row,
|
|
22
|
+
rowIndex,
|
|
23
|
+
})
|
|
24
|
+
}
|
|
25
|
+
return rowStyle || null
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const getRowClass = (row: T, rowIndex: number) => {
|
|
29
|
+
const classes = [ns.e('row')]
|
|
30
|
+
if (
|
|
31
|
+
parent?.props.highlightCurrentRow &&
|
|
32
|
+
row === props.store.states.currentRow.value
|
|
33
|
+
) {
|
|
34
|
+
classes.push('current-row')
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (props.stripe && rowIndex % 2 === 1) {
|
|
38
|
+
classes.push(ns.em('row', 'striped'))
|
|
39
|
+
}
|
|
40
|
+
const rowClassName = parent?.props.rowClassName
|
|
41
|
+
if (typeof rowClassName === 'string') {
|
|
42
|
+
classes.push(rowClassName)
|
|
43
|
+
} else if (typeof rowClassName === 'function') {
|
|
44
|
+
classes.push(
|
|
45
|
+
rowClassName.call(null, {
|
|
46
|
+
row,
|
|
47
|
+
rowIndex,
|
|
48
|
+
})
|
|
49
|
+
)
|
|
50
|
+
}
|
|
51
|
+
return classes
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const getCellStyle = (
|
|
55
|
+
rowIndex: number,
|
|
56
|
+
columnIndex: number,
|
|
57
|
+
row: T,
|
|
58
|
+
column: TableColumnCtx<T>
|
|
59
|
+
) => {
|
|
60
|
+
const cellStyle = parent?.props.cellStyle
|
|
61
|
+
let cellStyles = cellStyle ?? {}
|
|
62
|
+
if (typeof cellStyle === 'function') {
|
|
63
|
+
cellStyles = cellStyle.call(null, {
|
|
64
|
+
rowIndex,
|
|
65
|
+
columnIndex,
|
|
66
|
+
row,
|
|
67
|
+
column,
|
|
68
|
+
})
|
|
69
|
+
}
|
|
70
|
+
const fixedStyle = getFixedColumnOffset(
|
|
71
|
+
columnIndex,
|
|
72
|
+
props?.fixed,
|
|
73
|
+
props.store
|
|
74
|
+
)
|
|
75
|
+
ensurePosition(fixedStyle, 'left')
|
|
76
|
+
ensurePosition(fixedStyle, 'right')
|
|
77
|
+
return Object.assign({}, cellStyles, fixedStyle)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const getCellClass = (
|
|
81
|
+
rowIndex: number,
|
|
82
|
+
columnIndex: number,
|
|
83
|
+
row: T,
|
|
84
|
+
column: TableColumnCtx<T>,
|
|
85
|
+
offset: number
|
|
86
|
+
) => {
|
|
87
|
+
const fixedClasses = getFixedColumnsClass(
|
|
88
|
+
ns.b(),
|
|
89
|
+
columnIndex,
|
|
90
|
+
props?.fixed,
|
|
91
|
+
props.store,
|
|
92
|
+
undefined,
|
|
93
|
+
offset
|
|
94
|
+
)
|
|
95
|
+
const classes = [column.id, column.align, column.className, ...fixedClasses]
|
|
96
|
+
const cellClassName = parent?.props.cellClassName
|
|
97
|
+
if (typeof cellClassName === 'string') {
|
|
98
|
+
classes.push(cellClassName)
|
|
99
|
+
} else if (typeof cellClassName === 'function') {
|
|
100
|
+
classes.push(
|
|
101
|
+
cellClassName.call(null, {
|
|
102
|
+
rowIndex,
|
|
103
|
+
columnIndex,
|
|
104
|
+
row,
|
|
105
|
+
column,
|
|
106
|
+
})
|
|
107
|
+
)
|
|
108
|
+
}
|
|
109
|
+
classes.push(ns.e('cell'))
|
|
110
|
+
return classes.filter((className) => Boolean(className)).join(' ')
|
|
111
|
+
}
|
|
112
|
+
const getSpan = (
|
|
113
|
+
row: T,
|
|
114
|
+
column: TableColumnCtx<T>,
|
|
115
|
+
rowIndex: number,
|
|
116
|
+
columnIndex: number
|
|
117
|
+
) => {
|
|
118
|
+
let rowspan = 1
|
|
119
|
+
let colspan = 1
|
|
120
|
+
const fn = parent?.props.spanMethod
|
|
121
|
+
if (typeof fn === 'function') {
|
|
122
|
+
const result = fn({
|
|
123
|
+
row,
|
|
124
|
+
column,
|
|
125
|
+
rowIndex,
|
|
126
|
+
columnIndex,
|
|
127
|
+
})
|
|
128
|
+
if (Array.isArray(result)) {
|
|
129
|
+
rowspan = result[0]
|
|
130
|
+
colspan = result[1]
|
|
131
|
+
} else if (typeof result === 'object') {
|
|
132
|
+
rowspan = result.rowspan
|
|
133
|
+
colspan = result.colspan
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return { rowspan, colspan }
|
|
137
|
+
}
|
|
138
|
+
const getColspanRealWidth = (
|
|
139
|
+
columns: TableColumnCtx<T>[],
|
|
140
|
+
colspan: number,
|
|
141
|
+
index: number
|
|
142
|
+
): number => {
|
|
143
|
+
if (colspan < 1) {
|
|
144
|
+
return columns[index].realWidth
|
|
145
|
+
}
|
|
146
|
+
const widthArr = columns
|
|
147
|
+
.map(({ realWidth, width }) => realWidth || width)
|
|
148
|
+
.slice(index, index + colspan)
|
|
149
|
+
return Number(
|
|
150
|
+
widthArr.reduce((acc, width) => Number(acc) + Number(width), -1)
|
|
151
|
+
)
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return {
|
|
155
|
+
getRowStyle,
|
|
156
|
+
getRowClass,
|
|
157
|
+
getCellStyle,
|
|
158
|
+
getCellClass,
|
|
159
|
+
getSpan,
|
|
160
|
+
getColspanRealWidth,
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export default useStyles
|