diff --git a/docs/app/components/content/examples/table/TableContextMenuExample.vue b/docs/app/components/content/examples/table/TableRowContextMenuEventExample.vue similarity index 100% rename from docs/app/components/content/examples/table/TableContextMenuExample.vue rename to docs/app/components/content/examples/table/TableRowContextMenuEventExample.vue diff --git a/docs/app/components/content/examples/table/TableRowHoverEventExample.vue b/docs/app/components/content/examples/table/TableRowHoverEventExample.vue new file mode 100644 index 00000000..41e0d4ff --- /dev/null +++ b/docs/app/components/content/examples/table/TableRowHoverEventExample.vue @@ -0,0 +1,157 @@ + + + diff --git a/docs/app/components/content/examples/table/TableRowSelectionEventExample.vue b/docs/app/components/content/examples/table/TableRowSelectEventExample.vue similarity index 100% rename from docs/app/components/content/examples/table/TableRowSelectionEventExample.vue rename to docs/app/components/content/examples/table/TableRowSelectEventExample.vue diff --git a/docs/content/3.components/table.md b/docs/content/3.components/table.md index bcea678f..eb2a9cff 100644 --- a/docs/content/3.components/table.md +++ b/docs/content/3.components/table.md @@ -266,8 +266,8 @@ You can group rows based on a given column value and show/hide sub rows via some #### Important parts: -* Add prop `grouping` to `UTable` component with an array of column ids you want to group by. -* Add prop `grouping-options` to `UTable`. It must include `getGroupedRowModel`, you can import it from `@tanstack/vue-table` or implement your own. +* Add `grouping` prop with an array of column ids you want to group by. +* Add `grouping-options` prop. It must include `getGroupedRowModel`, you can import it from `@tanstack/vue-table` or implement your own. * Expand rows via `row.toggleExpanded()` method on any cell of the row. Keep in mind, it also toggles `#expanded` slot. * Use `aggregateFn` on column definition to define how to aggregate the rows. * `agregatedCell` renderer on column definition only works if there is no `cell` renderer. @@ -304,19 +304,19 @@ 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 +### With row 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. +You can add a `@select` listener to make rows clickable with or without a checkbox column. ::note -You can use this to navigate to a page, open a modal or even to select the row manually. +The handler function receives the `TableRow` instance as the first argument and an optional `Event` as the second argument. :: ::component-example --- prettier: true collapse: true -name: 'table-row-selection-event-example' +name: 'table-row-select-event-example' highlights: - 123 - 130 @@ -324,15 +324,23 @@ class: '!p-0' --- :: -### With context menu :badge{label="Soon" class="align-text-top"} +::tip +You can use this to navigate to a page, open a modal or even to select the row manually. +:: -You can wrap the `UTable` component in a [ContextMenu](/components/context-menu) component to make rows right clickable. You also need to add a `@contextmenu` listener to the `UTable` component to determine wich row is being right clicked. The handler function receives the `Event` and `TableRow` instance as the first and second arguments respectively. +### With row context menu event :badge{label="Soon" class="align-text-top"} + +You can add a `@contextmenu` listener to make rows right clickable and wrap the Table in a [ContextMenu](/components/context-menu) component to display row actions for example. + +::note +The handler function receives the `Event` and `TableRow` instance as the first and second arguments respectively. +:: ::component-example --- prettier: true collapse: true -name: 'table-context-menu-example' +name: 'table-row-context-menu-event-example' highlights: - 130 - 170 @@ -340,6 +348,30 @@ class: '!p-0' --- :: +### With row hover event :badge{label="Soon" class="align-text-top"} + +You can add a `@hover` listener to make rows hoverable and use a [Popover](/components/popover) or a [Tooltip](/components/tooltip) component to display row details for example. + +::note +The handler function receives the `Event` and `TableRow` instance as the first and second arguments respectively. +:: + +::component-example +--- +prettier: true +collapse: true +name: 'table-row-hover-event-example' +highlights: + - 126 + - 149 +class: '!p-0' +--- +:: + +::note +This example is similar as the Popover [with following cursor example](/components/popover#with-following-cursor) and uses a [`refDebounced`](https://vueuse.org/shared/refDebounced/#refdebounced) to prevent the Popover from opening and closing too quickly when moving the cursor from one row to another. +:: + ### 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/playground/app/pages/components/table.vue b/playground/app/pages/components/table.vue index 32ef0e90..03c71a08 100644 --- a/playground/app/pages/components/table.vue +++ b/playground/app/pages/components/table.vue @@ -3,7 +3,7 @@ import { h, resolveComponent } from 'vue' import { upperFirst } from 'scule' import type { TableColumn, TableRow } from '@nuxt/ui' import { getPaginationRowModel } from '@tanstack/vue-table' -import { useClipboard } from '@vueuse/core' +import { useClipboard, refDebounced } from '@vueuse/core' const UButton = resolveComponent('UButton') const UCheckbox = resolveComponent('UCheckbox') @@ -311,6 +311,30 @@ function onContextmenu(e: Event, row: TableRow) { contextmenuRow.value = row } +const popoverOpen = ref(false) +const popoverOpenDebounced = refDebounced(popoverOpen, 1) +const popoverAnchor = ref({ x: 0, y: 0 }) +const popoverRow = ref | null>(null) + +const reference = computed(() => ({ + getBoundingClientRect: () => + ({ + width: 0, + height: 0, + left: popoverAnchor.value.x, + right: popoverAnchor.value.x, + top: popoverAnchor.value.y, + bottom: popoverAnchor.value.y, + ...popoverAnchor.value + } as DOMRect) +})) + +function onHover(_e: Event, row: TableRow | null) { + popoverRow.value = row + + popoverOpen.value = !!row +} + onMounted(() => { setTimeout(() => { loading.value = false @@ -374,6 +398,11 @@ onMounted(() => { class="border border-accented rounded-sm" @select="onSelect" @contextmenu="onContextmenu" + @pointermove="(ev: PointerEvent) => { + popoverAnchor.x = ev.clientX + popoverAnchor.y = ev.clientY + }" + @hover="onHover" >