From 2a4e5606ebbfeeea5d84f19539ea148e2baa3c17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?O=C4=9Fuz=20=C3=96LKE?= <70342517+olkeoguz@users.noreply.github.com> Date: Fri, 11 Nov 2022 13:05:31 +0300 Subject: [PATCH] fix: pagination items per page options (#313) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(pagination): add select options property * docs(pagination): add customization to canvas * style(pagination): change page list on small screens * fix(pagination): make items-per-page-options property only Co-authored-by: Murat Çorlu <127687+muratcorlu@users.noreply.github.com> Co-authored-by: Murat Çorlu <127687+muratcorlu@users.noreply.github.com> --- src/components/pagination/bl-pagination.css | 17 ++- .../pagination/bl-pagination.stories.mdx | 61 ++++++++-- .../pagination/bl-pagination.test.ts | 14 ++- src/components/pagination/bl-pagination.ts | 104 ++++++++++-------- 4 files changed, 134 insertions(+), 62 deletions(-) diff --git a/src/components/pagination/bl-pagination.css b/src/components/pagination/bl-pagination.css index b4ee73b0..5da9674f 100644 --- a/src/components/pagination/bl-pagination.css +++ b/src/components/pagination/bl-pagination.css @@ -1,9 +1,9 @@ -:host { - width: max-content; -} - .pagination { display: flex; + flex-wrap:wrap; + justify-content: center; + max-width:max-content; + gap: var(--bl-size-s); } .pagination * { @@ -35,8 +35,9 @@ .pagination-helpers { display: flex; + flex-wrap: wrap; + justify-content: center; align-items: center; - margin-right: var(--bl-size-s); gap: var(--bl-size-m); } @@ -63,3 +64,9 @@ bl-input { bl-select { width:128px; } + +@media only screen and (max-width: 768px) { + label { + display:none; + } +} diff --git a/src/components/pagination/bl-pagination.stories.mdx b/src/components/pagination/bl-pagination.stories.mdx index 8dc3268d..fee5405f 100644 --- a/src/components/pagination/bl-pagination.stories.mdx +++ b/src/components/pagination/bl-pagination.stories.mdx @@ -17,7 +17,7 @@ import { Meta, Canvas, ArgsTable, Story, Preview, Source } from '@storybook/addo itemsPerPage: { control: { type: 'number', - default:100 + default:10 } }, hasJumper: { @@ -44,10 +44,26 @@ import { Meta, Canvas, ArgsTable, Story, Preview, Source } from '@storybook/addo default:"Show" } }, - optionText: { + itemsPerPageOptions: { control: { - type: 'text', - default:"Items" + type: 'array', + default:[ + { + text: '100 Sonuç', + value: 100, + }, + { + text: '250 Sonuç', + value: 250, + }, + { + text: '500 Sonuç', + value: 500, + }, + { + text: '1000 Sonuç', + value: 1000, + }] } }, }} @@ -62,7 +78,7 @@ export const PaginationTemplate = (args) => html` jumper-label=${ifDefined(args.jumperLabel)} has-select=${ifDefined(args.hasSelect)} select-label=${ifDefined(args.selectLabel)} - option-text=${ifDefined(args.optionText)} + .itemsPerPageOptions=${ifDefined(args.itemsPerPageOptions)} > ` @@ -76,7 +92,7 @@ The Pagination component enables the user to select a specific page from a range Use a simple pagination for navigating to the next or previous page or a selected page. - + {PaginationTemplate.bind({})} @@ -85,7 +101,7 @@ Use a simple pagination for navigating to the next or previous page or a selecte Use the jumper to directly go to a specific page. - + {PaginationTemplate.bind({})} @@ -94,16 +110,41 @@ Use the jumper to directly go to a specific page. Use the select helper to change the number of items per page. - + {PaginationTemplate.bind({})} ## Customization -The labels and the texts of the jumper and select element are fully customizable. +The labels and the texts of the jumper and select element are fully customizable. The default select options can also be changed. - + {PaginationTemplate.bind({})} diff --git a/src/components/pagination/bl-pagination.test.ts b/src/components/pagination/bl-pagination.test.ts index 6334e41d..de88db71 100644 --- a/src/components/pagination/bl-pagination.test.ts +++ b/src/components/pagination/bl-pagination.test.ts @@ -32,12 +32,11 @@ describe('bl-pagination', () => { it('should render with the correct default values', async () => { const el = await fixture(html` `); expect(el?.currentPage).to.equal(1); - expect(el.itemsPerPage).to.equal(100); + expect(el.itemsPerPage).to.equal(10); expect(el.hasJumper).to.equal(false); expect(el.hasSelect).to.equal(false); expect(el.jumperLabel).to.equal('Go To'); expect(el.selectLabel).to.equal('Show'); - expect(el.optionText).to.equal('Items'); }); it('should correctly set the attributes', async () => { @@ -51,7 +50,6 @@ describe('bl-pagination', () => { jumper-label="Git" has-select select-label="Göster" - option-text="Sonuç" > ` @@ -62,7 +60,6 @@ describe('bl-pagination', () => { expect(el.hasSelect).to.equal(true); expect(el.jumperLabel).to.equal('Git'); expect(el.selectLabel).to.equal('Göster'); - expect(el.optionText).to.equal('Sonuç'); }); describe('back and forward arrows', () => { @@ -130,6 +127,15 @@ describe('bl-pagination', () => { expect(el.currentPage).to.equal(4); }); }); + + it('renders only the current page with previous - next arrows on mobile view', async () => { + window.innerWidth = 600; + const el = await fixture( + html`` + ); + expect(el.shadowRoot?.querySelectorAll('bl-button').length).to.eq(3); + expect(el.shadowRoot?.querySelectorAll('.dots').length).to.eq(0); + }); }); describe('jumper and select element', () => { diff --git a/src/components/pagination/bl-pagination.ts b/src/components/pagination/bl-pagination.ts index 59688c32..d7708e08 100644 --- a/src/components/pagination/bl-pagination.ts +++ b/src/components/pagination/bl-pagination.ts @@ -12,25 +12,6 @@ import style from './bl-pagination.css'; * @summary Baklava Pagination component */ -const selectOptions = [ - { - text: '100', - value: 100, - }, - { - text: '250', - value: 250, - }, - { - text: '500', - value: 500, - }, - { - text: '1000', - value: 1000, - }, -]; - @customElement('bl-pagination') export default class BlPagination extends LitElement { static get styles(): CSSResultGroup { @@ -53,7 +34,7 @@ export default class BlPagination extends LitElement { * Sets the number of items per page */ @property({ attribute: 'items-per-page', type: Number, reflect: true }) - itemsPerPage = 100; + itemsPerPage = 10; /** * Adds jumper element if provided as true @@ -80,10 +61,28 @@ export default class BlPagination extends LitElement { selectLabel = 'Show'; /** - * Sets the option texts. + * Sets the items per page options of the select element + * PROPERTY */ - @property({ attribute: 'option-text', type: String }) - optionText = 'Items'; + @property({ type: Array, attribute: false }) + itemsPerPageOptions = [ + { + text: '10 Items', + value: 10, + }, + { + text: '25 Items', + value: 25, + }, + { + text: '50 Items', + value: 50, + }, + { + text: '100 Items', + value: 100, + }, + ]; @state() private pages: Array = []; @@ -92,8 +91,25 @@ export default class BlPagination extends LitElement { */ @event('bl-change') private onChange: EventDispatcher<{ selectedPage: number; prevPage: number }>; + connectedCallback() { + super.connectedCallback(); + + setTimeout(() => { + window?.addEventListener('resize', () => this._paginate()); + }); + } + + disconnectedCallback() { + super.disconnectedCallback(); + window?.removeEventListener('resize', this._paginate); + } + updated(changedProperties: PropertyValues) { - if (changedProperties.has('currentPage') || changedProperties.has('itemsPerPage') || changedProperties.has('totalItems') ) { + if ( + changedProperties.has('currentPage') || + changedProperties.has('itemsPerPage') || + changedProperties.has('totalItems') + ) { this._paginate(); this.onChange({ selectedPage: this.currentPage, @@ -159,7 +175,7 @@ export default class BlPagination extends LitElement { this.currentPage = 1; } - private renderSinglePage(page: number | string) { + private _renderSinglePage(page: number | string) { if (typeof page === 'string') { return html``; } @@ -186,7 +202,9 @@ export default class BlPagination extends LitElement { ?disabled=${this.currentPage === 1} >
    - ${this.pages.map(page => html`${this.renderSinglePage(page)}`)} + ${window.innerWidth < 768 + ? html`${this._renderSinglePage(this.currentPage)}` + : this.pages.map(page => html`${this._renderSinglePage(page)}`)}
- - - ${selectOptions.map(option => { - return html`${option.text} ${this.optionText}`; - })} - - - ` : null; + const selectEl = this.hasSelect + ? html` +
+ + + ${this.itemsPerPageOptions.map(option => { + return html`${option.text}`; + })} + +
+ ` + : null; const jumperEl = this.hasJumper ? html`
@@ -225,9 +245,7 @@ export default class BlPagination extends LitElement { const getHelperElements = () => { if (!this.hasSelect && !this.hasJumper) return; - return html` -
${selectEl} ${jumperEl}
- `; + return html`
${selectEl} ${jumperEl}
`; }; return html` `;