Skip to content

Commit 15c3bc2

Browse files
author
Kubit
committed
Add new styles and features to Pagination
1 parent c80f88a commit 15c3bc2

File tree

4 files changed

+55
-8
lines changed

4 files changed

+55
-8
lines changed

src/components/pagination/helpers/getMaxCountersNumber.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export const buildstepsNumber = (
2020

2121
let currentPosition = currentStep + 1;
2222

23-
if (currentPosition >= maxCounters) {
23+
if (currentPosition >= maxCounters && maxSteps !== maxCounters) {
2424
maxCounters--;
2525
startWith = [1, '...'];
2626
}

src/components/pagination/pagination.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,18 @@ const PaginationComponent = React.forwardRef(
2222

2323
const limitCurrentStep = Math.max(0, Math.min(currentStep, maxStepsNumber - 1));
2424

25+
let paginationCountersNumber = styles.paginationCountersNumber?.[device]?.counters ?? 5;
26+
27+
if (maxCountersNumber && maxCountersNumber < maxStepsNumber) {
28+
paginationCountersNumber = maxCountersNumber;
29+
} else {
30+
paginationCountersNumber = Math.min(maxStepsNumber, paginationCountersNumber);
31+
}
32+
2533
const stepsNumber = buildstepsNumber(
2634
limitCurrentStep,
2735
maxStepsNumber,
28-
styles.paginationCountersNumber?.[device]?.counters,
36+
paginationCountersNumber,
2937
maxCountersNumber
3038
);
3139
const stepActive = stepsNumber.indexOf(limitCurrentStep + 1);

src/components/pagination/stories/argtypes.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export const argtypes = (variants: IThemeObjectVariants, themeSelected: string):
2424
currentStep: {
2525
control: { type: 'number' },
2626
type: { name: 'number', required: true },
27-
description: 'Indicate the parent current position',
27+
description: 'Indicate the parent current position. The max value is maxStepsNumber',
2828
table: {
2929
type: {
3030
summary: 'number',
@@ -33,9 +33,9 @@ export const argtypes = (variants: IThemeObjectVariants, themeSelected: string):
3333
},
3434
},
3535
maxStepsNumber: {
36-
control: { type: 'number' },
36+
control: { type: 'number', min: 2 },
3737
type: { name: 'number', required: true },
38-
description: 'Set the max steps number',
38+
description: 'Set the max steps number. The min value is 2',
3939
table: {
4040
type: {
4141
summary: 'number',
@@ -46,7 +46,7 @@ export const argtypes = (variants: IThemeObjectVariants, themeSelected: string):
4646
maxCountersNumber: {
4747
control: { type: 'number' },
4848
type: { name: 'number' },
49-
description: 'Set the custom steps number to show',
49+
description: 'Set the custom steps number to show. The min value is 2',
5050
table: {
5151
type: {
5252
summary: 'number',

src/components/pagination/stories/pagination.stories.tsx

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Meta, StoryObj } from '@storybook/react';
2-
import * as React from 'react';
2+
import React, { useEffect } from 'react';
33

44
import { ICONS } from '@/assets';
55
import { STYLES_NAME } from '@/constants';
@@ -17,6 +17,12 @@ const meta = {
1717
component: Story,
1818
parameters: {
1919
layout: 'centered',
20+
docs: {
21+
description: {
22+
component:
23+
'Pagination is a controlled component. This story is an example of how to use it. To see other examples or modify the component behavior, visit the other stories in Pagination section or change the props in the controls.',
24+
},
25+
},
2026
},
2127
tags: ['autodocs'],
2228
argTypes: argtypes(variantsObject, themeSelected),
@@ -29,7 +35,13 @@ type Story = StoryObj<typeof meta> & { args: { themeArgs?: object } };
2935

3036
const StoryWithHooks = args => {
3137
const [step, setStep] = React.useState(0);
32-
const maxSteps = 10;
38+
const maxSteps = args.maxStepsNumber ? args.maxStepsNumber : 35;
39+
40+
useEffect(() => {
41+
if (args.currentStep) {
42+
setStep(args.currentStep - 1);
43+
}
44+
}, [args.currentStep]);
3345

3446
const leftEdge = step <= 0;
3547
const rightEdge = step >= maxSteps;
@@ -80,6 +92,33 @@ export const Pagination = {
8092
variantsObject[themeSelected].PaginationVariantsTheme || {}
8193
)[0] as string,
8294
themeArgs: themesObject[themeSelected][STYLES_NAME.PAGINATION],
95+
currentStep: 1,
96+
maxStepsNumber: 35,
97+
maxCountersNumber: 5,
98+
},
99+
};
100+
101+
export const PaginationWithTwoSteps = {
102+
args: {
103+
variant: Object.values(
104+
variantsObject[themeSelected].PaginationVariantsTheme || {}
105+
)[0] as string,
106+
themeArgs: themesObject[themeSelected][STYLES_NAME.PAGINATION],
107+
currentStep: 1,
108+
maxStepsNumber: 2,
109+
maxCountersNumber: 2,
110+
},
111+
};
112+
113+
export const PaginationWithFiveSteps = {
114+
args: {
115+
variant: Object.values(
116+
variantsObject[themeSelected].PaginationVariantsTheme || {}
117+
)[0] as string,
118+
themeArgs: themesObject[themeSelected][STYLES_NAME.PAGINATION],
119+
currentStep: 1,
120+
maxStepsNumber: 5,
121+
maxCountersNumber: 5,
83122
},
84123
};
85124

0 commit comments

Comments
 (0)