-
Notifications
You must be signed in to change notification settings - Fork 12
/
SearchInput.jsx
160 lines (146 loc) · 4.83 KB
/
SearchInput.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
import React from 'react';
import {
Search as SearchIcon,
CenterFocusStrong as ExactMatchIcon,
Clear as ClearIcon,
} from '@mui/icons-material';
import { InputBase, Divider, IconButton, Tooltip } from '@mui/material';
import { get } from 'lodash';
import { isAtGlobalSearch } from '../../common/utils';
class SearchInput extends React.Component {
constructor(props){
super(props);
this.state = {
input: undefined,
exactMatch: 'off',
queryParams: {},
}
}
getValueFromURL(attr, defaultValue, props) {
if(!props)
props = this.props
const queryParams = new URLSearchParams(get(props, 'location.search', ''))
return queryParams.get(attr, defaultValue || '')
}
componentDidMount() {
this.setState({
input: this.getValueFromURL('q'), exactMatch: this.getValueFromURL('exactMatch')
})
}
componentDidUpdate(prevProps) {
const currentSearchStrFromURL = this.getValueFromURL('q')
const currentExactMatchFromURL = this.getValueFromURL('exactMatch')
const prevSearchStrFromURL = this.getValueFromURL('q', '', prevProps)
const prevExactMatchFromURL = this.getValueFromURL('exactMatch', '', prevProps)
if(
(currentSearchStrFromURL !== prevSearchStrFromURL) ||
(currentSearchStrFromURL && this.state.input === undefined)
)
this.setState({input: currentSearchStrFromURL})
if(
(currentExactMatchFromURL !== prevExactMatchFromURL) ||
(currentExactMatchFromURL && !this.state.exactMatch)
)
this.setState({exactMatch: currentExactMatchFromURL})
}
handleKeyPress = event => {
if (event.key === 'Enter') {
event.preventDefault();
event.stopPropagation();
this.performSearch()
}
return false
}
performSearch = () => {
const { input, exactMatch } = this.state
if(this.props.onSearch)
this.props.onSearch(input, exactMatch)
else
this.moveToSearchPage()
}
clearSearch = () => {
this.setState({input: ''}, this.performSearch)
}
handleInputChange = event => {
this.setState({input: event.target.value}, () => {
if(this.props.onChange)
this.props.onChange(this.state.input)
})
}
moveToSearchPage = () => {
if(!isAtGlobalSearch()) {
const { input, exactMatch } = this.state
let _input = input || '';
const exactMatchStr = exactMatch === 'on' ? '&exactMatch=on' : '';
const URL = `/search/?q=${_input}${exactMatchStr}`;
window.location.hash = URL;
}
}
handleExactMatchChange = () => {
const isOn = this.state.exactMatch === 'on'
this.setState({exactMatch: isOn ? 'off' : 'on'}, this.performSearch)
}
render() {
const { input, exactMatch } = this.state
const { moreControls, searchInputPlaceholder, nested, noExactMatch } = this.props
const marginBottom = (isAtGlobalSearch() || nested) ? '10px' : '0px';
return (
<div className='col-sm-12 no-side-padding'>
<div className='col-sm-12 no-side-padding' style={{marginBottom: marginBottom, display: 'flex', alignItems: 'center', border: '1px solid darkgray', borderRadius: '4px'}}>
<InputBase
style={{flex: 1, marginLeft: '10px'}}
placeholder={searchInputPlaceholder || "Search OCL"}
inputProps={{ 'aria-label': 'search ocl' }}
value={input || ''}
fullWidth
onChange={this.handleInputChange}
onKeyPress={this.handleKeyPress}
/>
{
input &&
<Tooltip arrow title='Clear'>
<IconButton
type="submit"
style={{padding: '10px'}}
aria-label="clear"
onClick={this.clearSearch}
size="large">
<ClearIcon />
</IconButton>
</Tooltip>
}
<Tooltip arrow title='Search'>
<IconButton
type="submit"
style={{padding: '10px'}}
aria-label="search"
onClick={this.performSearch}
size="large">
<SearchIcon />
</IconButton>
</Tooltip>
{
!noExactMatch &&
<React.Fragment>
<Divider style={{height: '28px', margin: '4px'}} orientation="vertical" />
<Tooltip arrow title='Exact Match'>
<IconButton
color={exactMatch === 'on' ? "primary" : "default"}
style={{padding: '10px'}}
aria-label="exact"
onClick={this.handleExactMatchChange}
size="large">
<ExactMatchIcon />
</IconButton>
</Tooltip>
</React.Fragment>
}
</div>
<div style={{textAlign: 'left'}}>
{moreControls}
</div>
</div>
);
}
}
export default SearchInput;