Skip to content

Commit eb2b2c4

Browse files
committed
Fix #2 pagination now works with filtering
1 parent 6134620 commit eb2b2c4

File tree

2 files changed

+23
-25
lines changed

2 files changed

+23
-25
lines changed

src/datagrid.tsx

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,17 @@ const parseChildren = <T,>(children: JSX.Element | JSX.Element[]): DatagridDeriv
4848
export const Datagrid = <T extends DataSourceType>(props: DatagridProps<T>) => {
4949
const { columns, actions, pagination, placeholder } = parseChildren<T>(props.children);
5050

51+
const { getCellClassName, renderCell } = useRendering<T>();
52+
const { filterData, renderFilterControls } = useFiltering<T>(columns);
53+
const { sortData, renderSortingControl } = useSorting<T>(columns);
5154
const { paginateData, renderPaginationControls } = usePagination<T>(
52-
props.dataSource.length,
5355
pagination?.elementsPerPage,
5456
pagination?.directInput || false
5557
);
56-
const { sortData, renderSortingControl } = useSorting<T>(columns);
57-
const { filterData, renderFilterControls } = useFiltering<T>(columns);
58-
const { getCellClassName, renderCell } = useRendering<T>();
5958

60-
let data = filterData(props.dataSource);
61-
data = sortData(data);
62-
data = paginateData(data);
59+
const filteredData = filterData(props.dataSource);
60+
const sortedData = sortData(filteredData);
61+
const paginatedData = paginateData(sortedData);
6362

6463
return (
6564
<div className="datagrid-wrapper">
@@ -77,9 +76,9 @@ export const Datagrid = <T extends DataSourceType>(props: DatagridProps<T>) => {
7776
{columns.filter(c => c.filter).length > 0 && <tr>{columns.map(renderFilterControls)}</tr>}
7877
</thead>
7978
<tbody>
80-
{data.length > 0 ? (
79+
{paginatedData.length > 0 ? (
8180
<>
82-
{data.map((row, i) => (
81+
{paginatedData.map((row, i) => (
8382
<tr key={"row-" + i}>
8483
{columns.map(col => (
8584
<td key={col.field} className={getCellClassName(col)}>
@@ -110,7 +109,7 @@ export const Datagrid = <T extends DataSourceType>(props: DatagridProps<T>) => {
110109
</tr>
111110
))}
112111
{pagination &&
113-
[...Array(pagination.elementsPerPage - data.length)].map((_, i) => (
112+
[...Array(pagination.elementsPerPage - paginatedData.length)].map((_, i) => (
114113
<tr key={"row-fill-" + i}>
115114
{[...Array(columns.length + (actions.length > 0 ? 1 : 0))].map((_, j) => (
116115
<td key={"cell-fill-" + j}>
@@ -132,7 +131,7 @@ export const Datagrid = <T extends DataSourceType>(props: DatagridProps<T>) => {
132131
)}
133132
</tbody>
134133
</HTMLTable>
135-
{pagination && <div className="datagrid-bottombar">{renderPaginationControls()}</div>}
134+
{pagination && <div className="datagrid-bottombar">{renderPaginationControls(filteredData.length)}</div>}
136135
</div>
137136
);
138137
};

src/use-pagination.tsx

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,16 @@ const calculateNumberOfPages = (numberOfElements: number, elementsPerPage: numbe
1010
return ratio > floored ? floored + 1 : floored === 0 ? 1 : floored;
1111
};
1212

13-
export const usePagination = <T extends DataSourceType>(
14-
numberOfElements: number,
15-
elementsPerPage: number | undefined,
16-
directInput: boolean
17-
) => {
13+
export const usePagination = <T extends DataSourceType>(elementsPerPage: number | undefined, directInput: boolean) => {
1814
const [activePage, setActivePage] = useState<number>(1);
19-
const numberOfPages = elementsPerPage !== undefined ? calculateNumberOfPages(numberOfElements, elementsPerPage) : 1;
2015

21-
const directInputPage = (page: number) => {
22-
if (page >= 1 && page <= numberOfPages) {
23-
setActivePage(page);
16+
const renderPaginationControls = (numberOfElements: number) => {
17+
const numberOfPages = elementsPerPage !== undefined ? calculateNumberOfPages(numberOfElements, elementsPerPage) : 1;
18+
19+
if (activePage > numberOfPages) {
20+
setActivePage(1);
2421
}
25-
};
2622

27-
const renderPaginationControls = () => {
2823
return (
2924
<ButtonGroup>
3025
<Button icon={IconNames.DoubleChevronLeft} disabled={activePage === 1} onClick={() => setActivePage(1)} />
@@ -39,7 +34,11 @@ export const usePagination = <T extends DataSourceType>(
3934
min={1}
4035
max={numberOfPages}
4136
value={activePage}
42-
onValueChange={directInputPage}
37+
onValueChange={(page: number) => {
38+
if (page >= 1 && page <= numberOfPages) {
39+
setActivePage(page);
40+
}
41+
}}
4342
style={{ width: numberOfPages.toString().length * 18 + 32 + "px", textAlign: "right" }}
4443
buttonPosition="none"
4544
selectAllOnFocus
@@ -60,7 +59,7 @@ export const usePagination = <T extends DataSourceType>(
6059
<Button text={activePage - 1} onClick={() => setActivePage(activePage - 1)} />
6160
</>
6261
) : (
63-
[...Array(activePage - 1)].map((_, i) => (
62+
[...Array(activePage - 1 >= 0 ? activePage - 1 : 0)].map((_, i) => (
6463
<Button key={"button-" + i} text={i + 1} onClick={() => setActivePage(i + 1)} />
6564
))
6665
)}
@@ -72,7 +71,7 @@ export const usePagination = <T extends DataSourceType>(
7271
<Button text={".."} disabled />
7372
</>
7473
) : (
75-
[...Array(numberOfPages - activePage)].map((_, i) => {
74+
[...Array(numberOfPages - activePage >= 0 ? numberOfPages - activePage : 0)].map((_, i) => {
7675
const index = i + activePage + 1;
7776
return <Button key={"button-" + index} text={index} onClick={() => setActivePage(index)} />;
7877
})

0 commit comments

Comments
 (0)