# All files / elements/Table/Paginator index.js

92.31% Statements 24/26
0% Branches 0/2
68.75% Functions 11/16
91.3% Lines 21/23

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
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168

    103x

    5x
    74x
    74x

    74x

    74x

    103x

    74x

    74x
    31x

    31x
    31x

    74x

    2x
    2x

    2x

    2x

    2x

    2x

    5x

    5x

     import React, { useEffect, useState } from 'react';
    import PropTypes from 'prop-types';
    import { useForm, Controller } from 'react-hook-form';
    import { Button } from 'src/elements/Button';
    import { Form, SelectField, TextField } from 'src/elements/forms';
    import { Box, Flex } from 'src/elements/grid';
    import { Hideable } from 'src/elements/utils';
    import { Text } from 'src/elements/typography';
    import {
      faAngleDoubleLeft,
      faAngleDoubleRight,
      faAngleLeft,
      faAngleRight,
    } from '@fortawesome/free-solid-svg-icons';
    import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
    import { defaultPageOptions } from '../utils';

    const getCaptionForItemsPerPage = pageSize => `Show ${pageSize}`;

    const getCaptionForPage = pageVm => {
      const { pageCount, pageIndex } = pageVm;
      return `Page ${pageIndex + 1} of ${pageCount}`;
    };

    export function Paginator(props) {
      const {
        canPreviousPage,
        canNextPage,
        gotoPage,
        menuPlacement,
        nextPage,
        pageCount,
        pageIndex,
        pageOptions,
        pageSize,
        previousPage,
        setPageSize,
        showSkipButtons,
      } = props;

      const [pageVm, setPageVm] = useState({
        pageCount, pageIndex, pageOptions, pageSize,
      });

      const pageSelectOptions = React.useMemo(() => pageOptions.map(valuePageSize => ({ label: getCaptionForItemsPerPage(valuePageSize), value: valuePageSize })), [pageOptions]);
      const {
        control,
        setValue,
      } = useForm({
        defaultValues: {
          jumpToPage: pageVm.pageIndex + 1,
          selectedPageOption: pageSelectOptions[pageOptions.indexOf(pageSize)],
        },
      });
      useEffect(() => {
        setPageVm({
          pageCount,
          pageIndex,
          pageOptions,
          pageSize,
        });
        setValue('jumpToPage', pageIndex + 1);
        setValue('selectedPageOption', pageSelectOptions[pageOptions.indexOf(pageSize)]);
      }, [pageCount, pageIndex, pageOptions, pageSelectOptions, pageSize, setValue]);

      return (
        <Form role="form">
          <Flex
            alignItems="center"
            data-testid="paginator"
            flexDirection={['column-reverse', 'row']}
            flexWrap="wrap-reverse"
            justifyContent="flex-end"
            py={1}
          >
            <Flex>
              <Box flex={1} m={1} maxWidth={170}>
                <Controller
                  as={<TextField />}
                  control={control}
                  controlGroupProps={{ pb: 0 }}
                  labelText="Jump to Page"
                  name="jumpToPage"
                  onChange={([e]) => {
                    const nextGotoPage = e.target.value ? Number(e.target.value) - 1 : 0;
                    gotoPage(nextGotoPage);
                  }}
                  sm
                  type="number"
                />
              </Box>
              <Box flex={1} maxWidth={170} mx={[1, 2]} my={1}>
                <Controller
                  as={<SelectField />}
                  control={control}
                  controlGroupProps={{ pb: 0 }}
                  labelText="Rows Per Page"
                  menuPlacement={menuPlacement}
                  name="selectedPageOption"
                  onChange={([selected]) => {
                    setPageSize(Number(selected.value));
                    return selected;
                  }}
                  options={pageSelectOptions}
                  sm
                />
              </Box>
            </Flex>
            <Flex alignItems="center" flexDirection={['column', 'row']} my={1}>
              <Text mx={[1, 2]} sm>
                {getCaptionForPage(pageVm)}
              </Text>
              <Box mx={1}>
                <Hideable hide={!showSkipButtons}>
                  <Button data-testid="gotoFirstPage" disabled={!canPreviousPage} onClick={() => gotoPage(0)} role="button">
                    <FontAwesomeIcon icon={faAngleDoubleLeft} />
                  </Button>
                </Hideable>
                <Button data-testid="gotoPreviousPage" disabled={!canPreviousPage} onClick={() => previousPage()} role="button">
                  <FontAwesomeIcon icon={faAngleLeft} />
                </Button>
                <Button data-testid="gotoNextPage" disabled={!canNextPage} onClick={() => nextPage()} role="button">
                  <FontAwesomeIcon icon={faAngleRight} />
                </Button>
                <Hideable hide={!showSkipButtons}>
                  <Button data-testid="gotoLastPage" disabled={!canNextPage} onClick={() => gotoPage(pageCount - 1)} role="button">
                    <FontAwesomeIcon icon={faAngleDoubleRight} />
                  </Button>
                </Hideable>
              </Box>
            </Flex>
          </Flex>
        </Form>
      );
    }

    Paginator.propTypes = {
      canNextPage: PropTypes.bool,
      canPreviousPage: PropTypes.bool,
      gotoPage: PropTypes.func,
      menuPlacement: PropTypes.oneOfType([
        PropTypes.array,
        PropTypes.string,
      ]),
      nextPage: PropTypes.func,
      pageCount: PropTypes.number,
      pageIndex: PropTypes.number,
      pageOptions: PropTypes.arrayOf(PropTypes.number),
      pageSize: PropTypes.number,
      previousPage: PropTypes.func,
      setPageSize: PropTypes.func,
      showSkipButtons: PropTypes.bool,
    };
    Paginator.defaultProps = {
      canNextPage: false,
      canPreviousPage: false,
      gotoPage: () => {},
      menuPlacement: 'bottom',
      nextPage: () => {},
      pageCount: 0,
      pageIndex: 0,
      pageOptions: defaultPageOptions,
      pageSize: defaultPageOptions[0],
      previousPage: () => {},
      setPageSize: () => {},
      showSkipButtons: false,
    };