const Pagination = props => {
log('Pagination', LEVELS.INFO, props)
const { labels, components } = useContext(ConfigContext)
const { state, pageSizes } = props
const { paginationExtra } = components
const { pageIndex, pageSize, pageCount, total } = state
const dispatch = useContext(TableDispatch)
const pageSizeOptions = useMemo(
() =>
pageSizes.map(value => ({
value,
label: subst({ tpl: labels.rows, data: { value } })
})),
[labels, pageSizes]
)
const canPreviousPage = pageIndex > 0
const canNextPage = pageIndex < pageCount - 1
const message =
total > 0
? subst({
tpl: labels.range,
data: {
first: pageIndex * pageSize + 1,
last: Math.min(total, (pageIndex + 1) * pageSize),
total
}
})
: labels.noData
return (
<div className='rrt-pagination-info'>
{paginationExtra
? React.createElement(paginationExtra.type, paginationExtra.props)
: null}
<div className='rrt-pagination-pages'>
<button
title={labels.firstPage}
onClick={() => dispatch({ type: PAGING, pageIndex: 0, pageSize })}
disabled={!canPreviousPage}
>
<Icon icon='first' />
</button>
<button
title={labels.previousPage}
onClick={() =>
dispatch({ type: PAGING, pageIndex: pageIndex - 1, pageSize })}
disabled={!canPreviousPage}
>
<Icon icon='previous' />
</button>
<div className='rrt-pagination-of'>
<span>{labels.page}</span>
<input
type='number'
onChange={e => {
const value = e.target.value ? Number(e.target.value) : 1
const page = Math.min(Math.max(1, value), pageCount) - 1
dispatch({ type: PAGING, pageIndex: page, pageSize })
}}
className='rrt-pagination-input'
value={pageIndex + 1}
/>{' '}
<span>
{subst({ tpl: labels.ofPages, data: { pages: pageCount || 1 } })}
</span>
</div>
<button
title={labels.nextPage}
onClick={() =>
dispatch({ type: PAGING, pageIndex: pageIndex + 1, pageSize })}
disabled={!canNextPage}
>
<Icon icon='next' />
</button>
<button
title={labels.lastPage}
onClick={() =>
dispatch({ type: PAGING, pageIndex: pageCount - 1, pageSize })}
disabled={!canNextPage}
>
<Icon icon='last' />
</button>
</div>
<select
value={pageSize}
onChange={event => {
dispatch({ type: PAGING, pageIndex, pageSize: event.target.value })
}}
>
{pageSizeOptions.map(({ value, label }, index) => (
<option key={index} value={value}>
{label}
</option>
))}
</select>
<span className='rrt-pagination-view' title={message}>
{message}
</span>
</div>
)
}
Pagination.propTypes = {
Copyright 2020 Ulrich Gaal
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.