55
55
v-for =" (rowData, rowIndex) in workingList"
56
56
:key =" rowIndex"
57
57
class =" rounded-xl text-secondaryDark hover:cursor-pointer hover:bg-divider"
58
- :class =" { 'divide-x divide-divider': showYBorder }"
58
+ :class =" [
59
+ { 'divide-x divide-divider': showYBorder },
60
+ getRowCustomStyle(rowData),
61
+ ]"
59
62
@click =" onRowClicked(rowData)"
60
63
>
61
64
<td v-if =" selectedRows" >
95
98
import { useVModel } from " @vueuse/core"
96
99
import { isEqual } from " lodash-es"
97
100
import { computed , ref , watch , watchEffect } from " vue"
98
- import { HoppSmartSpinner } from " .. "
101
+ import HoppSmartSpinner from " ./Spinner.vue "
99
102
100
103
export type CellHeading = {
101
104
key: string
102
105
label? : string
103
106
preventClick? : boolean
104
107
}
105
108
106
- export type Item = Record <string , unknown >
109
+ export type RowStyle = {
110
+ classes? : string
111
+ style? : Record <string , string >
112
+ }
113
+
114
+ export type Item <T = Record <string , unknown >> = T
107
115
108
116
const props = withDefaults (
109
117
defineProps <{
110
- /** Whether to show the vertical border between columns */
118
+ // Whether to show vertical borders between cells
111
119
showYBorder? : boolean
112
- /** The list of items to be displayed in the table */
113
- list: Item []
114
- /** The headings of the table */
120
+
121
+ // List of items to be displayed in the table
122
+ list: Item <any >[]
123
+
124
+ // Headings for the table
115
125
headings? : CellHeading []
116
126
117
- /** Contains the rows selected */
118
- selectedRows? : Item []
127
+ // Currently selected rows
128
+ selectedRows? : Item < any > []
119
129
120
- /** Whether to enable sorting */
130
+ // Sorting configurations
121
131
sort? : {
122
- /** The key to sort the list by */
123
132
key: string
124
133
direction: Direction
125
134
}
126
135
127
- /** Whether to show a loading spinner */
136
+ // Whether the table is in loading state
128
137
loading? : boolean
138
+
139
+ /**
140
+ * Custom styles for rows:
141
+ * - Object: key-based (determined by getRowStyleKey)
142
+ * - String: applied as CSS class to all rows
143
+ */
144
+ rowStyles? : Record <string , RowStyle > | string
145
+ /**
146
+ * Function to determine which row style to apply
147
+ * Returns rowStyles directly if it's a string
148
+ * R eturns a key that exists in rowStyles (when rowStyles is an object), or null if no style should be applied
149
+ */
150
+ getRowStyleKey? : (rowData : Item <any >) => string | null
129
151
}>(),
130
152
{
131
153
showYBorder: false ,
132
154
sort: undefined ,
133
155
selectedRows: undefined ,
134
156
loading: false ,
157
+ rowStyles : () => ({}),
158
+ getRowStyleKey : () => null ,
135
159
},
136
160
)
137
161
138
162
const emit = defineEmits <{
139
- (event : " onRowClicked" , item : Item ): void
140
- (event : " update:list" , list : Item []): void
141
- (event : " update:selectedRows" , selectedRows : Item []): void
163
+ (event : " onRowClicked" , item : Item < any > ): void
164
+ (event : " update:list" , list : Item < any > []): void
165
+ (event : " update:selectedRows" , selectedRows : Item < any > []): void
142
166
}>()
143
167
144
- // The working version of the list that is used to perform operations upon
145
168
const workingList = useVModel (props , " list" , emit )
146
-
147
- // Checkbox functionality
148
169
const selectedRows = useVModel (props , " selectedRows" , emit )
149
170
150
171
watch (workingList .value , (updatedList ) => {
@@ -156,14 +177,14 @@ watch(workingList.value, (updatedList) => {
156
177
}
157
178
})
158
179
159
- const onRowClicked = (item : Item ) => emit (" onRowClicked" , item )
180
+ const onRowClicked = (item : Item < any > ) => emit (" onRowClicked" , item )
160
181
161
- const isRowSelected = (item : Item ) => {
182
+ const isRowSelected = (item : Item < any > ) => {
162
183
const { selected, ... data } = item
163
184
return selectedRows .value ?.some ((row ) => isEqual (row , data ))
164
185
}
165
186
166
- const toggleRow = (item : Item ) => {
187
+ const toggleRow = (item : Item < any > ) => {
167
188
item .selected = ! item .selected
168
189
const { selected, ... data } = item
169
190
@@ -212,7 +233,16 @@ watchEffect(() => {
212
233
}
213
234
})
214
235
215
- // Sort List by key and direction which can set to ascending or descending
236
+ const getRowCustomStyle = (rowData : Item <any >) => {
237
+ if (typeof props .rowStyles === " string" ) {
238
+ return props .rowStyles
239
+ }
240
+
241
+ const styleKey = props .getRowStyleKey ?.(rowData )
242
+ if (! styleKey || ! props .rowStyles [styleKey ]) return " "
243
+ return props .rowStyles [styleKey ].classes || " "
244
+ }
245
+
216
246
export type Direction = " ascending" | " descending"
217
247
218
248
const sortList = (key : string , direction : Direction ) => {
0 commit comments