-
Notifications
You must be signed in to change notification settings - Fork 12
/
PageResultsLabel.jsx
91 lines (82 loc) · 2.83 KB
/
PageResultsLabel.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import React from 'react';
import { Chip, MenuItem, Popper, Paper, Grow, MenuList } from '@mui/material';
import { min, isNaN, get, map } from 'lodash';
import { DEFAULT_LIMIT } from '../../common/constants';
const OPTIONS = [
{id: '10', count: 10},
{id: '25', count: 25},
{id: '50', count: 50},
{id: '100', count: 100},
]
const PageResultsLabel = ({ results, limit, isInfinite, onChange, disabled }) => {
const [anchorEl, setAnchorEl] = React.useState(null);
const getPageRange = () => {
const total = get(results, 'total', 0)
let _limit = limit || DEFAULT_LIMIT;
if(total === 0)
return [0, 0]
const page = get(results, 'pageNumber') || 1
const resultsInCurrentPage = min([(total - (_limit * (page-1))), _limit])
let start = ((page - 1) * _limit) + 1;
let end = start + resultsInCurrentPage - 1;
if(isNaN(start)) start = 0;
if(isNaN(end)) end = 0;
if(isInfinite)
start = 1
return [start, end]
};
const pageRange = getPageRange();
const onSetCount = _limit => {
setAnchorEl(null);
_limit = parseInt(_limit) || DEFAULT_LIMIT;
onChange(_limit);
}
return (
<React.Fragment>
<Chip
disabled={disabled}
id='results-chip'
onMouseOver={event => setAnchorEl(event.currentTarget)}
onMouseOut={() => setAnchorEl(null)}
variant='outlined'
onClick={event => setAnchorEl(event.currentTarget)}
label={
<span style={{fontSize: '12px'}}>
<span style={{fontWeight: 'bold', paddingRight: '4px'}}>{`${pageRange[0]}-${pageRange[1]}`}</span>
<span style={{paddingRight: '4px'}}>of</span>
<span style={{fontWeight: 'bold', paddingRight: '4px'}}>{get(results, 'total', 0).toLocaleString()}</span>
</span>
}
/>
<Popper
open={Boolean(anchorEl)}
anchorEl={anchorEl}
role={undefined}
transition
disablePortal
style={{zIndex: 1}}
onMouseOver={() => anchorEl ? null : setAnchorEl(document.getElementById('results-chip'))}
onMouseOut={() => setAnchorEl(null)}>
{({ TransitionProps, placement }) => (
<Grow
{...TransitionProps}
style={{ transformOrigin: placement === 'bottom' ? 'center top' : 'center bottom' }}
>
<Paper>
<MenuList autoFocusItem={Boolean(anchorEl)} id="menu-list-grow">
{
map(OPTIONS, option => (
<MenuItem selected={limit === option.count} key={option.id} value={option.count} onClick={() => onSetCount(option.count)}>
{option.id}
</MenuItem>
))
}
</MenuList>
</Paper>
</Grow>
)}
</Popper>
</React.Fragment>
)
}
export default PageResultsLabel;