-
Notifications
You must be signed in to change notification settings - Fork 12
/
ResultsCountDropDown.jsx
121 lines (110 loc) · 3.52 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import React from 'react';
import {
Button, Popper, MenuItem, MenuList, Grow, Paper, ClickAwayListener, Tooltip, ButtonGroup,
} from '@material-ui/core'
import {
ArrowDropDown as ArrowDropDownIcon,
} from '@material-ui/icons'
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,
}
this.buttonRef = React.createRef(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 = () => {
this.setState({open: !this.state.open})
}
handleClose = event => {
if (this.buttonRef.current && this.buttonRef.current.contains(event.target)) {
return;
}
this.toggleOpen()
}
componentDidMount() {
this.setDefaultLimitFromParent()
}
componentDidUpdate() {
this.setDefaultLimitFromParent()
}
setDefaultLimitFromParent() {
const { defaultLimit } = this.props;
if(defaultLimit && defaultLimit !== this.state.limit)
this.setState({limit: defaultLimit})
}
render() {
const { total, size } = this.props;
const { limit, open } = this.state;
return (
<span>
<Tooltip title='Results Per Page'>
<ButtonGroup variant="outlined" color="primary" ref={this.buttonRef} aria-label="limit button" size={size || 'medium'}>
<Button style={{fontSize: '0.8125em'}} onClick={this.toggleOpen}>
Results Per Page : {limit}
</Button>
<Button
color="primary"
aria-controls={open ? 'limit-button-menu' : undefined}
aria-expanded={open ? 'true' : undefined}
aria-label="select merge strategy"
aria-haspopup="menu"
onClick={this.toggleOpen}
style={{padding: '0px', minWidth: '30px'}}
>
<ArrowDropDownIcon />
</Button>
</ButtonGroup>
</Tooltip>
<Popper open={open} anchorEl={this.buttonRef.current} transition disablePortal style={{zIndex: 1000}}>
{({ TransitionProps, placement }) => (
<Grow
{...TransitionProps}
style={{
transformOrigin: placement === 'bottom' ? 'center top' : 'center bottom',
zIndex: '1000'
}}
>
<Paper>
<ClickAwayListener onClickAway={this.handleClose}>
<MenuList id="limit-button-menu">
{
map(OPTIONS, option => (
<MenuItem
id={option.id}
key={option.id}
selected={option.count === limit}
onClick={() => this.onSetCount(option.count)}
disabled={option.count !== limit && option.count > total}
>
{option.id}
</MenuItem>
))
}
</MenuList>
</ClickAwayListener>
</Paper>
</Grow>
)}
</Popper>
</span>
)
}
}
export default ResultsCountDropDown;