Skip to content
Snippets Groups Projects

CE-1942: convert HostTable to common table

Merged Christina Jenks requested to merge CE-1942-replace-custom-tables-with-common into develop
Files
2
+ 57
0
import { useCallback, useMemo, useState } from "react";
const isZeroOrPositiveInt = (val) => {
return Number.isInteger(val) && val >= 0;
};
/**
* Manages pagination state e.g. outside a table or url.
*
* Provides getting/setting for a single pagination object, but internally
* manages changes for each attribute independently so that e.g. if there are
* no changes then the object is unchanged.
*/
export const usePagination = ({
initPage,
initLimit,
initTotalCount,
rowsPerPageOptions
}) => {
const [page, setPage] = useState(initPage ?? 0);
const [_rowsPerPageOptions] = useState(rowsPerPageOptions ?? [20, 50]);
const [limit, setLimit] = useState(initLimit ?? _rowsPerPageOptions[0]);
const [totalCount, setTotalCount] = useState(initTotalCount ?? 0);
/**
* Returns current set of pagination values
*/
const pagination = useMemo(() => {
const val = {
page,
limit,
rows: limit,
totalCount,
totalRecords: totalCount,
rowsPerPageOptions: _rowsPerPageOptions
};
return val;
}, [page, limit, totalCount, _rowsPerPageOptions]);
/**
* Sets pagination. Ignores negative or nonzero values.
*/
const setPagination = useCallback(
({ page, limit, totalCount, first, rows, totalRecords }) => {
isZeroOrPositiveInt(page) && setPage(page);
isZeroOrPositiveInt(limit || rows) && setLimit(limit || rows);
isZeroOrPositiveInt(totalCount || totalRecords) &&
setTotalCount(totalCount || totalRecords);
},
[]
);
return {
pagination,
setPagination
};
};
Loading