|
2 | 2 | import Pagination from "./Pagination.svelte"; |
3 | 3 | import PaginationItem from "./PaginationItem.svelte"; |
4 | 4 | import PaginationItemClear from "./PaginationItemClear.svelte"; |
5 | | - import { createEventDispatcher } from "svelte"; |
6 | 5 | import { generatePagination } from "./pagination-generator"; |
7 | 6 |
|
8 | | - /** |
9 | | - * The current page number |
10 | | - * @type {number} |
11 | | - */ |
12 | | - export let page: number; |
13 | | - /** |
14 | | - * The total number of pages |
15 | | - * @type {number} |
16 | | - */ |
17 | | - export let totalPages: number; |
18 | | - /** |
19 | | - * Function to generate the URL for a given page number |
20 | | - * @type {(page: number) => string} |
21 | | - */ |
22 | | - export let urlGenerator: (page: number) => string; |
23 | | - /** |
24 | | - * Whether to follow the link natively or rely on the pagechange event |
25 | | - * @type {boolean} |
26 | | - */ |
27 | | - export let followLink = true; |
28 | | - /** |
29 | | - * Localized translation for the visible "Next" page link text |
30 | | - */ |
31 | | - export let i18nNextText = "Next"; |
32 | | - /** |
33 | | - * Localized translation for the visible "Prev" page link text |
34 | | - */ |
35 | | - export let i18nPrevText = "Prev"; |
36 | | - /** |
37 | | - * Localized translation for Next/Prev "page" screen reader text |
38 | | - */ |
39 | | - export let i18nPageText = "page"; |
40 | | - /** |
41 | | - * Localized translation aria-label on nav element wrapping the pagination component |
42 | | - */ |
43 | | - export let i18nNavigationLabel: string = "Pagination"; |
| 7 | + interface Props { |
| 8 | + /** |
| 9 | + * The current page number |
| 10 | + */ |
| 11 | + page: number; |
| 12 | + /** |
| 13 | + * The total number of pages |
| 14 | + */ |
| 15 | + totalPages: number; |
| 16 | + /** |
| 17 | + * Function to generate the URL for a given page number |
| 18 | + */ |
| 19 | + urlGenerator: (page: number) => string; |
| 20 | + /** |
| 21 | + * Whether to follow the link natively or rely on the pagechange event |
| 22 | + */ |
| 23 | + followLink?: boolean; |
| 24 | + /** |
| 25 | + * Localized translation for the visible "Next" page link text |
| 26 | + */ |
| 27 | + i18nNextText?: string; |
| 28 | + /** |
| 29 | + * Localized translation for the visible "Prev" page link text |
| 30 | + */ |
| 31 | + i18nPrevText?: string; |
| 32 | + /** |
| 33 | + * Localized translation for Next/Prev "page" screen reader text |
| 34 | + */ |
| 35 | + i18nPageText?: string; |
| 36 | + /** |
| 37 | + * Localized translation aria-label on nav element wrapping the pagination component |
| 38 | + */ |
| 39 | + i18nNavigationLabel?: string; |
| 40 | + /** |
| 41 | + * Callback fired when a page is changed |
| 42 | + */ |
| 43 | + onpagechange?: (pageNumber: number) => void; |
| 44 | + } |
44 | 45 |
|
45 | | - const dispatch = createEventDispatcher<{ pagechange: number }>(); |
| 46 | + let { |
| 47 | + page, |
| 48 | + totalPages, |
| 49 | + urlGenerator, |
| 50 | + followLink = true, |
| 51 | + i18nNextText = "Next", |
| 52 | + i18nPrevText = "Prev", |
| 53 | + i18nPageText = "page", |
| 54 | + i18nNavigationLabel = "Pagination", |
| 55 | + onpagechange, |
| 56 | + }: Props = $props(); |
46 | 57 |
|
47 | 58 | /** |
48 | 59 | * Handles the click event for a pagination item |
|
52 | 63 | const onPaginationItemClick = (page: number) => (evt: Event) => { |
53 | 64 | if (!followLink) { |
54 | 65 | evt.preventDefault(); |
55 | | - dispatch("pagechange", page); |
| 66 | + onpagechange?.(page); |
56 | 67 | } |
57 | 68 | }; |
58 | 69 | </script> |
|
61 | 72 | {#if page > 1} |
62 | 73 | <PaginationItem |
63 | 74 | url={urlGenerator(page - 1)} |
64 | | - on:click={onPaginationItemClick(page - 1)} |
| 75 | + onclick={onPaginationItemClick(page - 1)} |
65 | 76 | > |
66 | 77 | {i18nPrevText} <span class="v-visible-sr">{i18nPageText}</span> |
67 | 78 | </PaginationItem> |
|
70 | 81 | {#if typeof p === "number"} |
71 | 82 | <PaginationItem |
72 | 83 | url={urlGenerator(p)} |
73 | | - on:click={onPaginationItemClick(p)} |
| 84 | + onclick={onPaginationItemClick(p)} |
74 | 85 | selected={p === page} |
75 | 86 | > |
76 | 87 | <span class="v-visible-sr">{i18nPageText}</span> |
|
83 | 94 | {#if page < totalPages} |
84 | 95 | <PaginationItem |
85 | 96 | url={urlGenerator(page + 1)} |
86 | | - on:click={onPaginationItemClick(page + 1)} |
| 97 | + onclick={onPaginationItemClick(page + 1)} |
87 | 98 | > |
88 | 99 | {i18nNextText} <span class="v-visible-sr">{i18nPageText}</span> |
89 | 100 | </PaginationItem> |
|
0 commit comments