-
Notifications
You must be signed in to change notification settings - Fork 12
/
SortButton.jsx
187 lines (164 loc) · 5.15 KB
/
SortButton.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import React from 'react';
import { withTranslation } from 'react-i18next';
import {
Popper, MenuItem, MenuList, Grow, Paper, ClickAwayListener, Tooltip,
Chip,
} from '@mui/material'
import {
ArrowDropDown as ArrowDropDownIcon,
ArrowDownward as ArrowDownwardIcon,
ArrowUpward as ArrowUpwardIcon,
} from '@mui/icons-material'
import { map, startCase, get, includes } from 'lodash';
import { SORT_ATTRS } from './ResultConstants'
const ASC = 'asc';
const DESC = 'desc';
const SORT_ICON_STYLES = {width: '14px', height: '14px'};
class SortButton extends React.Component {
constructor(props) {
super(props);
this.state = {
open: false,
selectedOption: 'last_update',
sortBy: 'desc',
}
this.anchorRef = React.createRef(null);
}
componentDidMount() {
this.setState({
selectedOption: this.props.sortOn || this.state.selectedOption,
sortBy: this.props.sortBy || this.state.sortBy
})
}
componentDidUpdate(prevProps) {
if(
prevProps.sortOn !== this.props.sortOn &&
this.props.sortOn &&
(this.props.sortOn !== this.state.selectedOption ||
this.props.sortBy !== this.state.sortBy)
)
this.setState({
selectedOption: this.props.sortOn,
sortBy: this.props.sortBy || this.state.sortBy
})
}
getOptions() {
const { resource } = this.props;
const sortables = get(SORT_ATTRS, resource) || []
return map(sortables, attr => ({name: this.formatOptionName(attr), id: attr}))
}
isAsc() {
return this.state.sortBy === ASC;
}
isDesc() {
return this.state.sortBy === DESC;
}
toggleOrder = () => {
const sortBy = this.isAsc() ? DESC : ASC;
if(sortBy !== this.state.sortBy)
this.setState({sortBy: sortBy }, this.propagate)
}
toggleOpen = () => {
this.setState({open: !this.state.open})
}
setSelectedOption = value => {
if(value !== this.state.selectedOption)
this.setState({selectedOption: value}, this.propagate)
}
handleMenuItemClick = value => {
if(includes(['name', 'username', 'numeric_id'], value) && value !== this.state.selectedOption)
this.setState({sortBy: ASC, selectedOption: value}, this.propagate)
else
this.setSelectedOption(value || 'score');
this.toggleOpen();
};
toQueryParams() {
const { selectedOption } = this.state;
return this.isAsc() ? {sortAsc: selectedOption} : {sortDesc: selectedOption}
}
propagate() {
this.props.onChange(this.toQueryParams())
}
handleClose = event => {
if (this.anchorRef.current && this.anchorRef.current.contains(event.target)) {
return;
}
this.toggleOpen()
};
handleClick = () => {
this.toggleOrder();
}
getSelectedOptionName() {
const { selectedOption } = this.state;
return selectedOption === 'score' ? t('search.best_match') : startCase(selectedOption)
}
formatOptionName(option) {
const { t } = this.props
if(option === 'score')
return t('search.best_match')
if(option === 'id')
return t('search.id')
if(option === 'numeric_id')
return t('search.numeric_id')
return startCase(option)
}
render() {
const { open, selectedOption } = this.state;
const { size } = this.props;
const isAsc = this.isAsc();
const selectedOptionName = this.getSelectedOptionName()
const options = this.getOptions()
return (
<span>
<Tooltip arrow title='Sort By'>
<Chip
ref={this.anchorRef}
variant="outlined"
icon={
isAsc ?
<ArrowUpwardIcon fontSize="inherit" style={SORT_ICON_STYLES} /> :
<ArrowDownwardIcon fontSize="inherit" style={SORT_ICON_STYLES} />
}
color="primary"
label={selectedOptionName}
onClick={this.handleClick}
size={size || 'medium'}
deleteIcon={<ArrowDropDownIcon fontSize="inherit" />}
onDelete={this.toggleOpen}
/>
</Tooltip>
<Popper open={open} anchorEl={this.anchorRef.current} transition style={{zIndex: 1000}}>
{({ TransitionProps, placement }) => (
<Grow
{...TransitionProps}
style={{
transformOrigin: placement === 'bottom' ? 'center top' : 'center bottom',
zIndex: '1000'
}}
>
<Paper>
<ClickAwayListener onClickAway={this.handleClose}>
<MenuList id="split-button-menu">
{
map(options, (option, index) => (
<MenuItem
id={option.id}
key={index}
selected={option.id === selectedOption}
onClick={() => this.handleMenuItemClick(option.id)}
>
{option.name}
</MenuItem>
))
}
</MenuList>
</ClickAwayListener>
</Paper>
</Grow>
)}
</Popper>
</span>
)
}
}
export default withTranslation('translations')(SortButton);