Skip to content

Commit

Permalink
feat(Table): handle meta.class on th and td (#2790)
Browse files Browse the repository at this point in the history
Co-authored-by: Benjamin Canac <[email protected]>
  • Loading branch information
clopezpro and benjamincanac authored Dec 2, 2024
1 parent f712135 commit 004a577
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
9 changes: 9 additions & 0 deletions playground/app/pages/components/table.vue
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ const columns: TableColumn<Payment>[] = [{
}, {
accessorKey: 'date',
header: 'Date',
meta: {
class: {
td: 'text-center font-semibold',
th: 'text-right text-green-500 w-48'
}
},
cell: ({ row }) => {
return new Date(row.getValue('date')).toLocaleString('en-US', {
day: 'numeric',
Expand Down Expand Up @@ -318,6 +324,9 @@ onMounted(() => {
:column-pinning="columnPinning"
:loading="loading"
sticky
:ui="{
tr: 'divide-x divide-[var(--ui-border)]'
}"
class="border border-[var(--ui-border-accented)] rounded-[var(--ui-radius)] flex-1"
>
<template #expanded="{ row }">
Expand Down
15 changes: 13 additions & 2 deletions src/runtime/components/Table.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import type { Ref } from 'vue'
import { tv, type VariantProps } from 'tailwind-variants'
import type { AppConfig } from '@nuxt/schema'
import type { RowData } from '@tanstack/table-core'
import type {
Row,
ColumnDef,
Expand All @@ -26,6 +27,16 @@ import type {
import _appConfig from '#build/app.config'
import theme from '#build/ui/table'

declare module '@tanstack/table-core' {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface ColumnMeta<TData extends RowData, TValue> {
class?: {
th?: string
td?: string
}
}
}

const appConfig = _appConfig as AppConfig & { ui: { table: Partial<typeof theme> } }

const table = tv({ extend: tv(theme), ...(appConfig.ui?.table || {}) })
Expand Down Expand Up @@ -206,7 +217,7 @@ defineExpose({
v-for="header in headerGroup.headers"
:key="header.id"
:data-pinned="header.column.getIsPinned()"
:class="ui.th({ class: [props.ui?.th], pinned: !!header.column.getIsPinned() })"
:class="ui.th({ class: [props.ui?.th, header.column.columnDef.meta?.class?.th], pinned: !!header.column.getIsPinned() })"
>
<slot :name="`${header.id}-header`" v-bind="header.getContext()">
<FlexRender v-if="!header.isPlaceholder" :render="header.column.columnDef.header" :props="header.getContext()" />
Expand All @@ -223,7 +234,7 @@ defineExpose({
v-for="cell in row.getVisibleCells()"
:key="cell.id"
:data-pinned="cell.column.getIsPinned()"
:class="ui.td({ class: [props.ui?.td], pinned: !!cell.column.getIsPinned() })"
:class="ui.td({ class: [props.ui?.td, cell.column.columnDef.meta?.class?.td], pinned: !!cell.column.getIsPinned() })"
>
<slot :name="`${cell.column.id}-cell`" v-bind="cell.getContext()">
<FlexRender :render="cell.column.columnDef.cell" :props="cell.getContext()" />
Expand Down

0 comments on commit 004a577

Please sign in to comment.