Composables
useRowSelection
Select records one by one or all at once, and keep their keys, for a table or any other list.
Usage
useRowSelection is the selection of the Table. Use it on its own for a list of cards with checkboxes, for example.
<script setup lang="ts">
import { useRowSelection } from '@kuina-ui/vue'
import { ref } from 'vue'
interface Transaction {
id: string
merchant: string
status: 'Completed' | 'Pending' | 'Failed'
}
const visibleTransactions: Transaction[] = [
{ id: 'txn-1048', merchant: 'Corner Coffee', status: 'Completed' },
{ id: 'txn-1046', merchant: 'Streamly', status: 'Pending' },
{ id: 'txn-1045', merchant: 'Skyway Airlines', status: 'Failed' },
]
const selectedTransactionIds = ref<string[]>([])
const { selectionState, isRowSelected, isRowSelectable, toggleRow, toggleAllRows } = useRowSelection(
visibleTransactions,
selectedTransactionIds,
{ isRowSelectable: (transaction) => transaction.status !== 'Failed' },
)
</script>
<template>
<KCheckbox
label="Select all"
:model-value="selectionState === 'all'"
:indeterminate="selectionState === 'some'"
@update:model-value="toggleAllRows"
/>
<KCheckbox
v-for="transaction in visibleTransactions"
:key="transaction.id"
:label="transaction.merchant"
:model-value="isRowSelected(transaction)"
:disabled="!isRowSelectable(transaction)"
@update:model-value="(isSelected) => toggleRow(transaction, isSelected)"
/>
<p>{{ selectedTransactionIds.length }} selected</p>
</template>
The keys are read from id, or from rowKey, a field name or a function. "All" covers the records you give, like the current page: selecting them all keeps the keys of records on other pages.
With the Nuxt module, useRowSelection is auto-imported. In a Vue app, import it from @kuina-ui/vue.
API
useRowSelection(rows, selectedRowKeys, options?)
Options
| Option | Type | Default |
|---|---|---|
rowKey | keyof Row | (row) => string | number | 'id' |
isRowSelectable | (row) => boolean |
Returned values
| Value | Type |
|---|---|
selectionState | ComputedRef<'none' | 'some' | 'all'> |
selectedRowCount | ComputedRef<number> |
isRowSelected | (row) => boolean |
isRowSelectable | (row) => boolean |
toggleRow | (row, shouldSelect?) => void |
toggleAllRows | (shouldSelect?) => void |
clearSelection | () => void |