Skip to content

Commit 274b915

Browse files
Merge pull request #37 from DistributedCollective/feat/SOV-680-d-2-address-table-pagination
[AddressTablePagination] - add component
2 parents 56b09ea + 9d20a20 commit 274b915

7 files changed

Lines changed: 147 additions & 0 deletions

File tree

.changeset/green-kiwis-sneeze.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@sovryn/ui": patch
3+
---
4+
5+
SOV-680: add address table pagination component
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export const ARROW_DOWN_WIDE = 'arrow-down-wide';
2+
export const ARROW_DOWN = 'arrow-down';
3+
export const ARROW_RIGHT = 'arrow-right';
4+
export const ARROW_FORWARD = 'arrow-forward';
5+
export const ARROW_BACK = 'arrow-back';
6+
export const DEPOSIT = 'deposit';
7+
export const EDIT = 'edit';
8+
export const FAILED_TX = 'failed-tx';
9+
export const INFO = 'info';
10+
export const MEATBALLS_MENU = 'meatballs-menu';
11+
export const NEW_TAB = 'new-tab';
12+
export const NOTIFICATIONS_ACTIVE = 'notifications-active';
13+
export const NOTIFICATIONS = 'notifications';
14+
export const PENDING = 'pending';
15+
export const SEARCH = 'search';
16+
export const SETTINGS = 'settings';
17+
export const SUCCESS_ICON = 'success-icon';
18+
export const SWAP_ARROW = 'swap-arrow';
19+
export const TRANSFER = 'transfer';
20+
export const TREND_ARROW_UP = 'trend-arrow-up';
21+
export const TREND_ARROW_DOWN = 'trend-arrow-down';
22+
export const WARNING = 'warning';
23+
export const WITHDRAW = 'withdraw';
24+
export const X_MARK = 'x-mark';
25+
export const COPY = 'copy';
26+
export const EXIT = 'exit';

packages/ui/src/1_atoms/Icon/iconSvgPaths.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ export const IconSvgPaths: Record<IconName, string[]> = {
1010
'arrow-forward': [
1111
'M5.68329 19.9305L13.8213 11.9975L5.68329 4.06649L7.87301 1.93275L18.2007 11.9995L7.87301 22.0662L5.68329 19.9305Z',
1212
],
13+
'arrow-back': [
14+
'M18.3167 4.06951L10.1787 12.0025L18.3167 19.9335L16.1269 22.0673L5.79922 12.0005L16.1269 1.93378L18.3167 4.06951Z',
15+
],
1316
'arrow-right': [
1417
'M16.01 10.9999H4.0001V12.9998H16.01V15.9998L20.0002 12L16.01 7.9999V10.9999Z',
1518
],
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.AddressTablePagination {
2+
@apply flex justify-center;
3+
4+
& > button {
5+
@apply mx-8 bg-transparent border-none outline-none cursor-pointer transition-opacity;
6+
&:disabled {
7+
@apply opacity-50 cursor-not-allowed;
8+
}
9+
}
10+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Story } from '@storybook/react';
2+
3+
import React, { ComponentProps } from 'react';
4+
5+
import { AddressTablePagination } from './AddressTablePagination';
6+
7+
export default {
8+
title: 'Molecule/AddressTablePagination',
9+
component: AddressTablePagination,
10+
};
11+
12+
const Template: Story<ComponentProps<typeof AddressTablePagination>> = args => (
13+
<>
14+
<AddressTablePagination
15+
{...args}
16+
onPageChange={offset => alert('offset - ' + offset)}
17+
/>
18+
</>
19+
);
20+
21+
export const Basic = Template.bind({});
22+
Basic.args = {
23+
className: '',
24+
dataLayoutId: '',
25+
itemsPerPage: 5,
26+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { render } from '@testing-library/react';
2+
import userEvent from '@testing-library/user-event';
3+
4+
import React from 'react';
5+
6+
import { AddressTablePagination } from './AddressTablePagination';
7+
8+
describe('AddressTablePagination', () => {
9+
it('renders AddressTablePagination', () => {
10+
const onPageChange = jest.fn();
11+
const { findAllByRole } = render(
12+
<AddressTablePagination onPageChange={onPageChange} />,
13+
);
14+
expect(findAllByRole('svg[data-icon="arrow-back"]')).toBeDefined();
15+
});
16+
17+
it('does not allow to click Previous without clicking Next first', () => {
18+
const onPageChange = jest.fn();
19+
const { getAllByRole } = render(
20+
<AddressTablePagination onPageChange={onPageChange} />,
21+
);
22+
getAllByRole('button').forEach(element => {
23+
userEvent.click(element);
24+
});
25+
expect(onPageChange).toBeCalledTimes(1);
26+
});
27+
});
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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

Comments
 (0)