import React, { FC, SyntheticEvent } from 'react' import { Colors, ElevationRange, FontSizes, FontWeights, getColor, getElevationShadow, } from '@monorail/helpers/exports' import { css, styled } from '@monorail/helpers/styled-components' import { Select } from '@monorail/v2/core/Select/Select' import { SelectItem } from '@monorail/v2/core/Select/SelectItem' import { Button } from '@monorail/visualComponents/buttons/Button' import { ButtonDisplay } from '@monorail/visualComponents/buttons/buttonTypes' import { isItemsPerPageOption, ITEMS_PER_PAGE_OPTIONS, ItemsPerPageOption, } from '@monorail/visualComponents/dataTable/ReactTableSelect/helpers' import { Icon } from '@monorail/visualComponents/icon/Icon' import { Text } from '@monorail/visualComponents/typography/Text' export enum TestId { TotalSelectedLabel = 'total-selected', ItemsPerPage = 'items-per-page', PreviousPage = 'prev-page', NextPage = 'next-page', Pages = 'pages', } //#region css const BarRow = styled.div` display: flex; flex-direction: row; justify-content: space-between; align-items: center; height: 32px; background: ${getColor(Colors.Grey96)}; padding: 4px 16px; ${getElevationShadow(ElevationRange.Elevation4)}; ` const BarSection = styled.div` display: flex; flex-direction: row; align-items: center; flex: 1 1 0; ` const NumberButton: FC<{ number: number current?: boolean handleClick: (e: SyntheticEvent) => void }> = ({ number, current = false, handleClick }) => { return current ? ( {number} ) : ( ) } const Ellipsis: FC = () => ... //#endregion //#region PageControls type PaginationButtonProps = { onClick?: (e: React.SyntheticEvent) => void disabled?: boolean } export const PaginationBackButton = ({ onClick, disabled = false, }: PaginationButtonProps) => ( ) export const PaginationNextButton = ({ onClick, disabled = false, }: PaginationButtonProps) => ( ) type PageControlsProps = Pick< SelectionPaginationBarProps, 'page' | 'pages' | 'onPageChange' > const PageControls = ({ page, pages, onPageChange }: PageControlsProps) => { const getSafePage = (pg: number) => { return Math.min(Math.max(pg, 0), pages - 1) } const changePage = ( e: SyntheticEvent, pg: number, ) => { const safePage = getSafePage(pg) if (page !== safePage) { onPageChange(safePage) } e.currentTarget.blur() } const linksForEveryPage = () => ( <> {Array.from(Array(pages).keys()).map((n, i) => ( changePage(e, n)} /> ))} ) const ellipsisAfterBeginning = () => ( <> changePage(e, 0)} current={page === 0} /> changePage(e, 1)} current={page === 1} /> changePage(e, 2)} current={page === 2} /> changePage(e, 3)} current={page === 3} /> changePage(e, 4)} /> changePage(e, pages - 1)} /> ) const ellipsisBeforeEnd = () => ( <> changePage(e, 0)} /> changePage(e, pages - 5)} /> changePage(e, pages - 4)} current={page === pages - 4} /> changePage(e, pages - 3)} current={page === pages - 3} /> changePage(e, pages - 2)} current={page === pages - 2} /> changePage(e, pages - 1)} current={page === pages - 1} /> ) const numbers = () => { if (pages < 8) { return linksForEveryPage() } if (page < 4) { return ellipsisAfterBeginning() } else if (page > pages - 5) { return ellipsisBeforeEnd() } else { return ( <> changePage(e, 0)} /> changePage(e, page - 1)} /> {}} /> changePage(e, page + 1)} /> changePage(e, pages - 1)} /> ) } } return ( <> changePage(e, page - 1)} disabled={page === 0} />
6 ? '250px' : 'auto'}; align-items: center; justify-content: space-between; `} > {numbers()}
changePage(e, page + 1)} disabled={page === pages - 1} /> ) } //#endregion const TotalSelectedLabel = ( props: Pick, 'totalSelectedItems'>, ) => (
{props.totalSelectedItems} {' selected'}
) export type SelectionPaginationBarProps = { /** * Filtered data */ sortedData: Array /** * Total unfiltered count */ totalItems: number /** * Total selected count */ totalSelectedItems: number /** * The selected option for the items-per-page Select */ itemsPerPage: ItemsPerPageOption onItemsPerPageChange: (n: ItemsPerPageOption) => void /** * Zero-indexed current page */ page: number /** * Count of pages available */ pages: number pageSize: number onPageChange: (n: number) => void } export const SelectionPaginationBar = ( props: SelectionPaginationBarProps, ) => { const { sortedData, totalItems, totalSelectedItems, page, pages, pageSize, onPageChange, itemsPerPage, onItemsPerPageChange, } = props const startingShownRange = page * pageSize + 1 const endingShownRange = Math.min((page + 1) * pageSize, sortedData.length) return ( {totalSelectedItems > 0 && ( )} Show: {`${startingShownRange}-${endingShownRange} of ${totalItems}`} ) }