-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add paginator to collection page (#526)
* feat: created simple paginator for collection root component WIP * chore: added extra test objects * fix: adjusted styling of paginator WIP * chore: move paginator to shared components package * chore: import new paginator component in manage WIP * feat: add paginator to presentation * test: coverage
- Loading branch information
Showing
17 changed files
with
1,125 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
packages/solid-crs-components/lib/paginator/paginator.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* eslint-disable @typescript-eslint/dot-notation */ | ||
import { PaginatorComponent } from './paginator.component'; | ||
|
||
describe('PaginatorComponent', () => { | ||
|
||
let component: PaginatorComponent; | ||
|
||
beforeEach(() => { | ||
|
||
component = window.document.createElement('nde-paginator') as PaginatorComponent; | ||
component.pageIndex = 0; | ||
component.objectsPerPage = 18; | ||
component.objectsAmount = 40; | ||
|
||
component.translator = { | ||
translate: () => 'page 1 of 10', | ||
} as any; | ||
|
||
}); | ||
|
||
afterEach(() => { | ||
|
||
document.getElementsByTagName('html')[0].innerHTML = ''; | ||
|
||
}); | ||
|
||
it('should be correctly instantiated', async () => { | ||
|
||
window.document.body.appendChild(component); | ||
await component.updateComplete; | ||
expect(component).toBeTruthy(); | ||
|
||
}); | ||
|
||
describe('HTML', () => { | ||
|
||
beforeEach(async () => { | ||
|
||
window.document.body.appendChild(component); | ||
await component.updateComplete; | ||
|
||
}); | ||
|
||
it('should disable previous button when on first page', async () => { | ||
|
||
component.pageIndex = 0; | ||
await component.updateComplete; | ||
expect(component.shadowRoot.querySelector<HTMLButtonElement>('button.previous').disabled).toBeTruthy(); | ||
|
||
}); | ||
|
||
it('should disable next button when on last page', async () => { | ||
|
||
// 40 objects, 18 per page, on page 3 (index 2) | ||
component.pageIndex = 2; | ||
await component.updateComplete; | ||
expect(component.shadowRoot.querySelector<HTMLButtonElement>('button.next').disabled).toBeTruthy(); | ||
|
||
}); | ||
|
||
}); | ||
|
||
describe('onNext', () => { | ||
|
||
it('should dispatch next CustomEvent', async (done) => { | ||
|
||
component.addEventListener('next', () => done()); | ||
component['onNext'](); | ||
|
||
}); | ||
|
||
}); | ||
|
||
describe('onPrevious', () => { | ||
|
||
it('should dispatch previous CustomEvent', async (done) => { | ||
|
||
component.addEventListener('previous', () => done()); | ||
component['onPrevious'](); | ||
|
||
}); | ||
|
||
}); | ||
|
||
}); |
132 changes: 132 additions & 0 deletions
132
packages/solid-crs-components/lib/paginator/paginator.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import { css, CSSResult, html, property, TemplateResult, unsafeCSS } from 'lit-element'; | ||
import { RxLitElement } from 'rx-lit'; | ||
import { Dropdown, Theme } from '@netwerk-digitaal-erfgoed/solid-crs-theme'; | ||
import { unsafeSVG } from 'lit-html/directives/unsafe-svg'; | ||
import { Translator } from '@netwerk-digitaal-erfgoed/solid-crs-core'; | ||
|
||
/** | ||
* A component for paginator controls. | ||
*/ | ||
export class PaginatorComponent extends RxLitElement { | ||
|
||
/** | ||
* The index of the page curently being viewed. | ||
*/ | ||
@property({ type: Object }) | ||
public translator: Translator; | ||
|
||
/** | ||
* The index of the page curently being viewed. | ||
*/ | ||
@property({ type: Number }) | ||
public pageIndex: number; | ||
|
||
/** | ||
* The amount of objects to show per page. | ||
*/ | ||
@property({ type: Number }) | ||
public objectsPerPage: number; | ||
|
||
/** | ||
* The total amount of objects. | ||
*/ | ||
@property({ type: Number }) | ||
public objectsAmount: number; | ||
|
||
/** | ||
* Emits a custom next event. | ||
*/ | ||
private onNext(): void { | ||
|
||
this.dispatchEvent(new CustomEvent('next')); | ||
|
||
} | ||
|
||
/** | ||
* Emits a custom previous event. | ||
*/ | ||
private onPrevious(): void { | ||
|
||
this.dispatchEvent(new CustomEvent('previous')); | ||
|
||
} | ||
|
||
/** | ||
* Renders the component as HTML. | ||
* | ||
* @returns The rendered HTML of the component. | ||
*/ | ||
render(): TemplateResult { | ||
|
||
return html` | ||
<!-- e.g. page 1 of 10 --> | ||
<p>${this.translator.translate('common.paginator.page-counter') | ||
.replace('{CURRENT}', (this.pageIndex+1).toString()) | ||
.replace('{TOTAL}', Math.ceil(this.objectsAmount / this.objectsPerPage).toString())} | ||
</p> | ||
<!-- previous button --> | ||
<button | ||
.disabled="${this.pageIndex < 1}" | ||
class="previous" | ||
@click="${this.onPrevious}"> | ||
${ unsafeSVG(Dropdown) } | ||
</button> | ||
<!-- next button --> | ||
<button | ||
.disabled="${(this.pageIndex + 1) * this.objectsPerPage >= this.objectsAmount}" | ||
class="next" | ||
@click="${this.onNext}"> | ||
${ unsafeSVG(Dropdown) } | ||
</button> | ||
<!-- </div> --> | ||
`; | ||
|
||
} | ||
|
||
/** | ||
* The styles associated with the component. | ||
*/ | ||
static get styles(): CSSResult[] { | ||
|
||
return [ | ||
unsafeCSS(Theme), | ||
css` | ||
:host { | ||
display: flex; | ||
justify-content: flex-end; | ||
gap: var(--gap-normal); | ||
height: var(--gap-normal); | ||
} | ||
:host[hidden] { | ||
display: none; | ||
} | ||
:host > * { | ||
margin: 0; | ||
padding: 0; | ||
display: block; | ||
background-color: unset; | ||
font-size: var(--font-size-small); | ||
line-height: var(--gap-normal); | ||
min-width: var(--gap-normal); | ||
} | ||
:host svg { | ||
fill: var(--colors-primary-dark); | ||
} | ||
button:disabled svg { | ||
fill: var(--colors-foreground-light); | ||
} | ||
.previous svg { | ||
transform: rotate(90deg); | ||
} | ||
.next svg { | ||
transform: rotate(270deg); | ||
} | ||
`, | ||
]; | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,4 +102,4 @@ | |
"preset": "@digita-ai/jest-config", | ||
"maxWorkers": 6 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.