1- <script setup lang="ts">
1+ <script setup lang="ts" generic = " S extends any [] | any | undefined = undefined " >
22import { useVirtualizer } from ' @tanstack/vue-virtual'
33import { useResizeObserver } from ' @vueuse/core'
44import xor from ' lodash-es/xor'
@@ -15,6 +15,7 @@ import {
1515import { type Table } from ' ../composables/Table'
1616import { getTextWidth } from ' ../support/Text'
1717import SInputCheckbox from ' ./SInputCheckbox.vue'
18+ import SInputRadio from ' ./SInputRadio.vue'
1819import SSpinner from ' ./SSpinner.vue'
1920import STableCell from ' ./STableCell.vue'
2021import STableColumn from ' ./STableColumn.vue'
@@ -24,11 +25,11 @@ import STableItem from './STableItem.vue'
2425
2526const props = defineProps <{
2627 options: Table
27- selected? : unknown []
28+ selected? : S
2829}>()
2930
3031const emit = defineEmits <{
31- (e : ' update:selected' , value : unknown [] ): void
32+ (e : ' update:selected' , value : S ): void
3233}>()
3334
3435const head = shallowRef <HTMLElement | null >(null )
@@ -41,7 +42,7 @@ const ordersToShow = computed(() => {
4142 const show = unref (props .options .columns )[key ]?.show
4243 return toValue (show ) !== false
4344 })
44- if (! props .selected ) {
45+ if (props .selected === undefined ) {
4546 return orders
4647 }
4748 return [' __select' , ... orders ]
@@ -126,7 +127,7 @@ const recordsWithSummary = computed(() => {
126127})
127128
128129const indexes = computed (() => {
129- if (! props .selected ) {
130+ if (props .selected === undefined ) {
130131 return []
131132 }
132133 const records = unref (props .options .records ) ?? []
@@ -139,9 +140,6 @@ const selectedIndexes = reactive(new Set())
139140
140141const control = computed ({
141142 get() {
142- if (! props .selected ) {
143- return false
144- }
145143 const selected = indexes .value .filter ((index ) => {
146144 return selectedIndexes .has (index )
147145 })
@@ -165,12 +163,11 @@ const control = computed({
165163})
166164
167165watch (indexes , (newValue , oldValue ) => {
168- if (! props .selected ) {
169- return
166+ if (Array .isArray (props .selected )) {
167+ xor (newValue , oldValue ).forEach ((index ) => {
168+ selectedIndexes .delete (index )
169+ })
170170 }
171- xor (newValue , oldValue ).forEach ((index ) => {
172- selectedIndexes .delete (index )
173- })
174171})
175172
176173const virtualizerOptions = computed (() => ({
@@ -343,8 +340,12 @@ function getCell(key: string, index: number) {
343340 return (isSummary (index ) && col ?.summaryCell ) ? col ?.summaryCell : col ?.cell
344341}
345342
346- function updateSelected(selected : unknown []) {
347- if (xor (selected , props .selected ?? []).length ) {
343+ function updateSelected(selected : any ) {
344+ if (Array .isArray (props .selected )) {
345+ if (xor (selected , props .selected ?? []).length ) {
346+ emit (' update:selected' , selected )
347+ }
348+ } else {
348349 emit (' update:selected' , selected )
349350 }
350351}
@@ -361,7 +362,7 @@ function updateSelected(selected: unknown[]) {
361362 :actions =" unref(options.actions)"
362363 :borderless =" unref(options.borderless)"
363364 :on-reset =" options.onReset"
364- :selected =" selected"
365+ :selected =" Array.isArray( selected) ? selected : undefined "
365366 />
366367
367368 <div class =" table" role =" grid" >
@@ -389,7 +390,7 @@ function updateSelected(selected: unknown[]) {
389390 @resize =" (value) => updateColWidth(key, value, true)"
390391 >
391392 <SInputCheckbox
392- v-if =" key === '__select' && unref(options.records)?.length"
393+ v-if =" Array.isArray(selected) && key === '__select' && unref(options.records)?.length"
393394 v-model =" control"
394395 />
395396 </STableColumn >
@@ -444,11 +445,18 @@ function updateSelected(selected: unknown[]) {
444445 :record =" recordsWithSummary[index]"
445446 :records =" unref(options.records)!"
446447 >
447- <SInputCheckbox
448- v-if =" key === '__select' && !isSummary(index)"
449- :value =" selectedIndexes.has(indexes[index])"
450- @change =" c => selectedIndexes[c ? 'add' : 'delete'](indexes[index])"
451- />
448+ <template v-if =" key === ' __select' && ! isSummary (index )" >
449+ <SInputCheckbox
450+ v-if =" Array.isArray(selected)"
451+ :model-value =" selectedIndexes.has(indexes[index])"
452+ @update:model-value =" c => selectedIndexes[c ? 'add' : 'delete'](indexes[index])"
453+ />
454+ <SInputRadio
455+ v-else
456+ :model-value =" selected === indexes[index]"
457+ @update:model-value =" c => updateSelected(c ? indexes[index] : null)"
458+ />
459+ </template >
452460 </STableCell >
453461 </STableItem >
454462 </div >
@@ -598,6 +606,7 @@ function updateSelected(selected: unknown[]) {
598606 align-items : center;
599607 padding : 0 16 px ;
600608 min-height : 40 px ;
609+ user-select : none;
601610 }
602611
603612 :deep (.container) {
0 commit comments