1
1
<template >
2
- <KTable
2
+ <KTableView
3
+ ref =" $ref"
3
4
data-testid =" app-collection"
4
5
class =" app-collection"
5
- :headers =" props.headers"
6
- :fetcher-cache-key =" String(cacheKey)"
7
- :fetcher =" () => {
8
- return { data: props.items }
9
- }"
10
- :cell-attrs =" ({ headerKey }: CellAttrParams) => ({
6
+ :headers =" props.headers.map((item) => {
7
+ if (item.key === 'actions') {
8
+ item.key = '_actions'
9
+ }
10
+ return item
11
+ })"
12
+ :data =" typeof props.items === 'undefined' ? [] : props.items"
13
+ :cell-attrs =" ({ headerKey }) => ({
11
14
class: `${headerKey}-column`,
12
15
})"
13
16
:row-attrs =" getRowAttributes"
14
- :disable-sorting =" true"
15
- :disable-pagination =" true"
17
+ :hide-pagination =" true"
16
18
:resize-columns =" true"
17
19
:table-preferences =" {
18
20
columnWidths: props.headers.reduce<Record<string, number>>((prev, value) => {
19
- if(typeof value.width !== 'undefined') {
21
+ if (typeof value.width !== 'undefined') {
20
22
prev[value.key] = value.width
21
23
}
22
24
return prev
27
29
@update:table-preferences =" resize"
28
30
>
29
31
<template
30
- v-for =" key in Object .keys (slots )"
32
+ v-for =" key in Object .keys (slots ). map (( item ) => item === ' actions ' ? ' _actions ' : item ) "
31
33
:key =" key "
32
34
#[key ]=" { row } "
33
35
>
34
36
<slot
35
37
v-if =" (props.items ?? []).length > 0"
36
- :name =" key"
38
+ :name =" key === '_actions' ? 'actions' : key "
37
39
:row =" row as Row"
38
40
/>
39
41
</template >
40
- </KTable >
42
+ </KTableView >
41
43
</template >
42
44
43
45
<script lang="ts" setup generic =" Row extends {}" >
44
- import { KTable } from ' @kong/kongponents'
45
- import { ref , watch , Ref , inject } from ' vue'
46
+ import { KTableView } from ' @kong/kongponents'
47
+ import { ref , inject , onMounted } from ' vue'
48
+
46
49
47
50
import { runInDebug } from ' ../../'
48
- import type { TableHeader as KTableHeader , TablePreferences } from ' @kong/kongponents'
49
- type CellAttrParams = {
50
- headerKey: string
51
- row: Row
52
- rowIndex: number
53
- colIndex: number
54
- }
51
+ import type { TableViewHeader as KTableViewHeader , TablePreferences } from ' @kong/kongponents'
55
52
type ResizeValue = {
56
53
headers: Record <string , { width: number }>
57
54
}
58
55
59
- type TableHeader = KTableHeader & {
56
+ type TableHeader = KTableViewHeader & {
60
57
width? : number
61
58
}
62
59
60
+
63
61
// when we are inside of a DataLoader make sure its using the `variant="list"`
64
62
// but only error in dev mode, if this fails in production we don't want things
65
63
// to blow up
@@ -90,9 +88,6 @@ const slots = defineSlots<{
90
88
}) => any
91
89
}>()
92
90
93
- const items = ref (props .items ) as Ref <typeof props .items >
94
- const cacheKey = ref <number >(0 )
95
-
96
91
const resize = (args : TablePreferences ) => {
97
92
const headers = Object .entries (args .columnWidths ?? {}).reduce <Record <string , { width: number }>>((prev , [key , value ]) => {
98
93
prev [key ] = {
@@ -106,27 +101,20 @@ const resize = (args: TablePreferences) => {
106
101
})
107
102
}
108
103
109
- watch (() => props .items , (newItems , oldItems ) => {
110
- if (newItems !== oldItems ) {
111
- cacheKey .value ++
112
- items .value = props .items
113
- }
114
- })
115
-
116
- function getRowAttributes(row : Row ): Record <string , string > {
104
+ function getRowAttributes(row : Record <string , any >): Record <string , any > {
117
105
if (! row ) {
118
106
return {}
119
107
}
120
108
121
109
const attributes: Record <string , string > = {}
122
110
123
- if (typeof props .isSelectedRow !== ' undefined' && props .isSelectedRow (row )) {
111
+ if (typeof props .isSelectedRow !== ' undefined' && props .isSelectedRow (row as Row )) {
124
112
attributes .class = ' is-selected'
125
113
}
126
114
127
115
return attributes
128
116
}
129
- const click = (e : MouseEvent ) => {
117
+ const click = (e : Event ) => {
130
118
const $tr = (e .target as HTMLElement ).closest (' tr' )
131
119
if ($tr ) {
132
120
const $a: HTMLAnchorElement | null = [' td:first-child a' , ' [data-action]' ].reduce <HTMLAnchorElement | null >((prev , item ) => {
@@ -141,6 +129,18 @@ const click = (e: MouseEvent) => {
141
129
}
142
130
}
143
131
}
132
+ const $ref = ref <InstanceType <typeof KTableView > | null >(null )
133
+ const rewrite = () => {
134
+ const $el = $ref .value ?.$el
135
+ if (
136
+ ((el : any | HTMLElement ): el is HTMLElement => el && typeof el .querySelectorAll === ' function' )($el )
137
+ ) {
138
+ const $trs = $el .querySelectorAll (' tr[tabindex="0"]' )
139
+ ;[' tabindex' ].forEach (attr => Array .from ($trs ).forEach (item => item .removeAttribute (attr )))
140
+ }
141
+ }
142
+ onMounted (rewrite )
143
+
144
144
</script >
145
145
146
146
<style lang="scss" scoped>
@@ -149,20 +149,22 @@ const click = (e: MouseEvent) => {
149
149
font-weight : $kui-font-weight-semibold ;
150
150
text-decoration : none ;
151
151
}
152
+
152
153
.app-collection :deep(td :first-child li a ) {
153
154
color : $kui-color-text-primary ;
154
155
font-weight : $kui-font-weight-regular ;
155
156
}
157
+
156
158
.app-collection :deep(td :first-child li a :hover ) {
157
159
text-decoration : underline ;
158
160
}
159
161
</style >
160
162
161
163
<style lang="scss">
162
-
163
- .app-collection .actions-column {
164
+ .app-collection ._actions-column {
164
165
width : 48px ;
165
166
}
167
+
166
168
.app-collection .is-selected {
167
169
background-color : $kui-color-background-neutral-weakest ;
168
170
}
0 commit comments