|
| 1 | +import React, { FC, useCallback, useMemo, useState } from 'react'; |
| 2 | + |
| 3 | +import classnames from 'classnames'; |
| 4 | + |
| 5 | +import { Icon } from '../../1_atoms'; |
| 6 | +import styles from './AddressTablePagination.module.css'; |
| 7 | + |
| 8 | +type AddressTablePaginationProps = { |
| 9 | + onPageChange: (value: number) => void; |
| 10 | + className?: string; |
| 11 | + dataLayoutId?: string; |
| 12 | + itemsPerPage?: number; |
| 13 | +}; |
| 14 | + |
| 15 | +const DEFAULT_ITEMS_PER_PAGE = 5; |
| 16 | + |
| 17 | +export const AddressTablePagination: FC<AddressTablePaginationProps> = ({ |
| 18 | + onPageChange, |
| 19 | + className, |
| 20 | + dataLayoutId, |
| 21 | + itemsPerPage = DEFAULT_ITEMS_PER_PAGE, |
| 22 | +}) => { |
| 23 | + const [offset, setOffset] = useState(0); |
| 24 | + |
| 25 | + const handleClickNext = useCallback(() => { |
| 26 | + setOffset(offset + itemsPerPage); |
| 27 | + onPageChange(offset + itemsPerPage); |
| 28 | + }, [setOffset, offset, itemsPerPage, onPageChange]); |
| 29 | + |
| 30 | + const handleClickPrevious = useCallback(() => { |
| 31 | + setOffset(offset - itemsPerPage); |
| 32 | + onPageChange(offset - itemsPerPage); |
| 33 | + }, [setOffset, offset, itemsPerPage, onPageChange]); |
| 34 | + |
| 35 | + const isDisabled = useMemo(() => offset <= 0, [offset]); |
| 36 | + |
| 37 | + return ( |
| 38 | + <div |
| 39 | + data-layout-id={dataLayoutId} |
| 40 | + className={classnames(styles.AddressTablePagination, className)} |
| 41 | + > |
| 42 | + <button type="button" disabled={isDisabled} onClick={handleClickPrevious}> |
| 43 | + <Icon icon="arrow-back" size={12} /> |
| 44 | + </button> |
| 45 | + <button type="button" onClick={handleClickNext}> |
| 46 | + <Icon icon="arrow-forward" size={12} /> |
| 47 | + </button> |
| 48 | + </div> |
| 49 | + ); |
| 50 | +}; |
0 commit comments