Skip to content

Commit

Permalink
Merge pull request #2995 from mi6/v3-cherry-pick-pagination-first-page
Browse files Browse the repository at this point in the history
V3 cherry pick pagination first page
  • Loading branch information
ad9242 authored Jan 14, 2025
2 parents 1119bd5 + fed2c21 commit d74b2a9
Show file tree
Hide file tree
Showing 11 changed files with 274 additions and 66 deletions.
26 changes: 24 additions & 2 deletions packages/canary-docs/docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,7 @@
"reflectToAttr": false,
"docs": "Sets the props for the built-in pagination bar. If the `pagination-bar` slot is used then this prop is ignored.",
"docsTags": [],
"default": "{\n itemsPerPageOptions: [\n { label: \"10\", value: \"10\" },\n { label: \"25\", value: \"25\" },\n { label: \"50\", value: \"50\" },\n ],\n rangeLabelType: \"page\",\n type: \"simple\",\n showItemsPerPageControl: true,\n showGoToPageControl: true,\n alignment: \"right\",\n itemLabel: \"Item\",\n pageLabel: \"Page\",\n hideRangeLabel: false,\n hideAllFromItemsPerPage: false,\n monochrome: false,\n }",
"default": "{\n itemsPerPageOptions: [\n { label: \"10\", value: \"10\" },\n { label: \"25\", value: \"25\" },\n { label: \"50\", value: \"50\" },\n ],\n rangeLabelType: \"page\",\n type: \"simple\",\n showItemsPerPageControl: true,\n showGoToPageControl: true,\n alignment: \"right\",\n itemLabel: \"Item\",\n pageLabel: \"Page\",\n hideRangeLabel: false,\n hideAllFromItemsPerPage: false,\n monochrome: false,\n setToFirstPageOnPaginationChange: false,\n }",
"values": [
{
"type": "IcPaginationBarOptions"
Expand Down Expand Up @@ -3341,6 +3341,28 @@
"optional": true,
"required": false
},
{
"name": "setToFirstPageOnPaginationChange",
"type": "boolean",
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
},
"mutable": false,
"attr": "set-to-first-page-on-pagination-change",
"reflectToAttr": false,
"docs": "If `true`, the pagination bar is set to the first page when the 'items per page' changes",
"docsTags": [],
"default": "false",
"values": [
{
"type": "boolean"
}
],
"optional": true,
"required": false
},
{
"name": "showGoToPageControl",
"type": "boolean",
Expand Down Expand Up @@ -4239,7 +4261,7 @@
"path": "src/components/ic-data-table/ic-data-table.types.tsx"
},
"src/utils/types.ts::IcPaginationBarOptions": {
"declaration": "export interface IcPaginationBarOptions {\n itemsPerPageOptions?: { label: string; value: string }[];\n rangeLabelType?: IcPaginationLabelTypes;\n type?: IcPaginationTypes;\n showItemsPerPageControl?: boolean;\n showGoToPageControl?: boolean;\n alignment?: IcPaginationAlignmentOptions;\n itemLabel?: string;\n pageLabel?: string;\n hideRangeLabel?: boolean;\n hideAllFromItemsPerPage?: boolean;\n theme?: IcThemeMode;\n monochrome?: boolean;\n}",
"declaration": "export interface IcPaginationBarOptions {\n itemsPerPageOptions?: { label: string; value: string }[];\n rangeLabelType?: IcPaginationLabelTypes;\n type?: IcPaginationTypes;\n showItemsPerPageControl?: boolean;\n showGoToPageControl?: boolean;\n alignment?: IcPaginationAlignmentOptions;\n itemLabel?: string;\n pageLabel?: string;\n hideRangeLabel?: boolean;\n hideAllFromItemsPerPage?: boolean;\n theme?: IcThemeMode;\n monochrome?: boolean;\n setToFirstPageOnPaginationChange?: boolean;\n}",
"docstring": "",
"path": "src/utils/types.ts"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,97 @@ describe("IcPaginationBar end-to-end tests", () => {
.should(HAVE_LENGTH, 1);
});

it("should reset to the first page when the setToFirstPageOnPaginationChange prop is set and the items per page is changed", () => {
mount(
<PaginationBarItemsPerPage
rangeLabelType="data"
setToFirstPageOnPaginationChange
/>
);

cy.checkHydrated(PAGINATION_BAR);

cy.findShadowEl(PAGINATION_BAR, "ic-pagination")
.shadow()
.find("#next-page-button")
.click();

cy.findShadowEl(
PAGINATION_BAR,
"ic-typography.item-pagination-label"
).should(HAVE_TEXT, "11 - 20 of 100 items");

cy.findShadowEl(PAGINATION_BAR, "ic-pagination")
.shadow()
.find("ic-pagination-item")
.shadow()
.find("ic-typography")
.should(HAVE_TEXT, "Page 2");

cy.findShadowEl(PAGINATION_BAR, "ic-select").click();

cy.findShadowEl(PAGINATION_BAR, "ic-select")
.shadow()
.find("li[role='option']")
.eq(1)
.click();

cy.findShadowEl(
PAGINATION_BAR,
"ic-typography.item-pagination-label"
).should(HAVE_TEXT, "1 - 20 of 100 items");

cy.findShadowEl(PAGINATION_BAR, "ic-pagination")
.shadow()
.find("ic-pagination-item")
.shadow()
.find("ic-typography")
.should(HAVE_TEXT, "Page 1");
});

it("should remain on the current page when the setToFirstPageOnPaginationChange prop is unset and the items per page is changed", () => {
mount(<PaginationBarItemsPerPage rangeLabelType="data" />);

cy.checkHydrated(PAGINATION_BAR);

cy.findShadowEl(PAGINATION_BAR, "ic-pagination")
.shadow()
.find("#next-page-button")
.click();

cy.findShadowEl(
PAGINATION_BAR,
"ic-typography.item-pagination-label"
).should(HAVE_TEXT, "11 - 20 of 100 items");

cy.findShadowEl(PAGINATION_BAR, "ic-pagination")
.shadow()
.find("ic-pagination-item")
.shadow()
.find("ic-typography")
.should(HAVE_TEXT, "Page 2");

cy.findShadowEl(PAGINATION_BAR, "ic-select").click();

cy.findShadowEl(PAGINATION_BAR, "ic-select")
.shadow()
.find("li[role='option']")
.eq(1)
.click();

cy.findShadowEl(
PAGINATION_BAR,
"ic-typography.item-pagination-label"
).should(HAVE_TEXT, "21 - 40 of 100 items");

cy.findShadowEl(PAGINATION_BAR, "ic-pagination")
.shadow()
.find("ic-pagination-item")
.shadow()
.find("ic-typography")
.should(HAVE_TEXT, "Page 2");
});

it('should render the number of items instead of the page number when rangeLabelType is "data"', () => {
mount(<PaginationBarItemsPerPage rangeLabelType="data" />);

Expand Down
20 changes: 3 additions & 17 deletions packages/canary-react/src/stories/ic-data-table.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -1877,22 +1877,6 @@ const DataTable = () => {
export default DataTable;
```

export const defaultPaginationBarArgs = {
itemsPerPageOptions: [
{ label: "5", value: "5" },
{ label: "10", value: "10" },
{ label: "15", value: "15" },
],
rangeLabelType: "page",
type: "simple",
showItemsPerPageControl: true,
showGoToPageControl: true,
alignment: "right",
itemLabel: "Item",
pageLabel: "Page",
hideRangeLabel: false
};

### Slotted pagination bar

There may be scenarios when using `IcDataTable` when pagination and data fetching is done using a backend API.
Expand Down Expand Up @@ -2219,6 +2203,7 @@ export const defaultArgs = {
],
paginationRangeLabelType: "page",
paginationType: "simple",
paginationSetToFirstPageOnPaginationChange: false,
paginationShowItemsPerPageControl: true,
paginationShowGoToPageControl: true,
paginationAlignment: "right",
Expand Down Expand Up @@ -2329,8 +2314,9 @@ export const defaultArgs = {
itemsPerPageOptions: args.paginationItemsPerPageOptions,
rangeLabelType: args.paginationRangeLabelType,
type: args.paginationType,
showItemsPerPageControl: args.paginationShowItemsPerPageControl,
setToFirstPageOnPaginationChange: args.paginationSetToFirstPageOnPaginationChange,
showGoToPageControl: args.paginationShowGoToPageControl,
showItemsPerPageControl: args.paginationShowItemsPerPageControl,
alignment: args.paginationAlignment,
pageLabel: args.paginationPageLabel,
itemLabel: args.paginationItemLabel,
Expand Down
43 changes: 41 additions & 2 deletions packages/canary-react/src/stories/ic-pagination-bar.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,43 @@ const PaginationBar = () => (
export default PaginationBar;
```

### Set to first page when items per page changes

If you have a predefined list of items you want in the itemsPerPage select, the 'All' option can be hidden by setting `hide-all-from-items-per-page` to `true`.

<Canvas withSource="none">
<Story name="Set to first page when items per page changes">
<div style={{
height: '150px'
}}>
<IcPaginationBar
totalItems="100"
showItemsPerPageControl
showGoToPageControl
setToFirstPageOnPaginationChange
/>
</div>
</Story>
</Canvas>

#### Set to first page when items per page changes code example

```jsx
import * as React from "react";
import { IcPaginationBar } from '@ukic/canary-react';

const PaginationBar = () => (
<IcPaginationBar
totalItems="100"
showItemsPerPageControl
showGoToPageControl
setToFirstPageOnPaginationChange
/>
);

export default PaginationBar;
```

### Playground

Go to the <IcLink href="/?path=/story/react-components-pagination-bar--playground">separate page for the playground example</IcLink> to view the prop controls.
Expand All @@ -431,8 +468,9 @@ export const defaultArgs = {
monochrome: false,
pageLabel: "Page",
rangeLabelType: "page",
showItemsPerPageControl: false,
showGoToPageControl: false,
setToFirstPageOnPaginationChange: false,
showItemsPerPageControl: true,
showGoToPageControl: true,
totalItems: 100,
type: "simple",
theme: "inherit",
Expand Down Expand Up @@ -476,6 +514,7 @@ export const defaultArgs = {
itemsPerPageOptions={args.itemsPerPageOptions}
pageLabel={args.pageLabel}
rangeLabelType={args.rangeLabelType}
setToFirstPageOnPaginationChange={args.setToFirstPageOnPaginationChange}
showItemsPerPageControl={args.showItemsPerPageControl}
showGoToPageControl={args.showGoToPageControl}
totalItems={args.totalItems}
Expand Down
8 changes: 8 additions & 0 deletions packages/canary-web-components/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,10 @@ export namespace Components {
* Whether total number of items and current item range or total number of pages and current page is displayed.
*/
"rangeLabelType"?: IcPaginationLabelTypes;
/**
* If `true`, the pagination bar is set to the first page when the 'items per page' changes
*/
"setToFirstPageOnPaginationChange"?: boolean;
/**
* If `true`, the 'go to page' control should be displayed.
*/
Expand Down Expand Up @@ -1226,6 +1230,10 @@ declare namespace LocalJSX {
* Whether total number of items and current item range or total number of pages and current page is displayed.
*/
"rangeLabelType"?: IcPaginationLabelTypes;
/**
* If `true`, the pagination bar is set to the first page when the 'items per page' changes
*/
"setToFirstPageOnPaginationChange"?: boolean;
/**
* If `true`, the 'go to page' control should be displayed.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ export class DataTable {
hideRangeLabel: false,
hideAllFromItemsPerPage: false,
monochrome: false,
setToFirstPageOnPaginationChange: false,
};

/**
Expand Down Expand Up @@ -2055,6 +2056,9 @@ export class DataTable {
hideAllFromItemsPerPage={
paginationBarOptions.hideAllFromItemsPerPage
}
setToFirstPageOnPaginationChange={
paginationBarOptions.setToFirstPageOnPaginationChange
}
></ic-pagination-bar>
)}
</div>
Expand Down
Loading

0 comments on commit d74b2a9

Please sign in to comment.