Skip to content

Commit

Permalink
Merge pull request #383 from b-hok/feature/localize-pagination-total
Browse files Browse the repository at this point in the history
Feat: Add pagination total results
  • Loading branch information
GPortas authored May 2, 2024
2 parents 774661b + 986434e commit 0feb0ae
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 19 deletions.
10 changes: 5 additions & 5 deletions public/locales/en/pagination.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"results_one": "1 {{item}}",
"results_other": "{{start}} to {{end}} of {{count}} {{item}}s",
"results_other": "{{start}} to {{end}} of {{formattedCount}} {{item}}s",
"accumulated": {
"one": "{{count}} {{item}}",
"lessThanPageSize": "{{count}} {{item}}s",
"moreThanPageSize": "{{accumulated}} of {{count}} {{item}}s seen"
"one": "{{formattedCount}} {{item}}",
"lessThanPageSize": "{{formattedCount}} {{item}}s",
"moreThanPageSize": "{{accumulated}} of {{formattedCount}} {{item}}s seen"
},
"pageSize": "{{item}}s per page"
}
}
33 changes: 20 additions & 13 deletions src/sections/shared/pagination/PaginationResultsInfo.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback } from 'react'
import { useCallback, useMemo } from 'react'
import styles from './Pagination.module.scss'
import { PaginationInfo } from '../../../shared/pagination/domain/models/PaginationInfo'
import { useTranslation } from 'react-i18next'
Expand All @@ -18,25 +18,32 @@ export function PaginationResultsInfo({ paginationInfo, accumulated }: Paginatio
accumulated === 1
? 'accumulated.one'
: accumulated < paginationInfo.pageSize
? 'accumulated.lessThanPageSize'
: 'accumulated.moreThanPageSize',
? 'accumulated.lessThanPageSize'

Check failure on line 21 in src/sections/shared/pagination/PaginationResultsInfo.tsx

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
: 'accumulated.moreThanPageSize',

Check failure on line 22 in src/sections/shared/pagination/PaginationResultsInfo.tsx

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
[accumulated, paginationInfo.pageSize]
)

const formattedCount = useMemo(
() => new Intl.NumberFormat().format(paginationInfo.totalItems),
[paginationInfo.totalItems]
)

return (
<span className={styles.results}>
{typeof accumulated === 'number'
? t(defineLocale(accumulated), {
accumulated: accumulated,
count: paginationInfo.totalItems,
item: paginationInfo.itemName
})
accumulated: accumulated,

Check failure on line 35 in src/sections/shared/pagination/PaginationResultsInfo.tsx

View workflow job for this annotation

GitHub Actions / lint

Insert `··`
count: paginationInfo.totalItems,

Check failure on line 36 in src/sections/shared/pagination/PaginationResultsInfo.tsx

View workflow job for this annotation

GitHub Actions / lint

Insert `··`
formattedCount: formattedCount,

Check failure on line 37 in src/sections/shared/pagination/PaginationResultsInfo.tsx

View workflow job for this annotation

GitHub Actions / lint

Replace `··········` with `············`
item: paginationInfo.itemName

Check failure on line 38 in src/sections/shared/pagination/PaginationResultsInfo.tsx

View workflow job for this annotation

GitHub Actions / lint

Insert `··`
})

Check failure on line 39 in src/sections/shared/pagination/PaginationResultsInfo.tsx

View workflow job for this annotation

GitHub Actions / lint

Insert `··`
: t('results', {
start: paginationInfo.pageStartItem,
end: paginationInfo.pageEndItem,
item: paginationInfo.itemName,
count: paginationInfo.totalItems
})}
start: paginationInfo.pageStartItem,

Check failure on line 41 in src/sections/shared/pagination/PaginationResultsInfo.tsx

View workflow job for this annotation

GitHub Actions / lint

Insert `··`
end: paginationInfo.pageEndItem,

Check failure on line 42 in src/sections/shared/pagination/PaginationResultsInfo.tsx

View workflow job for this annotation

GitHub Actions / lint

Insert `··`
item: paginationInfo.itemName,

Check failure on line 43 in src/sections/shared/pagination/PaginationResultsInfo.tsx

View workflow job for this annotation

GitHub Actions / lint

Insert `··`
count: paginationInfo.totalItems,
formattedCount: formattedCount,
})}
</span>
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,17 @@ describe('PaginationResultsInfo', () => {
)
cy.findByText('10 of 15 Items seen').should('exist')
})
})

it('shows the correct formatted results when accumulated prop passed and results are more than page size', () => {
const totalCount = 1500
const expectedFormattedTotalCount = new Intl.NumberFormat().format(totalCount)

cy.customMount(
<PaginationResultsInfo
paginationInfo={new PaginationInfo<DatasetPaginationInfo>(1, 100, totalCount)}
accumulated={500}
/>
)
cy.findByText(`500 of ${expectedFormattedTotalCount} Items seen`).should('exist')
})
})

0 comments on commit 0feb0ae

Please sign in to comment.