Skip to content

Commit 74c93cf

Browse files
authored
Merge pull request #1830 from dxc-technology/Mil4n0r/hide_paginator
Added option to hide the paginator inside a `ResultsetTable`
2 parents 63d5c38 + 9f86175 commit 74c93cf

File tree

4 files changed

+83
-33
lines changed

4 files changed

+83
-33
lines changed

lib/src/resultset-table/ResultsetTable.stories.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,10 @@ export const Chromatic = () => (
260260
<Title title="Scroll resultset table" theme="light" level={4} />
261261
<DxcResultsetTable columns={longColumns} rows={longRows} />
262262
</ExampleContainer>
263+
<ExampleContainer>
264+
<Title title="Without paginator" theme="light" level={4} />
265+
<DxcResultsetTable columns={columns} rows={rows} hidePaginator />
266+
</ExampleContainer>
263267
<ExampleContainer>
264268
<SmallContainer>
265269
<Title title="Small container and text overflow" theme="light" level={4} />

lib/src/resultset-table/ResultsetTable.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,12 @@ describe("Resultset table component tests", () => {
310310
expect(getAllByRole("row").length - 1).toEqual(1);
311311
});
312312

313+
test("Resultset table may not use the paginator", () => {
314+
const { getAllByRole } = render(<DxcResultsetTable columns={columns} rows={rows} hidePaginator />);
315+
const nextButton = getAllByRole("button")[3];
316+
expect(nextButton).not.toBeTruthy();
317+
});
318+
313319
test("Resultset table with ActionsCell", () => {
314320
const onSelectOption = jest.fn();
315321
const onClick = jest.fn();

lib/src/resultset-table/ResultsetTable.tsx

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import { spaces } from "../common/variables";
44
import DxcPaginator from "../paginator/Paginator";
55
import DxcTable, { DxcActionsCell } from "../table/Table";
66
import useTheme from "../useTheme";
7-
import ResultsetTablePropsType, { Column } from "./types";
7+
import ResultsetTablePropsType, { Column, Row } from "./types";
88
import icons from "./Icons";
99
import { getMargin } from "../common/utils";
1010
import CoreTokens from "../common/coreTokens";
1111

12-
const normalizeSortValue = (sortValue) => (typeof sortValue === "string" ? sortValue.toUpperCase() : sortValue);
12+
const normalizeSortValue = (sortValue: string | React.ReactNode) =>
13+
typeof sortValue === "string" ? sortValue.toUpperCase() : sortValue;
1314

14-
const sortArray = (index, order, resultset) =>
15+
const sortArray = (index: number, order: "ascending" | "descending", resultset: Row[][]) =>
1516
resultset.slice().sort((element1, element2) => {
1617
const sortValueA = normalizeSortValue(element1[index].sortValue || element1[index].displayValue);
1718
const sortValueB = normalizeSortValue(element2[index].sortValue || element2[index].displayValue);
@@ -28,15 +29,20 @@ const sortArray = (index, order, resultset) =>
2829
return order === "descending" ? comparison * -1 : comparison;
2930
});
3031

31-
const getMinItemsPerPageIndex = (currentPageInternal, itemsPerPage, page) =>
32+
const getMinItemsPerPageIndex = (currentPageInternal: number, itemsPerPage: number, page: number) =>
3233
currentPageInternal === 1 ? 0 : itemsPerPage * (page - 1);
3334

34-
const getMaxItemsPerPageIndex = (minItemsPerPageIndex, itemsPerPage, resultset, page) =>
35-
minItemsPerPageIndex + itemsPerPage > resultset.length ? resultset.length : itemsPerPage * page - 1;
35+
const getMaxItemsPerPageIndex = (
36+
minItemsPerPageIndex: number,
37+
itemsPerPage: number,
38+
resultset: Row[][],
39+
page: number
40+
) => (minItemsPerPageIndex + itemsPerPage > resultset.length ? resultset.length : itemsPerPage * page - 1);
3641

3742
const DxcResultsetTable = ({
3843
columns,
3944
rows,
45+
hidePaginator = false,
4046
showGoToPage = true,
4147
itemsPerPage = 5,
4248
itemsPerPageOptions,
@@ -81,7 +87,9 @@ const DxcResultsetTable = ({
8187
};
8288

8389
useEffect(() => {
84-
rows.length > 0 ? changePage(1) : changePage(0);
90+
if (!hidePaginator) {
91+
rows.length > 0 ? changePage(1) : changePage(0);
92+
}
8593
}, [rows]);
8694

8795
return (
@@ -121,31 +129,33 @@ const DxcResultsetTable = ({
121129
</tr>
122130
</thead>
123131
<tbody>
124-
{filteredResultset.map((cells, index) => (
125-
<tr key={`resultSetTableCell_${page}_${index}`}>
126-
{cells.map((cellContent, index) => (
127-
<td key={`resultSetTableCellContent_${index}`}>{cellContent.displayValue}</td>
132+
{filteredResultset.map((cells, rowIndex) => (
133+
<tr key={`resultSetTableCell_${page}_${rowIndex}`}>
134+
{cells.map((cellContent, cellIndex) => (
135+
<td key={`resultSetTableCellContent_${cellIndex}`}>{cellContent.displayValue}</td>
128136
))}
129137
</tr>
130138
))}
131139
</tbody>
132140
</DxcTable>
133-
<DxcPaginator
134-
totalItems={rows.length}
135-
itemsPerPage={itemsPerPage}
136-
itemsPerPageOptions={itemsPerPageOptions}
137-
itemsPerPageFunction={itemsPerPageFunction}
138-
currentPage={page}
139-
showGoToPage={showGoToPage}
140-
onPageChange={goToPage}
141-
tabIndex={tabIndex}
142-
/>
141+
{!hidePaginator && (
142+
<DxcPaginator
143+
totalItems={rows.length}
144+
itemsPerPage={itemsPerPage}
145+
itemsPerPageOptions={itemsPerPageOptions}
146+
itemsPerPageFunction={itemsPerPageFunction}
147+
currentPage={page}
148+
showGoToPage={showGoToPage}
149+
onPageChange={goToPage}
150+
tabIndex={tabIndex}
151+
/>
152+
)}
143153
</DxcResultsetTableContainer>
144154
</ThemeProvider>
145155
);
146156
};
147157

148-
const calculateWidth = (margin) => `calc(100% - ${getMargin(margin, "left")} - ${getMargin(margin, "right")})`;
158+
const calculateWidth = (margin: string | object) => `calc(100% - ${getMargin(margin, "left")} - ${getMargin(margin, "right")})`;
149159

150160
const DxcResultsetTableContainer = styled.div<{ margin: ResultsetTablePropsType["margin"] }>`
151161
width: ${(props) => calculateWidth(props.margin)};

lib/src/resultset-table/types.ts

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export type Column = {
1717
isSortable?: boolean;
1818
};
1919

20-
type Row = {
20+
export type Row = {
2121
/**
2222
* Value to be displayed in the cell.
2323
*/
@@ -29,7 +29,7 @@ type Row = {
2929
sortValue?: string;
3030
};
3131

32-
type Props = {
32+
type CommonProps = {
3333
/**
3434
* An array of objects representing the columns of the table.
3535
*/
@@ -39,6 +39,25 @@ type Props = {
3939
* as many objects as columns in the table.
4040
*/
4141
rows: Row[][];
42+
/**
43+
* Size of the margin to be applied to the component. You can pass an object with 'top',
44+
* 'bottom', 'left' and 'right' properties in order to specify different margin sizes.
45+
*/
46+
margin?: Space | Margin;
47+
/**
48+
* Value of the tabindex attribute applied to the sortable icon.
49+
*/
50+
tabIndex?: number;
51+
/**
52+
* Determines the visual style and layout
53+
* - "default": The default mode with big spacing
54+
* - "reduced": A reduced mode with minimal spacing for dense tables
55+
*/
56+
mode?: "default" | "reduced";
57+
};
58+
59+
type PaginatedProps = CommonProps & {
60+
hidePaginator?: false;
4261
/**
4362
* If true, a select component for navigation between pages will be displayed.
4463
*/
@@ -56,21 +75,32 @@ type Props = {
5675
* option. The value selected will be passed as a parameter.
5776
*/
5877
itemsPerPageFunction?: (value: number) => void;
78+
};
79+
80+
type NonPaginatedProps = CommonProps & {
5981
/**
60-
* Size of the margin to be applied to the component. You can pass an object with 'top',
61-
* 'bottom', 'left' and 'right' properties in order to specify different margin sizes.
82+
* If true, paginator will not be displayed.
6283
*/
63-
margin?: Space | Margin;
84+
hidePaginator: true;
6485
/**
65-
* Value of the tabindex attribute applied to the sortable icon.
86+
* If true, a select component for navigation between pages will be displayed.
6687
*/
67-
tabIndex?: number;
88+
showGoToPage?: never;
6889
/**
69-
* Determines the visual style and layout
70-
* - "default": The default mode with big spacing
71-
* - "reduced": A reduced mode with minimal spacing for dense tables
90+
* Number of items per page.
7291
*/
73-
mode?: "default" | "reduced";
92+
itemsPerPage?: never;
93+
/**
94+
* An array of numbers representing the items per page options.
95+
*/
96+
itemsPerPageOptions?: never;
97+
/**
98+
* This function will be called when the user selects an item per page
99+
* option. The value selected will be passed as a parameter.
100+
*/
101+
itemsPerPageFunction?: never;
74102
};
75103

104+
type Props = PaginatedProps | NonPaginatedProps;
105+
76106
export default Props;

0 commit comments

Comments
 (0)