-
Notifications
You must be signed in to change notification settings - Fork 12
/
ResultsCountDropDown.jsx
88 lines (78 loc) · 2.18 KB
/
ResultsCountDropDown.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
import React from 'react';
import { Tooltip, Chip, MenuItem, Menu } from '@mui/material'
import { FormatListNumberedRtl as NumberListIcon } from '@mui/icons-material'
import { map } from 'lodash';
import { DEFAULT_LIMIT } from '../../common/constants';
const OPTIONS = [
{id: '25', count: 25},
{id: '50', count: 50},
{id: '100', count: 100},
]
class ResultsCountDropDown extends React.Component {
constructor(props) {
super(props);
this.state = {
open: false,
limit: DEFAULT_LIMIT,
anchorEl: null,
}
}
onSetCount = limit => {
let _limit = parseInt(limit) || DEFAULT_LIMIT;
if(_limit !== this.state.limit)
this.setState({limit: _limit}, () => {
this.toggleOpen();
this.props.onChange(_limit);
})
}
toggleOpen = event => {
const newOpen = !this.state.open
this.setState({open: newOpen, anchorEl: newOpen ? event.currentTarget : null})
}
componentDidMount() {
this.setDefaultLimitFromParent()
}
componentDidUpdate() {
this.setDefaultLimitFromParent()
}
setDefaultLimitFromParent() {
const { defaultLimit } = this.props;
if(defaultLimit && defaultLimit !== this.state.limit)
this.setState({limit: defaultLimit})
}
render() {
const { size } = this.props;
const { limit, anchorEl } = this.state;
return (
<span>
<Tooltip arrow title='Results Per Page'>
<Chip
variant="outlined"
color='primary'
icon={<NumberListIcon fontSize='inherit' />}
label={`Page Size : ${limit}`}
onClick={this.toggleOpen}
size={size || 'medium'}
style={{minWidth: '80px'}}
/>
</Tooltip>
<Menu
id="results-size-menu"
anchorEl={anchorEl}
keepMounted
open={Boolean(anchorEl)}
onClose={this.toggleOpen}
>
{
map(OPTIONS, option => (
<MenuItem key={option.id} value={option.count} onClick={() => this.onSetCount(option.count)}>
{option.id}
</MenuItem>
))
}
</Menu>
</span>
)
}
}
export default ResultsCountDropDown;