Skip to content

Commit

Permalink
fix: cv-pagination make page, pageSizes, & numberOfItems properties r…
Browse files Browse the repository at this point in the history
…eactive
  • Loading branch information
davidnixon committed Mar 31, 2024
1 parent 64cbe0c commit b9c4c09
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
37 changes: 22 additions & 15 deletions src/components/CvPagination/CvPagination.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { Canvas, Meta, Story } from '@storybook/addon-docs';
import { CvPagination } from '.';
import { sbCompPrefix } from '../../global/storybook-utils';
import { action } from '@storybook/addon-actions';
import { ref } from 'vue';
const myPage=ref(1);
const myPageSizes=ref([5,7,10,11,13])
const myNumItems=ref(13)

<Meta title={`${sbCompPrefix}/CvPagination`} component={CvPagination} />

Expand All @@ -14,6 +18,9 @@ export const Template = args => ({
setup() {
return {
...args,
myPage,
myPageSizes,
myNumItems,
onChange: action('change'),
};
},
Expand All @@ -27,13 +34,22 @@ const defaultTemplate = `
:forward-text="forwardText"
:page-number-label="pageNumberLabel"
:page-sizes-label="pageSizesLabel"
:number-of-items="numberOfItems"
:number-of-items="myNumItems"
:actual-items-on-page="actualItemsOnPage"
:page="page"
:page-sizes="pageSizes"
:page="myPage"
:page-sizes="myPageSizes"
:backwards-button-disabled="backwardsButtonDisabled"
:forwards-button-disabled="forwardsButtonDisabled">
</cv-pagination>
<div style="margin-top:2rem; background-color: #888888; padding:1rem">
<h3>Reactive properties</h3>
page: <button @click="myPage--">← prev page</button>
<button @click="myPage++">next page →</button><br/>
page-sizes: <button @click="myPageSizes=[5,10]">change page sizes to [5,10]</button>
<button @click="myPageSizes=[10,20,30,40,50]">change page sizes to [10,20,30,40,50]</button><br/>
number-of-items: <button @click="myNumItems=4">Set total items = 4</button>
<button @click="myNumItems=103">Set total items = 103</button>
</div>
`;
const slotsTemplate = `
<cv-pagination
Expand Down Expand Up @@ -87,23 +103,14 @@ Migration notes:
forwardText: undefined,
pageNumberLabel: undefined,
pageSizesLabel: undefined,
numberOfItems: 103,
page: 2,
pageSizes: [
10,
{
value: 20,
selected: true,
},
30,
40,
50,
],
backwardsButtonDisabled: false,
forwardsButtonDisabled: false,
actualItemsOnPage: Infinity,
}}
argTypes={{
page: { control: false },
pageSizes: { control: false },
numberOfItems: { control: false }
}}
>
{Template.bind({})}
Expand Down
17 changes: 16 additions & 1 deletion src/components/CvPagination/CvPagination.vue
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,30 @@ const internalValue = computed(() => {
};
});
onMounted(() => {
function adjustValues() {
pageSizeValue.value = newPageSizeValue(props.pageSizes);
pageCount.value = newPageCount(props.numberOfItems, pageSizeValue.value);
pageValue.value = newPageValue(props.page, pageCount.value);
pages.value = newPagesArray(pageCount.value);
firstItem.value = newFirstItem(pageValue.value, pageSizeValue.value);
}
onMounted(() => {
adjustValues();
// always emit on mount
emit('change', internalValue.value);
});
watch(
() => props.pageSizes,
() => adjustValues()
);
watch(
() => props.page,
() => adjustValues()
);
watch(
() => props.numberOfItems,
() => adjustValues()
);
function onPageChange(newVal) {
pageValue.value = parseInt(newVal, 10);
Expand Down

0 comments on commit b9c4c09

Please sign in to comment.