From 0668a399dced48d1976de3820118bf2a29fe116e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=B3pez=20C?= Date: Fri, 28 Feb 2025 06:05:48 -0500 Subject: [PATCH] feat(Table): add `select` event (#2822) Co-authored-by: Benjamin Canac --- .../table/TableRowSelectionEventExample.vue | 131 ++++++++ docs/content/3.components/table.md | 22 +- src/runtime/components/Table.vue | 29 +- src/theme/table.ts | 2 +- .../__snapshots__/Table-vue.spec.ts.snap | 290 +++++++++--------- .../__snapshots__/Table.spec.ts.snap | 290 +++++++++--------- 6 files changed, 470 insertions(+), 294 deletions(-) create mode 100644 docs/app/components/content/examples/table/TableRowSelectionEventExample.vue 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 @@ + + + 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({