# All files / src/Pagination PaginationSelect.tsx

35.59% Statements 21/59
3.75% Branches 3/80
10% Functions 1/10
35.29% Lines 18/51

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
    169
    170
    171
    1722x
    2x
    2x
    2x

    2x

    2x

    2x

    2x

    2x

    7x
    7x
    7x
    7x
    7x
    7x
    7x

    7x

    7x

     import { times } from 'ramda'
    import React from 'react'
    import { DropdownButton } from '../Dropdown/Dropdown'
    import { MenuImperative } from '../MenuImperative/MenuImperative'
    import { MenuRef, Selection } from '../MenuImperative/MenuImperative.types'
    import {
      OverlayTrigger,
      useOverlayTriggerContext,
    } from '../OverlayTrigger/OverlayTrigger'
    import { StyledOverlay } from './Pagination.styles'
    import { PageSelectProps, PaginationMenuProps } from './Pagination.types'

    const noop = () => {}

    const PaginationMenu = React.forwardRef<HTMLDivElement, PaginationMenuProps>(
      function PaginationMenu(
        { activePage, menuRef, onSelect: onSelect_ = noop, pages, ...props },
        ref
      ) {
        const ctx = useOverlayTriggerContext()

        const lastItemRef = React.useRef<HTMLDivElement>(null)

        const hasFooter = pages > 10

        React.useEffect(() => {
          menuRef.current?.highlightFirst()
        }, [menuRef])

        React.useEffect(() => {
          menuRef.current?.highlightSuggested()
          menuRef.current?.highlightSelected()
        }, [menuRef])

        function onKeyDown(event: React.KeyboardEvent<HTMLDivElement>) {
          const currentPage = menuRef.current?.highlighted()

          if (event.key === 'ArrowDown' || event.key === 'Down') {
            // when on the second to last item, want to jump to the last item (in the footer)
            Iif (currentPage === pages - 1 && lastItemRef.current) {
              menuRef.current?.highlight(lastItemRef.current)
            }
          } else Iif (event.key === 'ArrowUp' || event.key === 'Up') {
            // on the last item (in the footer), want to jump back to the second to last
            // item back in the menu
            Iif (hasFooter && currentPage === pages) {
              event.stopPropagation()
              menuRef.current?.highlightLast()
            }
          }
        }

        function onSelect(selection: Selection) {
          onSelect_(selection)

          ctx.hide(selection.event)
        }

        const children = times((index: number) => {
          const page = index + 1

          return (
            <MenuImperative.Item
              item={page}
              key={index}
              selected={page === activePage}
              ref={lastItemRef}
            >
              {page}
            </MenuImperative.Item>
          )
        }, pages)

        return (
          <StyledOverlay ref={ref} shadowStrength={2}>
            <MenuImperative
              circular
              {...props}
              ref={menuRef}
              onKeyDown={onKeyDown}
              onSelect={onSelect}
            >
              {hasFooter ? (
                <>
                  <MenuImperative.Options>
                    {children.slice(0, -1)}
                  </MenuImperative.Options>
                  <MenuImperative.Footer padding="xs none">
                    {children[children.length - 1]}
                  </MenuImperative.Footer>
                </>
              ) : (
                <MenuImperative.Options>{children}</MenuImperative.Options>
              )}
            </MenuImperative>
          </StyledOverlay>
        )
      }
    )

    export const PaginationSelect = React.forwardRef<
      HTMLButtonElement,
      PageSelectProps
    >(function PaginationSelect(
      {
        activePage = 1,
        [`aria-labelledby`]: ariaLabelledby,
        disabled,
        id,
        onSelect,
        pages = 0,
        ...props
      },
      ref
    ) {
      const menuRef = React.useRef<MenuRef>(null)

      // Safari has an issue with giving focus to buttons after clicking on them
      // if the button or menu do not have focus, the keyboard navigation does not work,
      // so just focus the menu when we open it
      function afterShow() {
        // this is brittle, if MenuImperative dom structure changes this could break
        // but super specific to pagination, not really a concern elsewhere
        ;(menuRef.current?.el?.firstChild as HTMLDivElement)?.focus()
      }

      function onKeyDown(e: React.KeyboardEvent<HTMLButtonElement>) {
        if (e.key === 'Up' || e.key === 'ArrowUp') {
          e.preventDefault()
          menuRef.current?.prev()
        } else if (e.key === 'Down' || e.key === 'ArrowDown') {
          e.preventDefault()
          menuRef.current?.next()
        } else Iif (e.key === 'Enter') {
          e.preventDefault()
          menuRef.current?.select(e)
        }
      }

      return (
        <OverlayTrigger
          role="listbox"
          passA11yPropsToOverlay
          autoFocus
          afterShow={afterShow}
          overlay={
            <PaginationMenu
              activePage={activePage}
              menuRef={menuRef}
              onSelect={onSelect}
              pages={pages}
            />
          }
          placement="bottom-left"
          ref={ref}
          trigger="click"
          {...props}
        >
          <DropdownButton
            aria-labelledby={ariaLabelledby}
            arrow
            disabled={disabled}
            id={id}
            onKeyDown={onKeyDown}
            variant="tertiary"
          >
            {activePage}
          </DropdownButton>
        </OverlayTrigger>
      )
    })