import React, { useState, useEffect, useRef } from 'react'; import PropTypes from 'prop-types'; import i18next from 'i18next'; import { get } from 'lodash'; import { ThemeProvider } from 'styled-components'; import { PaginatorContainer, MaxPerPageDropdown, DropdownButton, DropdownList, DropdownItem, DropdownText, PagesHandler, PageSwitcher, PagesDisplay, PaginatorInfo, ElementsInfo, SelectedItemsInfo, SelectedItemsCount, Icon, } from './styledComponents'; import { Props } from './interfaces'; import ArrowGrey from '../images/icon-arrow-grey.svg'; import DropdownGrey from '../images/icon-dropdown-grey.svg'; import ArrowLeftBlue from '../images/icon-arrow-left-blue.svg'; import CheckMarkBlue from '../images/icon-check-blue.svg'; import DropdownDark from '../images/icon-dropdown-dark.svg'; import ArrowLeftDark from '../images/icon-arrow-left-dark.svg'; import CheckMarkDark from '../images/icon-check-dark.svg'; import { getTheme } from '../utils/theme'; const icons = { blue: { arrow: ArrowGrey, check: CheckMarkBlue, dropdown: DropdownGrey, navigation: ArrowLeftBlue, }, dark: { arrow: ArrowGrey, check: CheckMarkDark, dropdown: DropdownDark, navigation: ArrowLeftDark, }, }; const Paginator = (props: Props): JSX.Element => { const { theme, isLoading, currentPage, setCurrentPage, itemsCount, selectedItemsCount, maxPerPage, setMaxPerPage, maxPerPageOptions, hideAllPerPageOption, } = props; const updatedTheme = getTheme(theme, 'listView'); const [isDropdownOpen, setIsDropdownOpen] = useState(false); const dropdownRef = useRef(null); const getMaxPage = () => { return Math.ceil(itemsCount / maxPerPage); }; const getFirstElementOfPage = () => { return (currentPage - 1) * maxPerPage + 1; }; const getLastElementOfPage = () => { if (currentPage * maxPerPage > itemsCount) { return itemsCount; } return currentPage * maxPerPage; }; const setPreviousPage = () => { if (currentPage <= 1) { return; } setCurrentPage(currentPage - 1); }; const setNextPage = () => { if (currentPage >= getMaxPage()) { return; } setCurrentPage(currentPage + 1); }; const toggle = () => { setIsDropdownOpen(!isDropdownOpen); }; const close = () => { setIsDropdownOpen(false); }; const handleMaxPerPageChange = (option) => { setMaxPerPage(option); close(); }; useEffect(() => { function handleClickOutside(event) { const currentRef = get(dropdownRef, 'current'); if (currentRef && !currentRef.contains(event.target)) { close(); } } document.addEventListener('mousedown', handleClickOutside); return () => { document.removeEventListener('mousedown', handleClickOutside); }; }, [dropdownRef]); return ( !isLoading && toggle()} > {maxPerPage === itemsCount ? i18next.t('COMPONENT_LIST_VIEW_PAGINATOR_PER_PAGE_ALL') : `${maxPerPage} ${i18next.t( 'COMPONENT_LIST_VIEW_PAGINATOR_PER_PAGE' )}`} {isDropdownOpen && ( {maxPerPageOptions.map((option) => ( { handleMaxPerPageChange(option); setCurrentPage(1); }} > {option}{' '} {i18next.t('COMPONENT_LIST_VIEW_PAGINATOR_PER_PAGE')} {option === maxPerPage && ( )} ))} {!hideAllPerPageOption && ( { handleMaxPerPageChange(itemsCount); setCurrentPage(1); }} > {i18next.t('COMPONENT_LIST_VIEW_PAGINATOR_PER_PAGE_ALL')} {itemsCount === maxPerPage && ( )} )} )} {getFirstElementOfPage()} - {getLastElementOfPage()}{' '} {i18next.t('COMPONENT_LIST_VIEW_PAGINATOR_ON')} {itemsCount} {!!selectedItemsCount && ( {selectedItemsCount}{' '} {i18next.t('COMPONENT_LIST_VIEW_PAGINATOR_SELECTED')} )} !isLoading && setPreviousPage()} > {currentPage} / {getMaxPage()} !isLoading && setNextPage()} > ); }; Paginator.propTypes = { isLoading: PropTypes.bool, itemsCount: PropTypes.number.isRequired, selectedItemsCount: PropTypes.number, maxPerPage: PropTypes.number.isRequired, maxPerPageOptions: PropTypes.arrayOf(PropTypes.number), // eslint-disable-next-line react/forbid-prop-types theme: PropTypes.objectOf(PropTypes.any), setMaxPerPage: PropTypes.func, setCurrentPage: PropTypes.func, currentPage: PropTypes.number, hideAllPerPageOption: PropTypes.bool, }; Paginator.defaultProps = { isLoading: false, selectedItemsCount: 0, theme: null, setMaxPerPage: () => {}, setCurrentPage: () => {}, currentPage: 0, maxPerPageOptions: [10, 20, 50, 100], hideAllPerPageOption: false, }; export default Paginator;