All files / src/Pagination Pagination.tsx

70.83% Statements 17/24
50% Branches 2/4
50% Functions 4/8
70.83% Lines 17/24

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                                                1x                           19x   19x   19x 19x 18x     19x             19x             19x             19x               19x                     19x               19x                                       19x                                                             1x   19x 19x                            
import * as React from 'react';
import { Box as ReakitBox } from 'reakit';
 
import { useClassName, createComponent, createElement, createHook } from '../utils';
import { Box, BoxProps } from '../Box';
import { Button, ButtonProps } from '../Button';
import { Set, SetProps } from '../Set';
import { Select, SelectProps } from '../Select';
 
import * as styles from './styles';
 
export type LocalPaginationProps = {
  numberOfPages: number;
  currentPage?: number;
  onChangePage?: (page: number) => void;
  nextText?: string;
  previousText?: string;
  prepositionText?: string;
  nextButtonProps?: ButtonProps;
  previousButtonProps?: ButtonProps;
  selectProps?: Omit<SelectProps, 'options'>;
};
export type PaginationProps = BoxProps & LocalPaginationProps;
 
const useProps = createHook<PaginationProps>(
  (props, { themeKey, themeKeyOverride }) => {
    const {
      currentPage: _currentPage,
      nextButtonProps,
      nextText,
      numberOfPages,
      onChangePage,
      overrides,
      prepositionText,
      previousButtonProps,
      previousText,
      selectProps,
      ...restProps
    } = props;
 
    const setProps = Set.useProps({ ...restProps, overrides });
 
    const [currentPage, setCurrentPage] = React.useState(1);
    React.useEffect(() => {
      setCurrentPage(_currentPage || 1);
    }, [_currentPage]);
 
    const className = useClassName({
      style: styles.Pagination,
      styleProps: props,
      themeKey,
      themeKeyOverride,
      prevClassName: setProps.className
    });
    const buttonClassName = useClassName({
      style: styles.PaginationButton,
      styleProps: props,
      themeKey,
      themeKeyOverride,
      themeKeySuffix: 'Button'
    });
    const selectClassName = useClassName({
      style: styles.PaginationSelect,
      styleProps: props,
      themeKey,
      themeKeyOverride,
      themeKeySuffix: 'Select'
    });
    const prepositionClassName = useClassName({
      style: styles.PaginationPrepositionText,
      styleProps: props,
      themeKey,
      themeKeyOverride,
      themeKeySuffix: 'PrepositionText'
    });
 
    const handleChangePage = React.useCallback(
      page => {
        if (onChangePage) {
          onChangePage(page);
        } else {
          setCurrentPage(page);
        }
      },
      [onChangePage]
    );
 
    const handleChangePageDropdown = React.useCallback(
      e => {
        const index = parseInt(e.target.value, 10);
        handleChangePage(index + 1);
      },
      [handleChangePage]
    );
 
    return {
      ...setProps,
      className,
      children: (
        <React.Fragment>
          <Button
            className={buttonClassName}
            disabled={currentPage === 1}
            onClick={() => handleChangePage(currentPage - 1)}
            iconBefore="chevron-left"
            kind="ghost"
            themeKey={`${themeKey}.Button`}
            overrides={overrides}
            {...previousButtonProps}
          >
            {previousText}
          </Button>
          <Select
            className={selectClassName}
            onChange={handleChangePageDropdown}
            options={[...Array(10).keys()].map(index => ({
              label: `${index + 1}`,
              value: index
            }))}
            value={currentPage - 1}
            themeKey={`${themeKey}.Select`}
            overrides={overrides}
            {...selectProps}
          />
          <Box className={prepositionClassName}>
            {prepositionText} {numberOfPages}
          </Box>
          <Button
            className={buttonClassName}
            disabled={currentPage === numberOfPages}
            onClick={() => handleChangePage(currentPage + 1)}
            iconAfter="chevron-right"
            kind="ghost"
            themeKey={`${themeKey}.Button`}
            overrides={overrides}
            {...nextButtonProps}
          >
            {nextText}
          </Button>
        </React.Fragment>
      )
    };
  },
  { defaultProps: { prepositionText: 'of', previousText: 'Previous', nextText: 'Next' }, themeKey: 'Pagination' }
);
 
export const Pagination = createComponent<PaginationProps>(
  props => {
    const textProps = useProps(props);
    return createElement({
      children: props.children,
      component: ReakitBox,
      use: props.use,
      htmlProps: textProps
    });
  },
  {
    attach: {
      useProps
    },
    themeKey: 'Pagination'
  }
);