Skip to content

Commit 3947513

Browse files
authored
Merge pull request #2 from MGREMY/pagination
refactor(pagination): update post update post comment
2 parents 0ec0dfa + 021058b commit 3947513

8 files changed

Lines changed: 58 additions & 118 deletions

File tree

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
<flowbite-pagination
2-
[currentPage]="1"
32
[totalItems]="100"
4-
[navigation]="'both'" />
3+
[navigationMode]="'both'" />

apps/docs/docs/components/pagination/_custom.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
[currentPage]="20"
33
[totalItems]="100000"
44
[pageSize]="50"
5-
[firstLast]="false"
5+
[hasFirstLast]="false"
66
[ariaLabel]="'Custom pagination'"
77
[customStyle]="{
88
root: { base: 'flex gap-2' },
Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1 @@
1-
<table>
2-
@for (item of testDatas().items; track $index) {
3-
<tr>
4-
<td>{{ item.id }}</td>
5-
<td>{{ item.name }}</td>
6-
</tr>
7-
}
8-
</table>
9-
10-
<flowbite-pagination
11-
[currentPage]="currentPage()"
12-
(currentPageChange)="pageChangeHandler($event)"
13-
[totalItems]="testDatas().totalItems"
14-
[pageSize]="pageSize()" />
1+
<flowbite-pagination [totalItems]="1000" />
Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,10 @@
11
import { PaginationComponent } from 'flowbite-angular/pagination';
22

3-
import { Component, signal } from '@angular/core';
3+
import { Component } from '@angular/core';
44

55
@Component({
6-
// ng-doc-ignore-line
76
selector: 'flowbite-demo-pagination-default',
87
imports: [PaginationComponent],
9-
// ng-doc-ignore-line
108
templateUrl: './_default.component.html',
119
})
12-
export class FlowbiteDefaultComponent {
13-
private readonly _testDatas: IdNameDto[] = [];
14-
15-
readonly currentPage = signal(1);
16-
readonly pageSize = signal(10);
17-
readonly testDatas = signal(PagedResult.empty<IdNameDto>());
18-
19-
constructor() {
20-
// fetch test datas
21-
for (let i = 1; i <= 105; i++) {
22-
this._testDatas.push({ id: i, name: 'John Doe' });
23-
}
24-
25-
// simulate API call
26-
this.pageChangeHandler(1);
27-
}
28-
29-
pageChangeHandler(page: number) {
30-
this.currentPage.set(page);
31-
32-
// Call your API here
33-
// The following lines of code simulates a backend query
34-
const _items: IdNameDto[] = [];
35-
const _start = (this.currentPage() - 1) * this.pageSize();
36-
const _end = Math.min(this._testDatas.length, _start + this.pageSize());
37-
38-
for (let i = _start; i < _end; i++) {
39-
_items.push({ id: i + 1, name: 'John Doe' });
40-
}
41-
42-
this.testDatas.set({
43-
items: _items,
44-
totalItems: this._testDatas.length,
45-
});
46-
}
47-
}
48-
49-
export interface IdNameDto {
50-
id: number;
51-
name: string;
52-
}
53-
54-
export interface PagedResult<T> {
55-
items: T[];
56-
totalItems: number;
57-
}
58-
59-
export const PagedResult = {
60-
empty<T>(): PagedResult<T> {
61-
return { items: [], totalItems: 0 };
62-
},
63-
};
10+
export class FlowbiteDefaultComponent {}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
<flowbite-pagination
2-
[currentPage]="1"
32
[totalItems]="100"
4-
[navigation]="'text'" />
3+
[navigationMode]="'text'" />

libs/flowbite-angular/pagination/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
export {
22
PaginationComponent,
33
paginationDefaultValueProvider,
4+
FLOWBITE_PAGINATION_CURRENT_PAGE_DEFAULT_VALUE,
5+
FLOWBITE_PAGINATION_BUTTON_PROPERTIES_DEFAULT_VALUE,
46
FLOWBITE_PAGINATION_CUSTOM_STYLE_DEFAULT_VALUE,
57
FLOWBITE_PAGINATION_PREVIOUS_ICON_DEFAULT_VALUE,
68
FLOWBITE_PAGINATION_NEXT_ICON_DEFAULT_VALUE,
79
FLOWBITE_PAGINATION_FIRST_ICON_DEFAULT_VALUE,
810
FLOWBITE_PAGINATION_LAST_ICON_DEFAULT_VALUE,
911
FLOWBITE_PAGINATION_TABS_DEFAULT_VALUE,
1012
FLOWBITE_PAGINATION_PAGE_SIZE_DEFAULT_VALUE,
11-
FLOWBITE_PAGINATION_FIRSTLAST_DEFAULT_VALUE,
12-
FLOWBITE_PAGINATION_PREVNEXT_DEFAULT_VALUE,
13-
FLOWBITE_PAGINATION_NAVIGATION_DEFAULT_VALUE,
13+
FLOWBITE_PAGINATION_HAS_FIRST_LAST_DEFAULT_VALUE,
14+
FLOWBITE_PAGINATION_HAS_PREV_NEXT_DEFAULT_VALUE,
15+
FLOWBITE_PAGINATION_NAVIGATION_MODE_DEFAULT_VALUE,
1416
FLOWBITE_PAGINATION_SIZE_DEFAULT_VALUE,
1517
} from './pagination.component';
1618
export type { PaginationProperties, PaginationClass, PaginationTheme } from './pagination.theme';

