From 12e559762a25d8c515d0f0b524c40af9914b941a Mon Sep 17 00:00:00 2001 From: spotikhanov Date: Fri, 23 Aug 2024 11:12:16 +0300 Subject: [PATCH] feat(Pagination): add size prop (#1759) --- src/components/Pagination/Pagination.tsx | 5 ++- src/components/Pagination/README.md | 1 + .../Pagination/__tests__/Pagination.test.tsx | 45 +++++++++++++++++++ .../PaginationEllipsis.scss | 8 ++++ .../PaginationInput/PaginationInput.scss | 8 ++++ .../PaginationPage/PaginationPage.scss | 8 ++++ .../PaginationPageOf/PaginationPageOf.scss | 8 ++++ src/components/Pagination/types.ts | 6 ++- src/components/Pagination/utils.test.ts | 20 ++++++++- src/components/Pagination/utils.ts | 16 +++++++ 10 files changed, 121 insertions(+), 4 deletions(-) diff --git a/src/components/Pagination/Pagination.tsx b/src/components/Pagination/Pagination.tsx index c730a8ed1f..8d740539e6 100644 --- a/src/components/Pagination/Pagination.tsx +++ b/src/components/Pagination/Pagination.tsx @@ -15,7 +15,7 @@ import { } from './components'; import {usePagination} from './hooks/usePagination'; import type {PaginationProps} from './types'; -import {getResultPage, getResultTotal} from './utils'; +import {getResultPage, getResultTotal, getSize} from './utils'; import './Pagination.scss'; @@ -25,6 +25,7 @@ export const Pagination = ({ page, pageSize, total, + size: propSize, onUpdate, compact: propCompact = true, pageSizeOptions, @@ -35,7 +36,7 @@ export const Pagination = ({ }: PaginationProps) => { const mobile = useMobile(); - const size = mobile ? 'l' : 'm'; + const size = getSize({propSize, mobile}); const compact = mobile ? true : propCompact; const resultTotal = getResultTotal(total); diff --git a/src/components/Pagination/README.md b/src/components/Pagination/README.md index 098edafbfe..d613f176ab 100644 --- a/src/components/Pagination/README.md +++ b/src/components/Pagination/README.md @@ -30,6 +30,7 @@ const pagination = { test('Single page', () => { @@ -82,4 +85,46 @@ describe('Pagination component', () => { expect(nextButton).not.toBeDisabled(); }); + + test.each(new Array('m', 'l', undefined))( + '[desktop]: render with given "%s" size', + (size) => { + render(); + + const expectedSize = getSize({mobile: false, propSize: size}); + const expectedClass = `g-button_size_${expectedSize}`; + + const firstButton = screen.getByTestId(PaginationQa.PaginationButtonFirst); + expect(firstButton).toHaveClass(expectedClass); + + const prevButton = screen.getByTestId(PaginationQa.PaginationButtonPrevious); + expect(prevButton).toHaveClass(expectedClass); + + const nextButton = screen.getByTestId(PaginationQa.PaginationButtonNext); + expect(nextButton).toHaveClass(expectedClass); + }, + ); + + test.each(new Array('m', 'l', undefined))( + '[mobile]: render with given "%s" size', + (size) => { + render( + + , + , + ); + + const expectedSize = getSize({mobile: true, propSize: size}); + const expectedClass = `g-button_size_${expectedSize}`; + + const firstButton = screen.getByTestId(PaginationQa.PaginationButtonFirst); + expect(firstButton).toHaveClass(expectedClass); + + const prevButton = screen.getByTestId(PaginationQa.PaginationButtonPrevious); + expect(prevButton).toHaveClass(expectedClass); + + const nextButton = screen.getByTestId(PaginationQa.PaginationButtonNext); + expect(nextButton).toHaveClass(expectedClass); + }, + ); }); diff --git a/src/components/Pagination/components/PaginationEllipsis/PaginationEllipsis.scss b/src/components/Pagination/components/PaginationEllipsis/PaginationEllipsis.scss index f7940fb8a7..53fd9e2ae0 100644 --- a/src/components/Pagination/components/PaginationEllipsis/PaginationEllipsis.scss +++ b/src/components/Pagination/components/PaginationEllipsis/PaginationEllipsis.scss @@ -9,6 +9,10 @@ $block: '.#{variables.$ns}pagination-ellipsis'; align-items: flex-end; color: var(--g-color-text-secondary); + &_size_s { + padding-block-end: 3px; + } + &_size_m { padding-block-end: 5px; } @@ -16,4 +20,8 @@ $block: '.#{variables.$ns}pagination-ellipsis'; &_size_l { padding-block-end: 9px; } + + &_size_xl { + padding-block-end: 11px; + } } diff --git a/src/components/Pagination/components/PaginationInput/PaginationInput.scss b/src/components/Pagination/components/PaginationInput/PaginationInput.scss index 2907e28832..df08256e53 100644 --- a/src/components/Pagination/components/PaginationInput/PaginationInput.scss +++ b/src/components/Pagination/components/PaginationInput/PaginationInput.scss @@ -3,6 +3,10 @@ $block: '.#{variables.$ns}pagination-input'; #{$block} { + &#{$block}_size_s { + width: 70px; + } + &#{$block}_size_m { width: 80px; } @@ -10,4 +14,8 @@ $block: '.#{variables.$ns}pagination-input'; &#{$block}_size_l { width: 90px; } + + &#{$block}_size_xl { + width: 100px; + } } diff --git a/src/components/Pagination/components/PaginationPage/PaginationPage.scss b/src/components/Pagination/components/PaginationPage/PaginationPage.scss index 6abfa0f10b..1c366eaf77 100644 --- a/src/components/Pagination/components/PaginationPage/PaginationPage.scss +++ b/src/components/Pagination/components/PaginationPage/PaginationPage.scss @@ -9,6 +9,10 @@ $block: '.#{variables.$ns}pagination-page'; display: flex; align-items: center; + &_size_s { + padding: 0 8px; + } + &_size_m { padding: 0 13px; } @@ -16,5 +20,9 @@ $block: '.#{variables.$ns}pagination-page'; &_size_l { padding: 0 18px; } + + &_size_xl { + padding: 0 21px; + } } } diff --git a/src/components/Pagination/components/PaginationPageOf/PaginationPageOf.scss b/src/components/Pagination/components/PaginationPageOf/PaginationPageOf.scss index 9e9363fc4d..937427d65f 100644 --- a/src/components/Pagination/components/PaginationPageOf/PaginationPageOf.scss +++ b/src/components/Pagination/components/PaginationPageOf/PaginationPageOf.scss @@ -9,6 +9,10 @@ $block: '.#{variables.$ns}pagination-page-of'; align-items: flex-end; color: var(--g-color-text-secondary); + &_size_s { + padding-block-end: 3px; + } + &_size_m { padding-block-end: 5px; } @@ -16,4 +20,8 @@ $block: '.#{variables.$ns}pagination-page-of'; &_size_l { padding-block-end: 9px; } + + &_size_xl { + padding-block-end: 11px; + } } diff --git a/src/components/Pagination/types.ts b/src/components/Pagination/types.ts index 821ce04ee1..f926dbfcd1 100644 --- a/src/components/Pagination/types.ts +++ b/src/components/Pagination/types.ts @@ -2,7 +2,7 @@ import type {QAProps} from '../types'; export type ActionName = 'previous' | 'next' | 'first'; -export type PaginationSize = 'm' | 'l'; +export type PaginationSize = 's' | 'm' | 'l' | 'xl'; export type PaginationProps = { /** @@ -13,6 +13,10 @@ export type PaginationProps = { * Number of data items per page. */ pageSize: number; + /** + * Size of the pagination items. + */ + size?: PaginationSize; /** * Called when the page number or pageSize is changed. */ diff --git a/src/components/Pagination/utils.test.ts b/src/components/Pagination/utils.test.ts index a8bdc0ceed..bb2d6572fa 100644 --- a/src/components/Pagination/utils.test.ts +++ b/src/components/Pagination/utils.test.ts @@ -1,4 +1,4 @@ -import {getNumerationList} from './utils'; +import {getNumerationList, getSize} from './utils'; describe('Pagination utils', () => { describe('[desktop]: getNumerationList', () => { @@ -342,4 +342,22 @@ describe('Pagination utils', () => { ]); }); }); + + describe('[desktop]: getSize', () => { + it('without size prop', () => { + expect(getSize({mobile: false})).toEqual('m'); + }); + it('size prop is accounted', () => { + expect(getSize({mobile: false, propSize: 'l'})).toEqual('l'); + }); + }); + + describe('[mobile]: getSize', () => { + it('without size prop', () => { + expect(getSize({mobile: true})).toEqual('l'); + }); + it('size prop is accounted', () => { + expect(getSize({mobile: false, propSize: 'm'})).toEqual('m'); + }); + }); }); diff --git a/src/components/Pagination/utils.ts b/src/components/Pagination/utils.ts index d950bd7ebb..e5b0ccdfee 100644 --- a/src/components/Pagination/utils.ts +++ b/src/components/Pagination/utils.ts @@ -1,5 +1,7 @@ import uniq from 'lodash/uniq'; +import type {PaginationSize} from './types'; + export function getNumerationList({ page, numberOfPages, @@ -56,6 +58,20 @@ export function getResultTotal(total: number | undefined) { return total === undefined || total > 0 ? total : 1; } +export function getSize({ + propSize, + mobile, +}: { + propSize?: PaginationSize; + mobile: boolean; +}): PaginationSize { + if (propSize) { + return propSize; + } + + return mobile ? 'l' : 'm'; +} + export function getResultPage({ page, total,