Skip to content

Commit 84ef293

Browse files
committed
chore: review comments addressed
1 parent 41ed260 commit 84ef293

1 file changed

Lines changed: 62 additions & 48 deletions

File tree

packages/react/src/components/auth0/shared/data-pagination.tsx

Lines changed: 62 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ export interface DataPaginationProps {
8585
onPreviousPage?: () => void;
8686
}
8787

88+
interface PageRangeInfoProps {
89+
totalItems: number;
90+
currentPage: number;
91+
pageSize: number;
92+
locale?: string;
93+
labels: DataPaginationLabels;
94+
}
95+
8896
const formatNumber = (num: number | undefined | null, locale?: string): string => {
8997
if (num === null || num === undefined || isNaN(num)) {
9098
return '0';
@@ -95,6 +103,46 @@ const formatNumber = (num: number | undefined | null, locale?: string): string =
95103
return num.toLocaleString(resolvedLocale);
96104
};
97105

106+
const getPageRange = (
107+
totalItems: number,
108+
currentPage: number,
109+
pageSize: number,
110+
locale?: string,
111+
): { start: string; end: string } => {
112+
const start = totalItems === 0 ? 0 : (currentPage - 1) * pageSize + 1;
113+
const end = Math.min(currentPage * pageSize, totalItems);
114+
return {
115+
start: formatNumber(start, locale),
116+
end: formatNumber(end, locale),
117+
};
118+
};
119+
120+
/**
121+
* Renders the page range info.
122+
*
123+
* @param props - Component props.
124+
* @param props.totalItems - Total number of items.
125+
* @param props.currentPage - Current page number.
126+
* @param props.pageSize - Number of items per page.
127+
* @param props.locale - Locale identifier for number formatting.
128+
* @param props.labels - Label text configuration.
129+
* @returns JSX element displaying the page range info.
130+
*/
131+
function PageRangeInfo({ totalItems, currentPage, pageSize, locale, labels }: PageRangeInfoProps) {
132+
const range = getPageRange(totalItems, currentPage, pageSize, locale);
133+
return (
134+
<>
135+
{labels.showing}{' '}
136+
<span className="font-medium text-foreground">
137+
{range.start}-{range.end}
138+
</span>{' '}
139+
{labels.of}{' '}
140+
<span className="font-medium text-foreground">{formatNumber(totalItems, locale)}</span>
141+
{labels.results && <> {labels.results}</>}
142+
</>
143+
);
144+
}
145+
98146
/**
99147
*
100148
* @param props - Component props.
@@ -174,56 +222,22 @@ export function DataPagination({
174222
{showPageInfo && (
175223
<div className="text-sm text-foreground whitespace-nowrap sm:mr-auto">
176224
{isRegular && regularState ? (
177-
<>
178-
{labels.showing}{' '}
179-
<span className="font-medium text-foreground">
180-
{formatNumber(
181-
regularState.totalItems === 0
182-
? 0
183-
: (regularState.currentPage - 1) * regularState.pageSize + 1,
184-
locale,
185-
)}
186-
-
187-
{formatNumber(
188-
Math.min(
189-
regularState.currentPage * regularState.pageSize,
190-
regularState.totalItems,
191-
),
192-
locale,
193-
)}
194-
</span>{' '}
195-
{labels.of}{' '}
196-
<span className="font-medium text-foreground">
197-
{formatNumber(regularState.totalItems, locale)}
198-
</span>
199-
{labels.results && <> {labels.results}</>}
200-
</>
225+
<PageRangeInfo
226+
totalItems={regularState.totalItems}
227+
currentPage={regularState.currentPage}
228+
pageSize={regularState.pageSize}
229+
locale={locale}
230+
labels={labels}
231+
/>
201232
) : checkpointState?.totalItems !== undefined &&
202233
checkpointState?.currentPage !== undefined ? (
203-
<>
204-
{labels.showing}{' '}
205-
<span className="font-medium text-foreground">
206-
{formatNumber(
207-
checkpointState.totalItems === 0
208-
? 0
209-
: (checkpointState.currentPage - 1) * checkpointState.pageSize + 1,
210-
locale,
211-
)}
212-
-
213-
{formatNumber(
214-
Math.min(
215-
checkpointState.currentPage * checkpointState.pageSize,
216-
checkpointState.totalItems,
217-
),
218-
locale,
219-
)}
220-
</span>{' '}
221-
{labels.of}{' '}
222-
<span className="font-medium text-foreground">
223-
{formatNumber(checkpointState.totalItems, locale)}
224-
</span>
225-
{labels.results && <> {labels.results}</>}
226-
</>
234+
<PageRangeInfo
235+
totalItems={checkpointState.totalItems}
236+
currentPage={checkpointState.currentPage}
237+
pageSize={checkpointState.pageSize}
238+
locale={locale}
239+
labels={labels}
240+
/>
227241
) : checkpointState?.totalItems !== undefined ? (
228242
<>
229243
<span className="font-medium text-foreground">

0 commit comments

Comments
 (0)