Skip to content

A self-hosted media server and file management system

fix: cedar ui use table does not support reactive itemsPerPage

aminnausind631d01bb878
diff --git a/resources/js/components/cedar-ui/table/TableBase.vue b/resources/js/components/cedar-ui/table/TableBase.vue
index 68e636c5..b6946c9b 100644
--- a/resources/js/components/cedar-ui/table/TableBase.vue
+++ b/resources/js/components/cedar-ui/table/TableBase.vue
@@ -4,13 +4,14 @@ import type { Component } from 'vue';
import { PhSortDescendingLight, PhSortAscendingLight } from '../icons';
import { onMounted, ref, toRef } from 'vue';
-import { cn, useTable } from '@aminnausin/cedar-ui';
import { InputSelect } from '../select';
import { ButtonIcon } from '../button';
import { TextInput } from '../input';
+import { cn } from '@aminnausin/cedar-ui';
import TableLoadingSpinner from './TableLoadingSpinner.vue';
import TablePagination from './TablePagination.vue';
+import useTable from '@/composables/useTable';
const props = withDefaults(defineProps<TableProps<T> & { forceVerticalToolbar?: boolean; sticky?: boolean; stickyClass?: string; loadingPlaceholder?: Component }>(), {
useToolbar: true,
@@ -23,7 +24,7 @@ const props = withDefaults(defineProps<TableProps<T> & { forceVerticalToolbar?:
noResultsMessage: 'No Results',
loadingPlaceholder: TableLoadingSpinner,
});
-const { currentPage, itemsPerPage, pageData, setPage } = useTable<T>({ itemsPerPage: props.itemsPerPage, data: toRef(props, 'data') });
+const { currentPage, itemsPerPage, pageData, setPage } = useTable<T>({ itemsPerPage: () => props.itemsPerPage, data: toRef(props, 'data') });
const sortAscending = ref(props.startAscending);
const lastSortKey = ref('');
diff --git a/resources/js/composables/useTable.ts b/resources/js/composables/useTable.ts
index 5ea757be..1692159c 100644
--- a/resources/js/composables/useTable.ts
+++ b/resources/js/composables/useTable.ts
@@ -1,30 +1,43 @@
-import type { UseTableOptions } from '@aminnausin/cedar-ui';
+import type { MaybeRefOrGetter, Ref } from 'vue';
+import type { TableRow } from '@aminnausin/cedar-ui';
-import { computed, ref, toRef, watch } from 'vue';
+import { computed, ref, toValue, watch } from 'vue';
-export default function useTable<T>(options: UseTableOptions<T>) {
- const itemsPerPage = ref(options.itemsPerPage ?? 10);
+interface UseTableOptions<T> {
+ data: Ref<T[]>;
+ itemsPerPage?: MaybeRefOrGetter<number>;
+ resetOnDataChange?: MaybeRefOrGetter<boolean>;
+}
+
+export default function useTable<T extends TableRow>(options: UseTableOptions<T>) {
+ const itemsPerPage = computed(() => toValue(options.itemsPerPage) ?? 10);
const currentPage = ref(1);
- const data = toRef(options, 'data');
- const pageCount = computed(() => Math.ceil(data.value.length / itemsPerPage.value));
+ const pageCount = computed(() => Math.ceil(options.data.value.length / itemsPerPage.value));
const pageData = computed(() => {
const start = (currentPage.value - 1) * itemsPerPage.value;
- return data.value.slice(start, start + itemsPerPage.value);
+ return options.data.value.slice(start, start + itemsPerPage.value);
});
function setPage(page: number) {
- currentPage.value = Math.min(Math.max(1, page), pageCount.value);
+ currentPage.value = Math.min(Math.max(1, page), Math.max(1, pageCount.value));
}
function resetPage() {
currentPage.value = 1;
}
+ watch(itemsPerPage, () => resetPage());
+
watch(
- () => data,
- () => {
- if (options.resetOnDataChange !== false) resetPage();
+ options.data,
+ (next, prev) => {
+ if (toValue(options.resetOnDataChange) === false) return;
+
+ if (!prev) return resetPage(); // On first load
+ if (!next.length || !prev.length) return resetPage(); // Handles empty datasets ?
+ if (next.length !== prev.length) return resetPage(); // If length changes (search, data source change)
+ if (next[0]?.id !== prev[0]?.id) return resetPage(); // If data changes (first item is different but lengths are the same)
},
{ immediate: true },
);