Skip to content

Commit

Permalink
fix(Table): types in undeclared slots (#2544)
Browse files Browse the repository at this point in the history
Co-authored-by: Benjamin Canac <[email protected]>
  • Loading branch information
hywax and benjamincanac authored Nov 6, 2024
1 parent a6c1a6c commit f821e66
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
38 changes: 36 additions & 2 deletions docs/app/components/content/examples/table/TableSlotsExample.vue
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<script setup lang="ts">
import type { TableColumn } from '@nuxt/ui'
import type { TableColumn, DropdownMenuItem } from '@nuxt/ui'
type User = {
interface User {
id: number
name: string
position: string
email: string
role: string
}
const toast = useToast()
const data = ref<User[]>([{
id: 1,
name: 'Lindsay Walton',
Expand Down Expand Up @@ -59,7 +61,34 @@ const columns: TableColumn<User>[] = [{
}, {
accessorKey: 'role',
header: 'Role'
}, {
id: 'action'
}]
function getDropdownActions(user: User): DropdownMenuItem[][] {
return [
[{
label: 'Copy user Id',
icon: 'i-lucide-copy',
onSelect: () => {
navigator.clipboard.writeText(user.id.toString())
toast.add({
title: 'User ID copied to clipboard!',
color: 'success',
icon: 'i-lucide-circle-check'
})
}
}],
[{
label: 'Edit',
icon: 'i-lucide-edit'
}, {
label: 'Delete',
icon: 'i-lucide-trash',
color: 'error'
}]
]
}
</script>

<template>
Expand All @@ -77,5 +106,10 @@ const columns: TableColumn<User>[] = [{
</div>
</div>
</template>
<template #action-cell="{ row }">
<UDropdownMenu :items="getDropdownActions(row.original)">
<UButton icon="i-lucide-ellipsis-vertical" color="neutral" variant="ghost" />
</UDropdownMenu>
</template>
</UTable>
</template>
4 changes: 2 additions & 2 deletions src/runtime/components/Table.vue
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ export interface TableProps<T> {
ui?: Partial<typeof table.slots>
}

type DynamicHeaderSlots<T, K = keyof T> = Record<string, T> & Record<`${K extends string ? K : never}-header`, (props: HeaderContext<T, unknown>) => any>
type DynamicCellSlots<T, K = keyof T> = Record<string, T> & Record<`${K extends string ? K : never}-cell`, (props: CellContext<T, unknown>) => any>
type DynamicHeaderSlots<T, K = keyof T> = Record<string, (props: HeaderContext<T, unknown>) => any> & Record<`${K extends string ? K : never}-header`, (props: HeaderContext<T, unknown>) => any>
type DynamicCellSlots<T, K = keyof T> = Record<string, (props: CellContext<T, unknown>) => any> & Record<`${K extends string ? K : never}-cell`, (props: CellContext<T, unknown>) => any>

export type TableSlots<T> = {
expanded: (props: { row: Row<T> }) => any
Expand Down

0 comments on commit f821e66

Please sign in to comment.