libs/flowbite-angular/pagination/pagination.component.ts

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ import {
3131
} from '@angular/core';
3232
import { DomSanitizer } from '@angular/platform-browser';
3333

34+
export const FLOWBITE_PAGINATION_CURRENT_PAGE_DEFAULT_VALUE = new InjectionToken<number>(
35+
'FLOWBITE_PAGINATION_CURRENT_PAGE_DEFAULT_VALUE'
36+
);
37+
3438
export const FLOWBITE_PAGINATION_CUSTOM_STYLE_DEFAULT_VALUE = new InjectionToken<
3539
DeepPartial<PaginationTheme>
3640
>('FLOWBITE_PAGINATION_CUSTOM_STYLE_DEFAULT_VALUE');
@@ -59,17 +63,17 @@ export const FLOWBITE_PAGINATION_PAGE_SIZE_DEFAULT_VALUE = new InjectionToken<nu
5963
'FLOWBITE_PAGINATION_PAGE_SIZE_DEFAULT_VALUE'
6064
);
6165

62-
export const FLOWBITE_PAGINATION_FIRSTLAST_DEFAULT_VALUE = new InjectionToken<boolean>(
63-
'FLOWBITE_PAGINATION_FIRSTLAST_DEFAULT_VALUE'
66+
export const FLOWBITE_PAGINATION_HAS_FIRST_LAST_DEFAULT_VALUE = new InjectionToken<boolean>(
67+
'FLOWBITE_PAGINATION_HAS_FIRST_LAST_DEFAULT_VALUE'
6468
);
6569

66-
export const FLOWBITE_PAGINATION_PREVNEXT_DEFAULT_VALUE = new InjectionToken<boolean>(
67-
'FLOWBITE_PAGINATION_PREVNEXT_DEFAULT_VALUE'
70+
export const FLOWBITE_PAGINATION_HAS_PREV_NEXT_DEFAULT_VALUE = new InjectionToken<boolean>(
71+
'FLOWBITE_PAGINATION_HAS_PREV_NEXT_DEFAULT_VALUE'
6872
);
6973

70-
export const FLOWBITE_PAGINATION_NAVIGATION_DEFAULT_VALUE = new InjectionToken<
74+
export const FLOWBITE_PAGINATION_NAVIGATION_MODE_DEFAULT_VALUE = new InjectionToken<
7175
keyof PaginationNavigation
72-
>('FLOWBITE_PAGINATION_NAVIGATION_DEFAULT_VALUE');
76+
>('FLOWBITE_PAGINATION_NAVIGATION_MODE_DEFAULT_VALUE');
7377

7478
export const FLOWBITE_PAGINATION_SIZE_DEFAULT_VALUE = new InjectionToken<keyof PaginationSizes>(
7579
'FLOWBITE_PAGINATION_SIZE_DEFAULT_VALUE'
@@ -80,6 +84,10 @@ export const FLOWBITE_PAGINATION_BUTTON_PROPERTIES_DEFAULT_VALUE = new Injection
8084
>('FLOWBITE_PAGINATION_BUTTON_PROPERTIES_DEFAULT_VALUE');
8185

8286
export const paginationDefaultValueProvider = makeEnvironmentProviders([
87+
{
88+
provide: FLOWBITE_PAGINATION_CURRENT_PAGE_DEFAULT_VALUE,
89+
useValue: 1,
90+
},
8391
{
8492
provide: FLOWBITE_PAGINATION_CUSTOM_STYLE_DEFAULT_VALUE,
8593
useValue: {},
@@ -109,15 +117,15 @@ export const paginationDefaultValueProvider = makeEnvironmentProviders([
109117
useValue: 25,
110118
},
111119
{
112-
provide: FLOWBITE_PAGINATION_FIRSTLAST_DEFAULT_VALUE,
120+
provide: FLOWBITE_PAGINATION_HAS_FIRST_LAST_DEFAULT_VALUE,
113121
useValue: true,
114122
},
115123
{
116-
provide: FLOWBITE_PAGINATION_PREVNEXT_DEFAULT_VALUE,
124+
provide: FLOWBITE_PAGINATION_HAS_PREV_NEXT_DEFAULT_VALUE,
117125
useValue: true,
118126
},
119127
{
120-
provide: FLOWBITE_PAGINATION_NAVIGATION_DEFAULT_VALUE,
128+
provide: FLOWBITE_PAGINATION_NAVIGATION_MODE_DEFAULT_VALUE,
121129
useValue: 'icon',
122130
},
123131
{
@@ -150,7 +158,7 @@ export const paginationDefaultValueProvider = makeEnvironmentProviders([
150158
standalone: true,
151159
imports: [IconComponent, NgTemplateOutlet, ButtonComponent],
152160
template: `
153-
@if (firstLast()) {
161+
@if (hasFirstLast()) {
154162
<flowbite-button
155163
type="button"
156164
data-active="false"
@@ -159,7 +167,7 @@ export const paginationDefaultValueProvider = makeEnvironmentProviders([
159167
[fill]="buttonProperties().fill!"
160168
[size]="size()"
161169
[customStyle]="buttonProperties().customStyle!">
162-
@if (['icon', 'both'].includes(navigation())) {
170+
@if (['icon', 'both'].includes(navigationMode())) {
163171
@if (firstIcon()) {
164172
<ng-container [ngTemplateOutlet]="firstIcon()!" />
165173
} @else {
@@ -168,13 +176,13 @@ export const paginationDefaultValueProvider = makeEnvironmentProviders([
168176
[class]="contentClasses().iconClass" />
169177
}
170178
}
171-
@if (['text', 'both'].includes(navigation())) {
179+
@if (['text', 'both'].includes(navigationMode())) {
172180
<span>First</span>
173181
}
174182
</flowbite-button>
175183
}
176184
177-
@if (prevNext()) {
185+
@if (hasPrevNext()) {
178186
<flowbite-button
179187
type="button"
180188
data-active="false"
@@ -183,7 +191,7 @@ export const paginationDefaultValueProvider = makeEnvironmentProviders([
183191
[fill]="buttonProperties().fill!"
184192
[size]="size()"
185193
[customStyle]="buttonProperties().customStyle!">
186-
@if (['icon', 'both'].includes(navigation())) {
194+
@if (['icon', 'both'].includes(navigationMode())) {
187195
@if (previousIcon()) {
188196
<ng-container [ngTemplateOutlet]="previousIcon()!" />
189197
} @else {
@@ -192,7 +200,7 @@ export const paginationDefaultValueProvider = makeEnvironmentProviders([
192200
[class]="contentClasses().iconClass" />
193201
}
194202
}
195-
@if (['text', 'both'].includes(navigation())) {
203+
@if (['text', 'both'].includes(navigationMode())) {
196204
<span>Previous</span>
197205
}
198206
</flowbite-button>
@@ -202,7 +210,7 @@ export const paginationDefaultValueProvider = makeEnvironmentProviders([
202210
<flowbite-button
203211
type="button"
204212
[attr.data-active]="page === currentPage()"
205-
(click)="changePage(page)"
213+
(click)="goToPage(page)"
206214
[color]="buttonProperties().color!"
207215
[fill]="buttonProperties().fill!"
208216
[size]="size()"
@@ -211,7 +219,7 @@ export const paginationDefaultValueProvider = makeEnvironmentProviders([
211219
</flowbite-button>
212220
}
213221
214-
@if (prevNext()) {
222+
@if (hasPrevNext()) {
215223
<flowbite-button
216224
type="button"
217225
data-active="false"
@@ -220,10 +228,10 @@ export const paginationDefaultValueProvider = makeEnvironmentProviders([
220228
[fill]="buttonProperties().fill!"
221229
[size]="size()"
222230
[customStyle]="buttonProperties().customStyle!">
223-
@if (['text', 'both'].includes(navigation())) {
231+
@if (['text', 'both'].includes(navigationMode())) {
224232
<span>Next</span>
225233
}
226-
@if (['icon', 'both'].includes(navigation())) {
234+
@if (['icon', 'both'].includes(navigationMode())) {
227235
@if (lastIcon()) {
228236
<ng-container [ngTemplateOutlet]="lastIcon()!" />
229237
} @else {
@@ -235,7 +243,7 @@ export const paginationDefaultValueProvider = makeEnvironmentProviders([
235243
</flowbite-button>
236244
}
237245
238-
@if (firstLast()) {
246+
@if (hasFirstLast()) {
239247
<flowbite-button
240248
type="button"
241249
data-active="false"
@@ -244,10 +252,10 @@ export const paginationDefaultValueProvider = makeEnvironmentProviders([
244252
[fill]="buttonProperties().fill!"
245253
[size]="size()"
246254
[customStyle]="buttonProperties().customStyle!">
247-
@if (['text', 'both'].includes(navigation())) {
255+
@if (['text', 'both'].includes(navigationMode())) {
248256
<span>Last</span>
249257
}
250-
@if (['icon', 'both'].includes(navigation())) {
258+
@if (['icon', 'both'].includes(navigationMode())) {
251259
@if (lastIcon()) {
252260
<ng-container [ngTemplateOutlet]="lastIcon()!" />
253261
} @else {
@@ -277,17 +285,17 @@ export class PaginationComponent extends BaseComponent<PaginationClass> {
277285
public readonly domSanitizer = inject(DomSanitizer);
278286

279287
/**
280-
* Value of the current page
288+
* Value of the total items
281289
*
282290
* @required
283291
*/
284-
public readonly currentPage = model.required<number>();
292+
public readonly totalItems = model.required<number>();
285293
/**
286-
* Value of the total items
294+
* Value of the current page
287295
*
288-
* @required
296+
* @default 1
289297
*/
290-
public readonly totalItems = model.required<number>();
298+
public readonly currentPage = model(inject(FLOWBITE_PAGINATION_CURRENT_PAGE_DEFAULT_VALUE));
291299
/**
292300
* Value of how many tabs are displayed
293301
*
@@ -305,19 +313,19 @@ export class PaginationComponent extends BaseComponent<PaginationClass> {
305313
*
306314
* @default true
307315
*/
308-
public readonly prevNext = model(inject(FLOWBITE_PAGINATION_PREVNEXT_DEFAULT_VALUE));
316+
public readonly hasPrevNext = model(inject(FLOWBITE_PAGINATION_HAS_PREV_NEXT_DEFAULT_VALUE));
309317
/**
310318
* Whether to show or hide first and last buttons
311319
*
312320
* @default true
313321
*/
314-
public readonly firstLast = model(inject(FLOWBITE_PAGINATION_FIRSTLAST_DEFAULT_VALUE));
322+
public readonly hasFirstLast = model(inject(FLOWBITE_PAGINATION_HAS_FIRST_LAST_DEFAULT_VALUE));
315323
/**
316324
* Value of the navigation button's type
317325
*
318326
* @default icon
319327
*/
320-
public readonly navigation = model(inject(FLOWBITE_PAGINATION_NAVIGATION_DEFAULT_VALUE));
328+
public readonly navigationMode = model(inject(FLOWBITE_PAGINATION_NAVIGATION_MODE_DEFAULT_VALUE));
321329
/**
322330
* Value of the component's size
323331
*
@@ -474,43 +482,41 @@ export class PaginationComponent extends BaseComponent<PaginationClass> {
474482
//#endregion
475483

476484
/**
477-
* Sets the value of the `currentPage`
485+
* Sets the value of the `currentPage` if it's between 1 and `maxPages`
478486
* @param page number of the active page
479487
*/
480-
public changePage(page: number) {
481-
if (this.visibleCurrentPage() === page) return;
488+
public goToPage(page: number) {
489+
if (page < 1 || page > this.maxPages()) return;
482490
this.currentPage.set(page);
483491
}
484492

485493
/**
486-
* Decreases the value of `currentPage` if it's bigger than 1
494+
* Decreases the value of `currentPage` if it's more than 1
487495
*/
488496
public goToPreviousPage() {
489-
if (this.visibleCurrentPage() === 1) return;
497+
if (this.visibleCurrentPage() <= 1) return;
490498
this.currentPage.update((value) => value - 1);
491499
}
492500

493501
/**
494-
* Increases the value of `currentPage` if it's smaller than `maxPages`
502+
* Increases the value of `currentPage` if it's less than `maxPages`
495503
*/
496504
public goToNextPage() {
497-
if (this.visibleCurrentPage() === this.maxPages()) return;
505+
if (this.visibleCurrentPage() >= this.maxPages()) return;
498506
this.currentPage.update((value) => value + 1);
499507
}
500508

501509
/**
502510
* Sets the value of `currentPage` to 1
503511
*/
504512
public goToFirstPage() {
505-
if (this.currentPage() === 1) return;
506513
this.currentPage.set(1);
507514
}
508515

509516
/**
510517
* Sets the value of `currentPage` equal to `maxPages`
511518
*/
512519
public goToLastPage() {
513-
if (this.currentPage() === this.maxPages()) return;
514520
this.currentPage.set(this.maxPages());
515521
}
516522
}

0 commit comments

Comments
 (0)