import { useEffect, useState } from 'react'; import styled from '@emotion/styled'; import { css, withTheme } from '@emotion/react'; import noop from 'utils/noop'; import Button from 'components/Button'; import DropDownIcon from 'components/Icon/icons/arrows/dropdown'; const Container = styled.div` display: inline-flex; align-items: center; ${({ theme: { rtl } }) => rtl && css` direction: rtl; `} `; const Text = styled.span` ${({ theme: { rtl } }) => css` padding-${rtl ? 'left' : 'right'}: 0.86em; `} `; Text.displayName = 'Text'; const SCButton = styled(Button)` ${({ showLabel }) => !showLabel && css` padding: 0.5em; `}; `; SCButton.displayName = 'SCButton'; interface Props { /** Current Page number */ current?: number; /** Number of items per page */ pageSize?: number; /** Total number of items */ total?: number | null; /** Loading status for AJAX calls, disable buttons when it is `true` */ loading?: boolean; /** Text and title of prev button */ prevLabel?: string; /** Text and title of next button */ nextLabel?: string; /** Show text instead of icon for prev and next button */ showLabel?: boolean; /** * Callback function, to be executed when clicked prev and next button * * @param {Integer} nextPage Next page number * @param {Integer} pageSize Number of items per page * @param {Integer} fromIndex 1-based index of item start from * @param {Integer} toIndex 1-based index of item end to */ onChange?: ( _nextPage: number, _pageSize: number, _fromIndex: number, _toIndex: number ) => void; /** Description text next to pagination buttons, use `{{fromIndex}}` for 1-based index of item * start from, `{{toIndex}}` for 1-based index of item end to, `{{total}}` for total number * of items, `{{current}}` for current page number, `{{totalPages}}` for total * page number */ description?: string; /** Default initial page number */ defaultCurrent?: number; /** Default number of items per page */ defaultPageSize?: number; /** Default total number of items */ defaultTotal?: number; /** theme injected by withTheme */ theme?: { rtl: boolean; }; } const Pagination = ({ current = 0, pageSize = 0, total = null, loading = false, prevLabel = 'Prev', nextLabel = 'Next', showLabel = false, onChange = noop, description = 'Viewing {{fromIndex}}-{{toIndex}} of {{total}}', defaultCurrent = 1, defaultPageSize = 20, defaultTotal = 40, theme, }: Props) => { const [current_, setCurrent] = useState(current || defaultCurrent); const [pageSize_, setPageSize] = useState(pageSize || defaultPageSize); const [total_, setTotal] = useState(total ?? defaultTotal); // getDerivedStateFromProps equivalent useEffect(() => { if (current && current !== current_) { setCurrent(current); } if (pageSize && pageSize !== pageSize_) { setPageSize(pageSize); } if (total !== null && total !== total_) { setTotal(total); } }, [current, current_, pageSize, pageSize_, total, total_]); useEffect(() => { if (current && onChange === noop) { // eslint-disable-next-line no-console console.warn( 'No `onChange` handler provided with `current` prop. This will render a read-only' + ' component.' ); } }); const fromIndex = total_ === 0 ? 0 : current_ * pageSize_ - pageSize_ + 1; const toIndex = current_ * pageSize_ > total_ ? total_ : current_ * pageSize_; const totalPages = Math.ceil(total_ / pageSize_); const hasPrev = current_ > 1; const hasNext = current_ < totalPages; const handleChange = (nextPage: number) => { if (!current) { setCurrent(nextPage); } onChange(nextPage, pageSize, fromIndex, toIndex); }; const prev = () => { if (hasPrev) { handleChange(current_ - 1); } }; const next = () => { if (hasNext) { handleChange(current_ + 1); } }; return ( {description .replace('{{fromIndex}}', `${fromIndex}`) .replace('{{toIndex}}', `${toIndex}`) .replace('{{total}}', `${total_}`) .replace('{{current}}', `${current_}`) .replace('{{totalPages}}', `${totalPages}`)} {showLabel ? ( prevLabel ) : ( )} {showLabel ? ( nextLabel ) : ( )} ); }; export default withTheme(Pagination);