Skip to content

Commit

Permalink
Improve detection of no item rows in Table component (pkp#421)
Browse files Browse the repository at this point in the history
* Improve detection of no item rows in Table component

* Update how columnsCount are managed in TableColumn component
  • Loading branch information
blesildaramirez authored Oct 7, 2024
1 parent 346ecd0 commit 91cfdb1
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 9 deletions.
42 changes: 42 additions & 0 deletions src/components/Table/Table.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,45 @@ export const WithTitleAndDescription = {

args: {},
};

export const WithNoItems = {
render: (args) => ({
components: {
PkpTable,
TableHeader,
TableBody,
TableRow,
TableColumn,
TableCell,
PkpButton,
},
setup() {
return {args};
},
template: `
<PkpTable>
<template #label>
No Items
</template>
<template #description>
There's a total of <strong>0</strong> items(s).
</template>
<TableHeader>
<TableColumn>ID</TableColumn>
<TableColumn>Title</TableColumn>
<TableColumn>Views</TableColumn>
<TableColumn>Downloads</TableColumn>
<TableColumn>Total</TableColumn>
<TableColumn>Action</TableColumn>
</TableHeader>
<TableBody>
<template #no-content>
This is a custom no-content slot. No items available.
</template>
</TableBody>
</PkpTable>
`,
}),

args: {},
};
18 changes: 12 additions & 6 deletions src/components/Table/TableBody.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<tbody class="">
<slot></slot>
<tr v-if="noContent">
<tr v-if="!rowCount">
<td
:colspan="tableContext.columnsCount.value"
class="border-x border-b border-light p-5 text-base-normal"
Expand All @@ -14,7 +14,7 @@
</template>

<script setup>
import {useSlots, computed, inject} from 'vue';
import {useSlots, computed, inject, provide, ref} from 'vue';
import {useLocalize} from '@/composables/useLocalize';
const props = defineProps({
Expand All @@ -25,12 +25,18 @@ const slots = useSlots();
const {t} = useLocalize();
const tableContext = inject('tableContext');
const rowCount = ref(0);
const noContent = computed(() => {
const defaultSlot = slots.default ? slots.default() : [];
const registerRow = () => {
rowCount.value++;
};
return !defaultSlot?.[0]?.children?.length && !defaultSlot?.[0]?.type?.render;
});
const unregisterRow = () => {
rowCount.value--;
};
provide('registerRow', registerRow);
provide('unregisterRow', unregisterRow);
const emptyText = computed(() => {
return props.emptyText || t('grid.noItems');
Expand Down
10 changes: 8 additions & 2 deletions src/components/Table/TableColumn.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</template>

<script setup>
import {defineProps, inject} from 'vue';
import {defineProps, inject, onMounted, onUnmounted} from 'vue';
import Icon from '@/components/Icon/Icon.vue';
const props = defineProps({
Expand All @@ -32,5 +32,11 @@ const props = defineProps({
const tableContext = inject('tableContext');
tableContext.columnsCount.value++;
onMounted(() => {
tableContext.columnsCount.value++;
});
onUnmounted(() => {
tableContext.columnsCount.value--;
});
</script>
18 changes: 17 additions & 1 deletion src/components/Table/TableRow.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,20 @@
</tr>
</template>

<script setup></script>
<script setup>
import {inject, onMounted, onUnmounted} from 'vue';
// Inject the register and unregister functions from TableBody
const registerRow = inject('registerRow', null);
const unregisterRow = inject('unregisterRow', null);
// Register the row when the component is mounted
onMounted(() => {
if (registerRow) registerRow();
});
// Unregister the row when the component is unmounted
onUnmounted(() => {
if (unregisterRow) unregisterRow();
});
</script>

0 comments on commit 91cfdb1

Please sign in to comment.