-
Notifications
You must be signed in to change notification settings - Fork 568
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(Table): add select
event
#2822
Open
clopezpro
wants to merge
6
commits into
nuxt:v3
Choose a base branch
from
clopezpro:select-event-table
base: v3
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+310
β142
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0163a07
add props onSelect, function handleRowSelect and onClick event
clopezpro 7c90f88
add example component with event on select
clopezpro 1b61627
use example and add document how use @select event
clopezpro 5ddccbc
export type TableRow, create new props onSelect , function handleRowSβ¦
clopezpro 2c052eb
add can-select hover
clopezpro f6d05b0
update table snap
clopezpro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
141 changes: 141 additions & 0 deletions
141
docs/app/components/content/examples/table/TableRowSelectionEventExample.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
<script setup lang="ts"> | ||
import { h, resolveComponent } from 'vue' | ||
import type { TableColumn, TableRow } from '@nuxt/ui' | ||
|
||
const UBadge = resolveComponent('UBadge') | ||
|
||
type Payment = { | ||
id: string | ||
date: string | ||
status: 'paid' | 'failed' | 'refunded' | ||
email: string | ||
amount: number | ||
} | ||
|
||
const data = ref<Payment[]>([{ | ||
id: '4600', | ||
date: '2024-03-11T15:30:00', | ||
status: 'paid', | ||
email: '[email protected]', | ||
amount: 594 | ||
}, { | ||
id: '4599', | ||
date: '2024-03-11T10:10:00', | ||
status: 'failed', | ||
email: '[email protected]', | ||
amount: 276 | ||
}, { | ||
id: '4598', | ||
date: '2024-03-11T08:50:00', | ||
status: 'refunded', | ||
email: '[email protected]', | ||
amount: 315 | ||
}, { | ||
id: '4597', | ||
date: '2024-03-10T19:45:00', | ||
status: 'paid', | ||
email: '[email protected]', | ||
amount: 529 | ||
}, { | ||
id: '4596', | ||
date: '2024-03-10T15:55:00', | ||
status: 'paid', | ||
email: '[email protected]', | ||
amount: 639 | ||
}]) | ||
|
||
const columns: TableColumn<Payment>[] = [{ | ||
accessorKey: 'date', | ||
header: 'Date', | ||
cell: ({ row }) => { | ||
return new Date(row.getValue('date')).toLocaleString('en-US', { | ||
day: 'numeric', | ||
month: 'short', | ||
hour: '2-digit', | ||
minute: '2-digit', | ||
hour12: false | ||
}) | ||
} | ||
}, { | ||
accessorKey: 'status', | ||
header: 'Status', | ||
cell: ({ row }) => { | ||
const color = ({ | ||
paid: 'success' as const, | ||
failed: 'error' as const, | ||
refunded: 'neutral' as const | ||
})[row.getValue('status') as string] | ||
|
||
return h(UBadge, { class: 'capitalize', variant: 'subtle', color }, () => row.getValue('status')) | ||
} | ||
}, { | ||
accessorKey: 'email', | ||
header: 'Email' | ||
}, { | ||
accessorKey: 'amount', | ||
header: () => h('div', { class: 'text-right' }, 'Amount'), | ||
cell: ({ row }) => { | ||
const amount = Number.parseFloat(row.getValue('amount')) | ||
|
||
const formatted = new Intl.NumberFormat('en-US', { | ||
style: 'currency', | ||
currency: 'EUR' | ||
}).format(amount) | ||
|
||
return h('div', { class: 'text-right font-medium' }, formatted) | ||
} | ||
}] | ||
|
||
const table = useTemplateRef('table') | ||
|
||
const rowSelection = ref<Record<string, boolean>>({ }) | ||
function onSelect(row: TableRow<Payment>, e?: Event) { | ||
/* If you decide to also select the column you can do this */ | ||
row.toggleSelected(!row.getIsSelected()) | ||
console.info(e) | ||
} | ||
const selectedRows = computed(() => { | ||
if (!rowSelection.value) return [] | ||
const indexes = Object.entries(rowSelection.value).filter(rs => rs[1])?.map(rs => Number.parseInt(rs[0])) || [] | ||
if (indexes.length === 0) return [] | ||
return data.value.filter((_, index) => indexes.includes(index)) | ||
}) | ||
function quitSelect(row: TableRow<Payment>) { | ||
const index = data.value.findIndex(r => r.id === row.original.id) | ||
if (rowSelection.value[index]) { | ||
rowSelection.value = { ...rowSelection.value, [index]: false } | ||
} | ||
} | ||
</script> | ||
|
||
<template> | ||
<div class=" flex w-full flex-1 gap-1"> | ||
<div class="flex-1"> | ||
<UTable | ||
ref="table" | ||
v-model:row-selection="rowSelection" | ||
:data="data" | ||
:columns="columns" | ||
@select="onSelect" | ||
/> | ||
<div class="px-4 py-3.5 border-t border-[var(--ui-border-accented)] text-sm text-[var(--ui-text-muted)]"> | ||
{{ table?.tableApi?.getFilteredSelectedRowModel().rows.length || 0 }} of | ||
{{ table?.tableApi?.getFilteredRowModel().rows.length || 0 }} row(s) selected. | ||
</div> | ||
</div> | ||
<div v-if="selectedRows.length>0"> | ||
<UTable | ||
|
||
:data="selectedRows" | ||
:ui="{ | ||
td: 'truncate ... max-w-28' | ||
}" | ||
:columns="[ | ||
{ accessorKey: 'email', header: 'Remove' } | ||
]" | ||
class="border border-[var(--ui-border-accented)] rounded-[var(--ui-radius)] " | ||
@select="quitSelect" | ||
/> | ||
</div> | ||
</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice! but out of curiosity, how would this work for accessibility / screenreaders as this behaves like a button?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe the tanstack table docs also would be missing to address this (or my question is silly!)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this could be solved if we add this attribute?
:role="props.onSelect ? 'button' : undefined"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might need
tabIndex="0"
too (and in that case, should it be white outline, or primary colour, or maybe hover effect that bg changes, maybe for simplity we stick to white outline, similar to anchor tags)(and implement navigation using arrow keys I suppose)