Skip to content
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

fix(table): selected use prop key #95

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/chilled-dolls-boil.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@indielayer/ui": patch
---

fix(table): selected use prop key
7 changes: 4 additions & 3 deletions packages/ui/docs/pages/component/table/usage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const itemsSorted = computed<Book[]>(() => {
})
})

const selectedIndex = ref<number>(0)
const selected = ref<number>(2)
</script>

<template>
Expand Down Expand Up @@ -89,13 +89,14 @@ const selectedIndex = ref<number>(0)
<x-card>
<x-table
v-model:sort="sort"
v-model:selected="selectedIndex"
v-model:selected="selected"
key-prop="id"
dense
pointer
striped
:headers="headers"
:items="itemsSorted"
@click-row="(event, index) => { notifications?.log(event); selectedIndex = index }"
@click-row="(event, index) => { notifications?.log(event); selected = event.id }"
>
<template #item-published="{ item }">
{{ formatDate(item.published) }}
Expand Down
11 changes: 6 additions & 5 deletions packages/ui/src/components/table/Table.vue
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const tableProps = {
type: Number,
default: 5,
},
keyProp: String,
}

export type TableHeader = {
Expand All @@ -65,7 +66,7 @@ export default { name: 'XTable' }
</script>

<script setup lang="ts" generic="T">
import { ref, type ExtractPublicPropTypes, type PropType, watch, computed, type MaybeRef } from 'vue'
import { ref, type ExtractPublicPropTypes, type PropType, watch, computed } from 'vue'
import { useTheme, type ThemeComponent } from '../../composables/useTheme'
import { useVirtualList } from '../../composables/useVirtualList'

Expand All @@ -90,9 +91,9 @@ const props = defineProps({
},
})

const selectedIndex = defineModel<number | number[]>('selected')
const selected = defineModel<number | number[]>('selected')

const hasSelectedIndex = computed(() => typeof selectedIndex.value === 'number')
const hasSelected = computed(() => typeof selected.value === 'number')

type internalT = T & {
__expanded?: boolean;
Expand Down Expand Up @@ -257,11 +258,11 @@ const { styles, classes, className } = useTheme('Table', {}, props)
</td>
</tr>
</template>
<template v-for="(item, index) in list" v-else :key="index">
<template v-for="(item, index) in list" v-else :key="keyProp ?? index">
<x-table-row
:pointer="pointer"
:striped="striped"
:selected="hasSelectedIndex ? selectedIndex === item.index : undefined"
:selected="hasSelected ? selected === (keyProp ? (item.data as Record<string, unknown>)[keyProp] : item.index) : undefined"
@click="$emit('click-row', item.data, item.index)"
>
<x-table-cell v-if="expandable" width="48" class="!p-1">
Expand Down
Loading