Skip to content

Commit 5fc214c

Browse files
authored
feat(pagination): add next & previous events (#1357)
1 parent db0ae09 commit 5fc214c

File tree

3 files changed

+42
-14
lines changed

3 files changed

+42
-14
lines changed

packages/docs/components/Pagination.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,12 @@ The **Pagination** component is responsive and flexible way to indicate a series
5050

5151
### Events
5252

53-
| Event name | Properties | Description |
54-
| -------------- | ----------------------------------------- | ---------------------------- |
55-
| update:current | **value** `number` - updated current prop | current prop two-way binding |
56-
| change | **value** `number` - current value | on current change event |
53+
| Event name | Properties | Description |
54+
| -------------- | --------------------------------------------------------------------------------- | ---------------------------- |
55+
| update:current | **value** `number` - updated current prop | current prop two-way binding |
56+
| change | **value** `number` - current value | on current change event |
57+
| next | **event** `Event` - native click event<br/>**value** `number` - new current value | on next button event click |
58+
| previous | **event** `Event` - native click event<br/>**value** `number` - new current value | on previous button event |
5759

5860
### Slots
5961

packages/oruga/src/components/pagination/Pagination.vue

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,19 @@ const emits = defineEmits<{
5757
* on current change event
5858
* @param value {number} current value
5959
*/
60-
change: [event: number];
60+
change: [value: number];
61+
/**
62+
* on next button event click
63+
* @param event {Event} native click event
64+
* @param value {number} new current value
65+
*/
66+
next: [event: Event, value: number];
67+
/**
68+
* on previous button event
69+
* @param event {Event} native click event
70+
* @param value {number} new current value
71+
*/
72+
previous: [event: Event, value: number];
6173
}>();
6274
6375
const { isMobile } = useMatchMedia(props.mobileBreakpoint);
@@ -131,10 +143,23 @@ const pagesInRange = computed<ReturnType<typeof getPage>[]>(() => {
131143
return pages;
132144
});
133145
146+
const previousButtonBind = computed(() =>
147+
getPage(currentPage.value - 1, props.ariaPreviousLabel, (e, v) =>
148+
emits("previous", e, v),
149+
),
150+
);
151+
152+
const nextButtonBind = computed(() =>
153+
getPage(currentPage.value + 1, props.ariaNextLabel, (e, v) =>
154+
emits("next", e, v),
155+
),
156+
);
157+
134158
/** Get properties for a page */
135159
function getPage(
136160
num: number,
137161
ariaLabel?: string,
162+
onClick?: (event: Event, value: number) => void,
138163
): {
139164
number: number;
140165
isCurrent: boolean;
@@ -145,9 +170,12 @@ function getPage(
145170
return {
146171
number: num,
147172
isCurrent: currentPage.value === num,
148-
onClick: (event: Event): void => changePage(num, event),
173+
onClick: (event: Event): void => {
174+
changePage(num, event);
175+
if (onClick) onClick(event, num);
176+
},
149177
ariaLabel:
150-
ariaLabel || getAriaPageLabel(num, currentPage.value === num),
178+
ariaLabel ?? getAriaPageLabel(num, currentPage.value === num),
151179
tag: props.buttonTag,
152180
};
153181
}
@@ -270,11 +298,9 @@ defineExpose({ last, first, prev, next });
270298
@binding {(event: Event): void} onClick - click handler
271299
@binding {string} ariaLabel - aria-label attribute
272300
-->
273-
<slot
274-
name="previous"
275-
v-bind="getPage(currentPage - 1, ariaPreviousLabel)">
301+
<slot name="previous" v-bind="previousButtonBind">
276302
<o-pagination-button
277-
v-bind="getPage(currentPage - 1, ariaPreviousLabel)"
303+
v-bind="previousButtonBind"
278304
:disabled="isFirst"
279305
:root-class="buttonPrevClasses"
280306
:button-class="buttonClasses"
@@ -290,9 +316,9 @@ defineExpose({ last, first, prev, next });
290316
@binding {(event: Event): void} onClick - click handler
291317
@binding {string} ariaLabel - aria-label attribute
292318
-->
293-
<slot name="next" v-bind="getPage(currentPage + 1, ariaNextLabel)">
319+
<slot name="next" v-bind="nextButtonBind">
294320
<o-pagination-button
295-
v-bind="getPage(currentPage + 1, ariaNextLabel)"
321+
v-bind="nextButtonBind"
296322
:disabled="isLast"
297323
:root-class="buttonNextClasses"
298324
:button-class="buttonClasses"

packages/oruga/src/components/pagination/examples/customise.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ const size = ref("");
7171
<o-pagination
7272
v-model:current="current"
7373
:total="total"
74+
:per-page="perPage"
7475
:range-before="rangeBefore || 0"
7576
:range-after="rangeAfter || 0"
7677
:order="order"
7778
:size="size"
7879
:simple="isSimple"
7980
:rounded="isRounded"
80-
:per-page="perPage"
8181
:icon-prev="prevIcon"
8282
:icon-next="nextIcon" />
8383

0 commit comments

Comments
 (0)