diff --git a/docs/app/components/content/examples/table/TableRowSelectionEventExample.vue b/docs/app/components/content/examples/table/TableRowSelectionEventExample.vue new file mode 100644 index 00000000..b7238fdc --- /dev/null +++ b/docs/app/components/content/examples/table/TableRowSelectionEventExample.vue @@ -0,0 +1,131 @@ + + + + + + + + + {{ table?.tableApi?.getFilteredSelectedRowModel().rows.length || 0 }} of + {{ table?.tableApi?.getFilteredRowModel().rows.length || 0 }} row(s) selected. + + + + diff --git a/docs/content/3.components/table.md b/docs/content/3.components/table.md index 9bb1c83e..4492a92d 100644 --- a/docs/content/3.components/table.md +++ b/docs/content/3.components/table.md @@ -264,7 +264,7 @@ collapse: true name: 'table-row-selection-example' highlights: - 55 - - 70 + - 72 class: '!p-0' --- :: @@ -273,6 +273,26 @@ class: '!p-0' You can use the `row-selection` prop to control the selection state of the rows (can be binded with `v-model`). :: +### With `@select` event + +You can add a `@select` listener to make rows clickable. The handler function receives the `TableRow` instance as the first argument and an optional `Event` as the second argument. + +::note +You can use this to navigate to a page, open a modal or even to select the row manually. +:: + +::component-example +--- +prettier: true +collapse: true +name: 'table-row-selection-event-example' +highlights: + - 123 + - 130 +class: '!p-0' +--- +:: + ### With column sorting You can update a column `header` to render a [Button](/components/button) component inside the `header` to toggle the sorting state using the TanStack Table [Sorting APIs](https://tanstack.com/table/latest/docs/api/features/sorting). diff --git a/src/runtime/components/Table.vue b/src/runtime/components/Table.vue index e99ecc8b..fef9d03b 100644 --- a/src/runtime/components/Table.vue +++ b/src/runtime/components/Table.vue @@ -56,8 +56,8 @@ const table = tv({ extend: tv(theme), ...(appConfigTable.ui?.table || {}) }) type TableVariants = VariantProps +export type TableRow = Row export type TableData = RowData - export type TableColumn = ColumnDef export interface TableOptions extends Omit, 'data' | 'columns' | 'getCoreRowModel' | 'state' | 'onStateChange' | 'renderFallbackValue'> { @@ -144,6 +144,7 @@ export interface TableProps extends TableOptions { * @link [Guide](https://tanstack.com/table/v8/docs/guide/column-faceting) */ facetedOptions?: FacetedOptions + onSelect?: (row: TableRow, e?: Event) => void class?: any ui?: Partial } @@ -276,6 +277,22 @@ function valueUpdater>(updaterOrValue: T, ref: Ref) { ref.value = typeof updaterOrValue === 'function' ? updaterOrValue(ref.value) : updaterOrValue } +function handleRowSelect(row: TableRow, e: Event) { + if (!props.onSelect) { + return + } + const target = e.target as HTMLElement + const isInteractive = target.closest('button') + if (isInteractive) { + return + } + + e.preventDefault() + e.stopPropagation() + + props.onSelect(row, e) +} + defineExpose({ tableApi }) @@ -308,7 +325,15 @@ defineExpose({ - + ) => ({ base: 'min-w-full overflow-clip', caption: 'sr-only', thead: 'relative [&>tr]:after:absolute [&>tr]:after:inset-x-0 [&>tr]:after:bottom-0 [&>tr]:after:h-px [&>tr]:after:bg-(--ui-border-accented)', - tbody: 'divide-y divide-(--ui-border)', + tbody: 'divide-y divide-(--ui-border) [&>tr]:data-[selectable=true]:hover:bg-(--ui-bg-elevated)/50 [&>tr]:data-[selectable=true]:focus-visible:outline-(--ui-primary)', tr: 'data-[selected=true]:bg-(--ui-bg-elevated)/50', th: 'px-4 py-3.5 text-sm text-(--ui-text-highlighted) text-left rtl:text-right font-semibold [&:has([role=checkbox])]:pe-0', td: 'p-4 text-sm text-(--ui-text-muted) whitespace-nowrap [&:has([role=checkbox])]:pe-0', diff --git a/test/components/__snapshots__/Table-vue.spec.ts.snap b/test/components/__snapshots__/Table-vue.spec.ts.snap index f4c6661d..64d4d83b 100644 --- a/test/components/__snapshots__/Table-vue.spec.ts.snap +++ b/test/components/__snapshots__/Table-vue.spec.ts.snap @@ -12,36 +12,36 @@ exports[`Table > renders with as correctly 1`] = ` Email - - + + m5gr84i9 316 success ken99@yahoo.com - + 3u1reuv4 242 success Abe45@gmail.com - + derv1ws0 837 processing Monserrat44@gmail.com - + 5kma53ae 874 success Silas22@gmail.com - + bhqecj4p 721 failed @@ -65,36 +65,36 @@ exports[`Table > renders with caption correctly 1`] = ` Email - - + + m5gr84i9 316 success ken99@yahoo.com - + 3u1reuv4 242 success Abe45@gmail.com - + derv1ws0 837 processing Monserrat44@gmail.com - + 5kma53ae 874 success Silas22@gmail.com - + bhqecj4p 721 failed @@ -118,36 +118,36 @@ exports[`Table > renders with caption slot correctly 1`] = ` Email - - + + m5gr84i9 316 success ken99@yahoo.com - + 3u1reuv4 242 success Abe45@gmail.com - + derv1ws0 837 processing Monserrat44@gmail.com - + 5kma53ae 874 success Silas22@gmail.com - + bhqecj4p 721 failed @@ -171,36 +171,36 @@ exports[`Table > renders with cell slot correctly 1`] = ` Email - - + + ID Cell slot 316 success ken99@yahoo.com - + ID Cell slot 242 success Abe45@gmail.com - + ID Cell slot 837 processing Monserrat44@gmail.com - + ID Cell slot 874 success Silas22@gmail.com - + ID Cell slot 721 failed @@ -224,36 +224,36 @@ exports[`Table > renders with class correctly 1`] = ` Email - - + + m5gr84i9 316 success ken99@yahoo.com - + 3u1reuv4 242 success Abe45@gmail.com - + derv1ws0 837 processing Monserrat44@gmail.com - + 5kma53ae 874 success Silas22@gmail.com - + bhqecj4p 721 failed @@ -294,8 +294,8 @@ exports[`Table > renders with columns correctly 1`] = ` - - + + @@ -326,7 +326,7 @@ exports[`Table > renders with columns correctly 1`] = ` - + @@ -357,7 +357,7 @@ exports[`Table > renders with columns correctly 1`] = ` - + @@ -388,7 +388,7 @@ exports[`Table > renders with columns correctly 1`] = ` - + @@ -419,7 +419,7 @@ exports[`Table > renders with columns correctly 1`] = ` - + @@ -467,36 +467,36 @@ exports[`Table > renders with data correctly 1`] = ` Email - - + + m5gr84i9 316 success ken99@yahoo.com - + 3u1reuv4 242 success Abe45@gmail.com - + derv1ws0 837 processing Monserrat44@gmail.com - + 5kma53ae 874 success Silas22@gmail.com - + bhqecj4p 721 failed @@ -520,36 +520,36 @@ exports[`Table > renders with empty slot correctly 1`] = ` Email - - + + m5gr84i9 316 success ken99@yahoo.com - + 3u1reuv4 242 success Abe45@gmail.com - + derv1ws0 837 processing Monserrat44@gmail.com - + 5kma53ae 874 success Silas22@gmail.com - + bhqecj4p 721 failed @@ -573,36 +573,36 @@ exports[`Table > renders with expanded slot correctly 1`] = ` Email - - + + m5gr84i9 316 success ken99@yahoo.com - + 3u1reuv4 242 success Abe45@gmail.com - + derv1ws0 837 processing Monserrat44@gmail.com - + 5kma53ae 874 success Silas22@gmail.com - + bhqecj4p 721 failed @@ -626,36 +626,36 @@ exports[`Table > renders with header slot correctly 1`] = ` Email - - + + m5gr84i9 316 success ken99@yahoo.com - + 3u1reuv4 242 success Abe45@gmail.com - + derv1ws0 837 processing Monserrat44@gmail.com - + 5kma53ae 874 success Silas22@gmail.com - + bhqecj4p 721 failed @@ -679,36 +679,36 @@ exports[`Table > renders with loading animation carousel correctly 1`] = ` Email - - + + m5gr84i9 316 success ken99@yahoo.com - + 3u1reuv4 242 success Abe45@gmail.com - + derv1ws0 837 processing Monserrat44@gmail.com - + 5kma53ae 874 success Silas22@gmail.com - + bhqecj4p 721 failed @@ -732,36 +732,36 @@ exports[`Table > renders with loading animation carousel-inverse correctly 1`] = Email - - + + m5gr84i9 316 success ken99@yahoo.com - + 3u1reuv4 242 success Abe45@gmail.com - + derv1ws0 837 processing Monserrat44@gmail.com - + 5kma53ae 874 success Silas22@gmail.com - + bhqecj4p 721 failed @@ -785,36 +785,36 @@ exports[`Table > renders with loading animation elastic correctly 1`] = ` Email - - + + m5gr84i9 316 success ken99@yahoo.com - + 3u1reuv4 242 success Abe45@gmail.com - + derv1ws0 837 processing Monserrat44@gmail.com - + 5kma53ae 874 success Silas22@gmail.com - + bhqecj4p 721 failed @@ -838,36 +838,36 @@ exports[`Table > renders with loading animation swing correctly 1`] = ` Email - - + + m5gr84i9 316 success ken99@yahoo.com - + 3u1reuv4 242 success Abe45@gmail.com - + derv1ws0 837 processing Monserrat44@gmail.com - + 5kma53ae 874 success Silas22@gmail.com - + bhqecj4p 721 failed @@ -891,36 +891,36 @@ exports[`Table > renders with loading color error correctly 1`] = ` Email - - + + m5gr84i9 316 success ken99@yahoo.com - + 3u1reuv4 242 success Abe45@gmail.com - + derv1ws0 837 processing Monserrat44@gmail.com - + 5kma53ae 874 success Silas22@gmail.com - + bhqecj4p 721 failed @@ -944,36 +944,36 @@ exports[`Table > renders with loading color info correctly 1`] = ` Email - - + + m5gr84i9 316 success ken99@yahoo.com - + 3u1reuv4 242 success Abe45@gmail.com - + derv1ws0 837 processing Monserrat44@gmail.com - + 5kma53ae 874 success Silas22@gmail.com - + bhqecj4p 721 failed @@ -997,36 +997,36 @@ exports[`Table > renders with loading color neutral correctly 1`] = ` Email - - + + m5gr84i9 316 success ken99@yahoo.com - + 3u1reuv4 242 success Abe45@gmail.com - + derv1ws0 837 processing Monserrat44@gmail.com - + 5kma53ae 874 success Silas22@gmail.com - + bhqecj4p 721 failed @@ -1050,36 +1050,36 @@ exports[`Table > renders with loading color primary correctly 1`] = ` Email - - + + m5gr84i9 316 success ken99@yahoo.com - + 3u1reuv4 242 success Abe45@gmail.com - + derv1ws0 837 processing Monserrat44@gmail.com - + 5kma53ae 874 success Silas22@gmail.com - + bhqecj4p 721 failed @@ -1103,36 +1103,36 @@ exports[`Table > renders with loading color secondary correctly 1`] = ` Email - - + + m5gr84i9 316 success ken99@yahoo.com - + 3u1reuv4 242 success Abe45@gmail.com - + derv1ws0 837 processing Monserrat44@gmail.com - + 5kma53ae 874 success Silas22@gmail.com - + bhqecj4p 721 failed @@ -1156,36 +1156,36 @@ exports[`Table > renders with loading color success correctly 1`] = ` Email - - + + m5gr84i9 316 success ken99@yahoo.com - + 3u1reuv4 242 success Abe45@gmail.com - + derv1ws0 837 processing Monserrat44@gmail.com - + 5kma53ae 874 success Silas22@gmail.com - + bhqecj4p 721 failed @@ -1209,36 +1209,36 @@ exports[`Table > renders with loading color warning correctly 1`] = ` Email - - + + m5gr84i9 316 success ken99@yahoo.com - + 3u1reuv4 242 success Abe45@gmail.com - + derv1ws0 837 processing Monserrat44@gmail.com - + 5kma53ae 874 success Silas22@gmail.com - + bhqecj4p 721 failed @@ -1262,36 +1262,36 @@ exports[`Table > renders with loading correctly 1`] = ` Email - - + + m5gr84i9 316 success ken99@yahoo.com - + 3u1reuv4 242 success Abe45@gmail.com - + derv1ws0 837 processing Monserrat44@gmail.com - + 5kma53ae 874 success Silas22@gmail.com - + bhqecj4p 721 failed @@ -1315,36 +1315,36 @@ exports[`Table > renders with sticky correctly 1`] = ` Email - - + + m5gr84i9 316 success ken99@yahoo.com - + 3u1reuv4 242 success Abe45@gmail.com - + derv1ws0 837 processing Monserrat44@gmail.com - + 5kma53ae 874 success Silas22@gmail.com - + bhqecj4p 721 failed @@ -1368,36 +1368,36 @@ exports[`Table > renders with ui correctly 1`] = ` Email - - + + m5gr84i9 316 success ken99@yahoo.com - + 3u1reuv4 242 success Abe45@gmail.com - + derv1ws0 837 processing Monserrat44@gmail.com - + 5kma53ae 874 success Silas22@gmail.com - + bhqecj4p 721 failed @@ -1416,7 +1416,7 @@ exports[`Table > renders without data correctly 1`] = ` - + No data diff --git a/test/components/__snapshots__/Table.spec.ts.snap b/test/components/__snapshots__/Table.spec.ts.snap index ef46f549..2ee106ff 100644 --- a/test/components/__snapshots__/Table.spec.ts.snap +++ b/test/components/__snapshots__/Table.spec.ts.snap @@ -12,36 +12,36 @@ exports[`Table > renders with as correctly 1`] = ` Email - - + + m5gr84i9 316 success ken99@yahoo.com - + 3u1reuv4 242 success Abe45@gmail.com - + derv1ws0 837 processing Monserrat44@gmail.com - + 5kma53ae 874 success Silas22@gmail.com - + bhqecj4p 721 failed @@ -65,36 +65,36 @@ exports[`Table > renders with caption correctly 1`] = ` Email - - + + m5gr84i9 316 success ken99@yahoo.com - + 3u1reuv4 242 success Abe45@gmail.com - + derv1ws0 837 processing Monserrat44@gmail.com - + 5kma53ae 874 success Silas22@gmail.com - + bhqecj4p 721 failed @@ -118,36 +118,36 @@ exports[`Table > renders with caption slot correctly 1`] = ` Email - - + + m5gr84i9 316 success ken99@yahoo.com - + 3u1reuv4 242 success Abe45@gmail.com - + derv1ws0 837 processing Monserrat44@gmail.com - + 5kma53ae 874 success Silas22@gmail.com - + bhqecj4p 721 failed @@ -171,36 +171,36 @@ exports[`Table > renders with cell slot correctly 1`] = ` Email - - + + ID Cell slot 316 success ken99@yahoo.com - + ID Cell slot 242 success Abe45@gmail.com - + ID Cell slot 837 processing Monserrat44@gmail.com - + ID Cell slot 874 success Silas22@gmail.com - + ID Cell slot 721 failed @@ -224,36 +224,36 @@ exports[`Table > renders with class correctly 1`] = ` Email - - + + m5gr84i9 316 success ken99@yahoo.com - + 3u1reuv4 242 success Abe45@gmail.com - + derv1ws0 837 processing Monserrat44@gmail.com - + 5kma53ae 874 success Silas22@gmail.com - + bhqecj4p 721 failed @@ -294,8 +294,8 @@ exports[`Table > renders with columns correctly 1`] = ` - - + + @@ -326,7 +326,7 @@ exports[`Table > renders with columns correctly 1`] = ` - + @@ -357,7 +357,7 @@ exports[`Table > renders with columns correctly 1`] = ` - + @@ -388,7 +388,7 @@ exports[`Table > renders with columns correctly 1`] = ` - + @@ -419,7 +419,7 @@ exports[`Table > renders with columns correctly 1`] = ` - + @@ -467,36 +467,36 @@ exports[`Table > renders with data correctly 1`] = ` Email - - + + m5gr84i9 316 success ken99@yahoo.com - + 3u1reuv4 242 success Abe45@gmail.com - + derv1ws0 837 processing Monserrat44@gmail.com - + 5kma53ae 874 success Silas22@gmail.com - + bhqecj4p 721 failed @@ -520,36 +520,36 @@ exports[`Table > renders with empty slot correctly 1`] = ` Email - - + + m5gr84i9 316 success ken99@yahoo.com - + 3u1reuv4 242 success Abe45@gmail.com - + derv1ws0 837 processing Monserrat44@gmail.com - + 5kma53ae 874 success Silas22@gmail.com - + bhqecj4p 721 failed @@ -573,36 +573,36 @@ exports[`Table > renders with expanded slot correctly 1`] = ` Email - - + + m5gr84i9 316 success ken99@yahoo.com - + 3u1reuv4 242 success Abe45@gmail.com - + derv1ws0 837 processing Monserrat44@gmail.com - + 5kma53ae 874 success Silas22@gmail.com - + bhqecj4p 721 failed @@ -626,36 +626,36 @@ exports[`Table > renders with header slot correctly 1`] = ` Email - - + + m5gr84i9 316 success ken99@yahoo.com - + 3u1reuv4 242 success Abe45@gmail.com - + derv1ws0 837 processing Monserrat44@gmail.com - + 5kma53ae 874 success Silas22@gmail.com - + bhqecj4p 721 failed @@ -679,36 +679,36 @@ exports[`Table > renders with loading animation carousel correctly 1`] = ` Email - - + + m5gr84i9 316 success ken99@yahoo.com - + 3u1reuv4 242 success Abe45@gmail.com - + derv1ws0 837 processing Monserrat44@gmail.com - + 5kma53ae 874 success Silas22@gmail.com - + bhqecj4p 721 failed @@ -732,36 +732,36 @@ exports[`Table > renders with loading animation carousel-inverse correctly 1`] = Email - - + + m5gr84i9 316 success ken99@yahoo.com - + 3u1reuv4 242 success Abe45@gmail.com - + derv1ws0 837 processing Monserrat44@gmail.com - + 5kma53ae 874 success Silas22@gmail.com - + bhqecj4p 721 failed @@ -785,36 +785,36 @@ exports[`Table > renders with loading animation elastic correctly 1`] = ` Email - - + + m5gr84i9 316 success ken99@yahoo.com - + 3u1reuv4 242 success Abe45@gmail.com - + derv1ws0 837 processing Monserrat44@gmail.com - + 5kma53ae 874 success Silas22@gmail.com - + bhqecj4p 721 failed @@ -838,36 +838,36 @@ exports[`Table > renders with loading animation swing correctly 1`] = ` Email - - + + m5gr84i9 316 success ken99@yahoo.com - + 3u1reuv4 242 success Abe45@gmail.com - + derv1ws0 837 processing Monserrat44@gmail.com - + 5kma53ae 874 success Silas22@gmail.com - + bhqecj4p 721 failed @@ -891,36 +891,36 @@ exports[`Table > renders with loading color error correctly 1`] = ` Email - - + + m5gr84i9 316 success ken99@yahoo.com - + 3u1reuv4 242 success Abe45@gmail.com - + derv1ws0 837 processing Monserrat44@gmail.com - + 5kma53ae 874 success Silas22@gmail.com - + bhqecj4p 721 failed @@ -944,36 +944,36 @@ exports[`Table > renders with loading color info correctly 1`] = ` Email - - + + m5gr84i9 316 success ken99@yahoo.com - + 3u1reuv4 242 success Abe45@gmail.com - + derv1ws0 837 processing Monserrat44@gmail.com - + 5kma53ae 874 success Silas22@gmail.com - + bhqecj4p 721 failed @@ -997,36 +997,36 @@ exports[`Table > renders with loading color neutral correctly 1`] = ` Email - - + + m5gr84i9 316 success ken99@yahoo.com - + 3u1reuv4 242 success Abe45@gmail.com - + derv1ws0 837 processing Monserrat44@gmail.com - + 5kma53ae 874 success Silas22@gmail.com - + bhqecj4p 721 failed @@ -1050,36 +1050,36 @@ exports[`Table > renders with loading color primary correctly 1`] = ` Email - - + + m5gr84i9 316 success ken99@yahoo.com - + 3u1reuv4 242 success Abe45@gmail.com - + derv1ws0 837 processing Monserrat44@gmail.com - + 5kma53ae 874 success Silas22@gmail.com - + bhqecj4p 721 failed @@ -1103,36 +1103,36 @@ exports[`Table > renders with loading color secondary correctly 1`] = ` Email - - + + m5gr84i9 316 success ken99@yahoo.com - + 3u1reuv4 242 success Abe45@gmail.com - + derv1ws0 837 processing Monserrat44@gmail.com - + 5kma53ae 874 success Silas22@gmail.com - + bhqecj4p 721 failed @@ -1156,36 +1156,36 @@ exports[`Table > renders with loading color success correctly 1`] = ` Email - - + + m5gr84i9 316 success ken99@yahoo.com - + 3u1reuv4 242 success Abe45@gmail.com - + derv1ws0 837 processing Monserrat44@gmail.com - + 5kma53ae 874 success Silas22@gmail.com - + bhqecj4p 721 failed @@ -1209,36 +1209,36 @@ exports[`Table > renders with loading color warning correctly 1`] = ` Email - - + + m5gr84i9 316 success ken99@yahoo.com - + 3u1reuv4 242 success Abe45@gmail.com - + derv1ws0 837 processing Monserrat44@gmail.com - + 5kma53ae 874 success Silas22@gmail.com - + bhqecj4p 721 failed @@ -1262,36 +1262,36 @@ exports[`Table > renders with loading correctly 1`] = ` Email - - + + m5gr84i9 316 success ken99@yahoo.com - + 3u1reuv4 242 success Abe45@gmail.com - + derv1ws0 837 processing Monserrat44@gmail.com - + 5kma53ae 874 success Silas22@gmail.com - + bhqecj4p 721 failed @@ -1315,36 +1315,36 @@ exports[`Table > renders with sticky correctly 1`] = ` Email - - + + m5gr84i9 316 success ken99@yahoo.com - + 3u1reuv4 242 success Abe45@gmail.com - + derv1ws0 837 processing Monserrat44@gmail.com - + 5kma53ae 874 success Silas22@gmail.com - + bhqecj4p 721 failed @@ -1368,36 +1368,36 @@ exports[`Table > renders with ui correctly 1`] = ` Email - - + + m5gr84i9 316 success ken99@yahoo.com - + 3u1reuv4 242 success Abe45@gmail.com - + derv1ws0 837 processing Monserrat44@gmail.com - + 5kma53ae 874 success Silas22@gmail.com - + bhqecj4p 721 failed @@ -1416,7 +1416,7 @@ exports[`Table > renders without data correctly 1`] = ` - + No data