5555 v-for =" (rowData, rowIndex) in workingList"
5656 :key =" rowIndex"
5757 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+ ]"
5962 @click =" onRowClicked(rowData)"
6063 >
6164 <td v-if =" selectedRows" >
9598import { useVModel } from " @vueuse/core"
9699import { isEqual } from " lodash-es"
97100import { computed , ref , watch , watchEffect } from " vue"
98- import { HoppSmartSpinner } from " .. "
101+ import HoppSmartSpinner from " ./Spinner.vue "
99102
100103export type CellHeading = {
101104 key: string
102105 label? : string
103106 preventClick? : boolean
104107}
105108
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
107115
108116const props = withDefaults (
109117 defineProps <{
110- /** Whether to show the vertical border between columns */
118+ // Whether to show vertical borders between cells
111119 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
115125 headings? : CellHeading []
116126
117- /** Contains the rows selected */
118- selectedRows? : Item []
127+ // Currently selected rows
128+ selectedRows? : Item < any > []
119129
120- /** Whether to enable sorting */
130+ // Sorting configurations
121131 sort? : {
122- /** The key to sort the list by */
123132 key: string
124133 direction: Direction
125134 }
126135
127- /** Whether to show a loading spinner */
136+ // Whether the table is in loading state
128137 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
129151 }>(),
130152 {
131153 showYBorder: false ,
132154 sort: undefined ,
133155 selectedRows: undefined ,
134156 loading: false ,
157+ rowStyles : () => ({}),
158+ getRowStyleKey : () => null ,
135159 },
136160)
137161
138162const 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
142166}>()
143167
144- // The working version of the list that is used to perform operations upon
145168const workingList = useVModel (props , " list" , emit )
146-
147- // Checkbox functionality
148169const selectedRows = useVModel (props , " selectedRows" , emit )
149170
150171watch (workingList .value , (updatedList ) => {
@@ -156,14 +177,14 @@ watch(workingList.value, (updatedList) => {
156177 }
157178})
158179
159- const onRowClicked = (item : Item ) => emit (" onRowClicked" , item )
180+ const onRowClicked = (item : Item < any > ) => emit (" onRowClicked" , item )
160181
161- const isRowSelected = (item : Item ) => {
182+ const isRowSelected = (item : Item < any > ) => {
162183 const { selected, ... data } = item
163184 return selectedRows .value ?.some ((row ) => isEqual (row , data ))
164185}
165186
166- const toggleRow = (item : Item ) => {
187+ const toggleRow = (item : Item < any > ) => {
167188 item .selected = ! item .selected
168189 const { selected, ... data } = item
169190
@@ -212,7 +233,16 @@ watchEffect(() => {
212233 }
213234})
214235
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+
216246export type Direction = " ascending" | " descending"
217247
218248const sortList = (key : string , direction : Direction ) => {
0 commit comments