All files / src/components/table Pagination.jsx

100% Statements 39/39
100% Branches 2/2
100% Functions 14/14
100% Lines 33/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143                                              110x   106x 106x   18x 52x         18x   18x 52x 3x               18x             18x 52x         18x   18x 52x 4x               18x             18x   18x 47x               18x             18x 49x 49x 46x 25x   46x   49x     18x 47x 47x             47x                     18x                              
/*
 *      Copyright (C) 2010 - 2025 VREM Software Development <VREMSoftwareDevelopment@gmail.com>
 *
 *      Licensed under the Apache License, Version 2.0 (the "License");
 *      you may not use this file except in compliance with the License.
 *      You may obtain a copy of the License at
 *
 *           http: //www.apache.org/licenses/LICENSE-2.0
 *
 *      Unless required by applicable law or agreed to in writing, software
 *      distributed under the License is distributed on an "AS IS" BASIS,
 *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *      See the License for the specific language governing permissions and
 *      limitations under the License.
 *
 * Bandwidth Monitor
 */
 
import React from 'react';
import PropTypes from 'prop-types';
import { Box, IconButton, TablePagination } from '@mui/material';
import { FirstPage, KeyboardArrowLeft, KeyboardArrowRight, LastPage } from '@mui/icons-material';
 
const lastPage = (count, rowsPerPage) => Math.ceil(count / rowsPerPage) - 1;
 
const isFirstPage = (page) => page === 0;
const isLastPage = (page, count, rowsPerPage) => page >= lastPage(count, rowsPerPage);
 
const FirstPageAction = ({ page, onPageChange }) => (
    <IconButton onClick={(event) => onPageChange(event, 0)} disabled={isFirstPage(page)} aria-label="first page">
        <FirstPage />
    </IconButton>
);
 
FirstPageAction.propTypes = { page: PropTypes.number.isRequired, onPageChange: PropTypes.func.isRequired };
 
const LastPageAction = ({ count, page, rowsPerPage, onPageChange }) => (
    <IconButton
        onClick={(event) => onPageChange(event, Math.max(0, lastPage(count, rowsPerPage)))}
        disabled={isLastPage(page, count, rowsPerPage)}
        aria-label="last page"
    >
        <LastPage />
    </IconButton>
);
 
LastPageAction.propTypes = {
    count: PropTypes.number.isRequired,
    page: PropTypes.number.isRequired,
    rowsPerPage: PropTypes.number.isRequired,
    onPageChange: PropTypes.func.isRequired,
};
 
const PreviousPageAction = ({ page, onPageChange }) => (
    <IconButton onClick={(event) => onPageChange(event, page - 1)} disabled={isFirstPage(page)} aria-label="previous page">
        <KeyboardArrowLeft />
    </IconButton>
);
 
PreviousPageAction.propTypes = { page: PropTypes.number.isRequired, onPageChange: PropTypes.func.isRequired };
 
const NextPageAction = ({ count, page, rowsPerPage, onPageChange }) => (
    <IconButton
        onClick={(event) => onPageChange(event, page + 1)}
        disabled={isLastPage(page, count, rowsPerPage)}
        aria-label="next page"
    >
        <KeyboardArrowRight />
    </IconButton>
);
 
NextPageAction.propTypes = {
    count: PropTypes.number.isRequired,
    page: PropTypes.number.isRequired,
    rowsPerPage: PropTypes.number.isRequired,
    onPageChange: PropTypes.func.isRequired,
};
 
const options = { flexShrink: 0, display: 'flex' };
 
const Actions = ({ count, page, rowsPerPage, onPageChange }) => (
    <Box sx={options}>
        <FirstPageAction page={page} onPageChange={onPageChange} />
        <PreviousPageAction page={page} onPageChange={onPageChange} />
        <NextPageAction page={page} onPageChange={onPageChange} count={count} rowsPerPage={rowsPerPage} />
        <LastPageAction page={page} onPageChange={onPageChange} count={count} rowsPerPage={rowsPerPage} />
    </Box>
);
 
Actions.propTypes = {
    count: PropTypes.number.isRequired,
    page: PropTypes.number.isRequired,
    rowsPerPage: PropTypes.number.isRequired,
    onPageChange: PropTypes.func.isRequired,
};
 
const rowsPerPageOptions = (rowsPerPage, count) => {
    let rowsPerPageOptions = [rowsPerPage];
    if (rowsPerPage !== count) {
        for (let i = rowsPerPage * 2; i < count; i *= 2) {
            rowsPerPageOptions.push(i);
        }
        rowsPerPageOptions.push(count);
    }
    return rowsPerPageOptions;
};
 
const Pagination = (props) => {
    const options = rowsPerPageOptions(Math.min(props.minimum, props.count), props.count);
    const selectProps = {
        inputProps: { 'aria-label': 'rows per page' },
        native: true,
        id: 'select-rows-per-page-id',
        labelId: 'select-label-rows-per-page-id',
    };
 
    return (
        <TablePagination
            {...props}
            labelRowsPerPage=""
            slotProps={{ select: selectProps }}
            rowsPerPageOptions={options}
            ActionsComponent={Actions}
        />
    );
};
 
Pagination.propTypes = { count: PropTypes.number.isRequired, minimum: PropTypes.number.isRequired };
 
export {
    Pagination,
    rowsPerPageOptions,
    FirstPageAction,
    LastPageAction,
    PreviousPageAction,
    NextPageAction,
    lastPage,
    isFirstPage,
    isLastPage,
};
 
export default Pagination;