🔧 Fixes for controlled pagination on react-table
This commit is contained in:
@@ -35,7 +35,7 @@ export const Library = (): ReactElement => {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
pagination: {
|
pagination: {
|
||||||
size: 25,
|
size: 15,
|
||||||
from: 0,
|
from: 0,
|
||||||
},
|
},
|
||||||
type: "all",
|
type: "all",
|
||||||
@@ -44,19 +44,33 @@ export const Library = (): ReactElement => {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
}, []);
|
}, []);
|
||||||
|
const [{ pageIndex, pageSize }, setPagination] =
|
||||||
|
React.useState<PaginationState>({
|
||||||
|
pageIndex: 1,
|
||||||
|
pageSize: 15,
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
const pagination = React.useMemo(
|
||||||
|
() => ({
|
||||||
|
pageIndex,
|
||||||
|
pageSize,
|
||||||
|
}),
|
||||||
|
[pageIndex, pageSize]
|
||||||
|
);
|
||||||
|
|
||||||
const T2Table = (tableOptions): ReactElement => {
|
const T2Table = (tableOptions): ReactElement => {
|
||||||
const { columns, totalPages, rowClickHandler } =
|
const { columns, totalPages, rowClickHandler } =
|
||||||
tableOptions;
|
tableOptions;
|
||||||
const [{ pageIndex, pageSize }, setPagination] =
|
|
||||||
React.useState<PaginationState>({
|
|
||||||
pageIndex: 1,
|
|
||||||
pageSize: 25,
|
|
||||||
})
|
|
||||||
|
|
||||||
// pagination methods
|
// pagination methods
|
||||||
const goToNextPage = useCallback((pageIndex, pageSize) => {
|
const goToNextPage = useCallback(() => {
|
||||||
table.setPageIndex(pageSize);
|
setPagination({
|
||||||
|
pageIndex: pageIndex + 1,
|
||||||
|
pageSize,
|
||||||
|
});
|
||||||
dispatch(
|
dispatch(
|
||||||
searchIssue(
|
searchIssue(
|
||||||
{
|
{
|
||||||
@@ -75,14 +89,16 @@ export const Library = (): ReactElement => {
|
|||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
|
||||||
const goToPreviousPage = useCallback((pageIndex, pageSize) => {
|
const goToPreviousPage = useCallback(() => {
|
||||||
console.log("papsdas", pageIndex);
|
setPagination({
|
||||||
// table.setPageIndex(pageIndex + 1)
|
pageIndex: pageIndex - 1,
|
||||||
|
pageSize,
|
||||||
|
});
|
||||||
let from = 0;
|
let from = 0;
|
||||||
if (pageIndex === 2) {
|
if (pageIndex === 2) {
|
||||||
from = (pageIndex - 1) * pageSize + 2 - 27;
|
from = (pageIndex - 1) * pageSize + 2 - 17;
|
||||||
} else {
|
} else {
|
||||||
from = (pageIndex - 1) * pageSize + 2 - 26;
|
from = (pageIndex - 1) * pageSize + 2 - 16;
|
||||||
}
|
}
|
||||||
dispatch(
|
dispatch(
|
||||||
searchIssue(
|
searchIssue(
|
||||||
@@ -101,14 +117,19 @@ export const Library = (): ReactElement => {
|
|||||||
);
|
);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const table = useReactTable({
|
const table = useReactTable({
|
||||||
data: searchResults.hits.hits,
|
data: searchResults.hits.hits,
|
||||||
columns,
|
columns,
|
||||||
manualPagination: true,
|
manualPagination: true,
|
||||||
getCoreRowModel: getCoreRowModel(),
|
getCoreRowModel: getCoreRowModel(),
|
||||||
|
pageCount: searchResults?.hits?.hits?.length ?? -1,
|
||||||
getFilteredRowModel: getFilteredRowModel(),
|
state: {
|
||||||
pageCount: totalPages,
|
pagination,
|
||||||
|
},
|
||||||
|
onPaginationChange: setPagination,
|
||||||
// getPaginationRowModel: getPaginationRowModel(),
|
// getPaginationRowModel: getPaginationRowModel(),
|
||||||
|
|
||||||
debugTable: true,
|
debugTable: true,
|
||||||
@@ -116,15 +137,15 @@ export const Library = (): ReactElement => {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{/* pagination control */}
|
{/* pagination control */}
|
||||||
<nav className="pagination">
|
<nav className="pagination">
|
||||||
{pageIndex }
|
{JSON.stringify(pagination)}
|
||||||
<div className="field has-addons">
|
<div className="field has-addons">
|
||||||
<p className="control">
|
<p className="control">
|
||||||
<div className="button" onClick={() => goToNextPage(table.getState().pagination.pageIndex + 1, 25)}> Next Page </div>
|
<div className="button" onClick={() => goToNextPage()}> Next Page </div>
|
||||||
</p>
|
</p>
|
||||||
<p className="control">
|
<p className="control">
|
||||||
<div className="button" onClick={() => goToPreviousPage(table.getState().pagination.pageIndex, 25)}> Previous Page</div>
|
<div className="button" onClick={() => goToPreviousPage()}> Previous Page</div>
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
</nav>
|
</nav>
|
||||||
@@ -166,9 +187,6 @@ export const Library = (): ReactElement => {
|
|||||||
})}
|
})}
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
@@ -220,7 +238,7 @@ export const Library = (): ReactElement => {
|
|||||||
const WantedStatus = ({ value }) => {
|
const WantedStatus = ({ value }) => {
|
||||||
return !value ? <span className="tag is-info is-light">Wanted</span> : null;
|
return !value ? <span className="tag is-info is-light">Wanted</span> : null;
|
||||||
};
|
};
|
||||||
const columns = [
|
const columns = React.useMemo(() => [
|
||||||
{
|
{
|
||||||
header: "Comic Metadata",
|
header: "Comic Metadata",
|
||||||
footer: 1,
|
footer: 1,
|
||||||
@@ -275,7 +293,7 @@ export const Library = (): ReactElement => {
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
]
|
], []);
|
||||||
|
|
||||||
// ImportStatus.propTypes = {
|
// ImportStatus.propTypes = {
|
||||||
// value: PropTypes.bool.isRequired,
|
// value: PropTypes.bool.isRequired,
|
||||||
@@ -289,7 +307,7 @@ export const Library = (): ReactElement => {
|
|||||||
<h1 className="title">Library</h1>
|
<h1 className="title">Library</h1>
|
||||||
{/* Search bar */}
|
{/* Search bar */}
|
||||||
<SearchBar />
|
<SearchBar />
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
{!isUndefined(searchResults.hits) && (
|
{!isUndefined(searchResults.hits) && (
|
||||||
<div>
|
<div>
|
||||||
|
|||||||
Reference in New Issue
Block a user