Skip to content

Commit 1db21d1

Browse files
feat(Table): add style to table and column meta (#4513)
Co-authored-by: Benjamin Canac <canacb1@gmail.com>
1 parent 6f2ce5c commit 1db21d1

File tree

5 files changed

+565
-4
lines changed

5 files changed

+565
-4
lines changed

‎docs/content/3.components/table.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ Use the `columns` prop as an array of [ColumnDef](https://tanstack.com/table/lat
8383
- `class`:
8484
- `td`: [The classes to apply to the `td` element.]{class="text-muted"}
8585
- `th`: [The classes to apply to the `th` element.]{class="text-muted"}
86+
- `style`:
87+
- `td`: [The style to apply to the `td` element.]{class="text-muted"}
88+
- `th`: [The style to apply to the `th` element.]{class="text-muted"}
8689

8790
In order to render components or other HTML elements, you will need to use the Vue [`h` function](https://vuejs.org/api/render-function.html#h) inside the `header` and `cell` props. This is different from other components that use slots but allows for more flexibility.
8891

@@ -112,6 +115,8 @@ Use the `meta` prop as an object ([TableMeta](https://tanstack.com/table/latest/
112115

113116
- `class`:
114117
- `tr`: [The classes to apply to the `tr` element.]{class="text-muted"}
118+
- `style`:
119+
- `tr`: [The style to apply to the `tr` element.]{class="text-muted"}
115120

116121
### Loading
117122

‎src/runtime/components/Table.vue

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,19 @@ declare module '@tanstack/table-core' {
4545
th?: string | ((cell: Header<TData, TValue>) => string)
4646
td?: string | ((cell: Cell<TData, TValue>) => string)
4747
}
48+
style?: {
49+
th?: string | Record<string, string> | ((cell: Header<TData, TValue>) => string | Record<string, string>)
50+
td?: string | Record<string, string> | ((cell: Cell<TData, TValue>) => string | Record<string, string>)
51+
}
4852
}
4953
5054
interface TableMeta<TData> {
5155
class?: {
5256
tr?: string | ((row: Row<TData>) => string)
5357
}
58+
style?: {
59+
tr?: string | Record<string, string> | ((row: Row<TData>) => string | Record<string, string>)
60+
}
5461
}
5562
5663
}
@@ -432,6 +439,7 @@ defineExpose({
432439
typeof tableApi.options.meta?.class?.tr === 'function' ? tableApi.options.meta.class.tr(row) : tableApi.options.meta?.class?.tr
433440
]
434441
})"
442+
:style="typeof tableApi.options.meta?.style?.tr === 'function' ? tableApi.options.meta?.style?.tr(row) : tableApi.options.meta?.style?.tr"
435443
@click="onRowSelect($event, row)"
436444
@pointerenter="onRowHover($event, row)"
437445
@pointerleave="onRowHover($event, null)"
@@ -448,6 +456,7 @@ defineExpose({
448456
],
449457
pinned: !!cell.column.getIsPinned()
450458
})"
459+
:style="typeof cell.column.columnDef.meta?.style?.td === 'function' ? cell.column.columnDef.meta.style.td(cell) : cell.column.columnDef.meta?.style?.td"
451460
>
452461
<slot :name="`${cell.column.id}-cell`" v-bind="cell.getContext()">
453462
<FlexRender :render="cell.column.columnDef.cell" :props="cell.getContext()" />
@@ -495,6 +504,7 @@ defineExpose({
495504
],
496505
pinned: !!header.column.getIsPinned()
497506
})"
507+
:style="typeof header.column.columnDef.meta?.style?.th === 'function' ? header.column.columnDef.meta.style.th(header) : header.column.columnDef.meta?.style?.th"
498508
>
499509
<slot :name="`${header.id}-footer`" v-bind="header.getContext()">
500510
<FlexRender v-if="!header.isPlaceholder" :render="header.column.columnDef.footer" :props="header.getContext()" />

‎test/components/Table.spec.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ describe('Table', () => {
165165
['with loading', { props: { ...props, loading: true } }],
166166
...loadingColors.map((loadingColor: string) => [`with loading color ${loadingColor}`, { props: { ...props, loading: true, loadingColor } }]),
167167
...loadingAnimations.map((loadingAnimation: string) => [`with loading animation ${loadingAnimation}`, { props: { ...props, loading: true, loadingAnimation } }]),
168+
['with meta prop', { props: { ...props, meta: { class: { tr: 'custom-row-class' }, style: { tr: { backgroundColor: 'lightgray' } } } } }],
169+
['with meta field on columns', { props: { ...props, columns: columns.map(c => ({ ...c, meta: { class: { th: 'custom-heading-class', td: 'custom-cell-class' }, style: { th: { backgroundColor: 'black' }, td: { backgroundColor: 'lightgray' } } } })) } }],
168170
['with as', { props: { ...props, as: 'section' } }],
169171
['with class', { props: { ...props, class: 'absolute' } }],
170172
['with ui', { props: { ...props, ui: { base: 'table-auto' } } }],
@@ -194,10 +196,10 @@ describe('Table', () => {
194196
},
195197
...(filter.value === 2
196198
? [
197-
{
198-
accessorKey: 'amount',
199-
header: () => h('div', { ['data-test-th']: 'amount' }, 'Amount')
200-
} satisfies TableColumn<typeof data[number]>
199+
{
200+
accessorKey: 'amount',
201+
header: () => h('div', { ['data-test-th']: 'amount' }, 'Amount')
202+
} satisfies TableColumn<typeof data[number]>
201203
]
202204
: [])
203205
])

0 commit comments

Comments
 (0)