-
Notifications
You must be signed in to change notification settings - Fork 13
/
SearchInput.jsx
186 lines (168 loc) · 5.46 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
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
import 'core-js/features/url-search-params';
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, getSiteTitle } from '../../common/utils';
class SearchInput extends React.Component {
constructor(props){
super(props);
this.inputRef = React.createRef();
this.state = {
siteTitle: getSiteTitle(),
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 || '')
}
_listenKey = event => {
const isCtrlS = event.keyCode === 83 && event.ctrlKey;
if (!isCtrlS) {
return;
}
this.inputRef.current.focus();
};
componentWillUnmount() {
document.body.removeEventListener("keydown", this._listenKey);
}
componentDidMount() {
document.body.addEventListener("keydown", this._listenKey);
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 = event => {
if(event) {
event.preventDefault()
event.stopPropagation()
}
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(!this.props.nested) {
const { input, exactMatch } = this.state
let _input = input || '';
const exactMatchStr = exactMatch === 'on' ? '&exactMatch=on' : '';
const queryParams = new URLSearchParams(window.location.hash.split('?')[1])
const resourceType = queryParams.get('type')
if(!_input)
setTimeout(() => window.location.hash = '/search/', 500)
else {
let URL =`/search/?q=${_input}${exactMatchStr}`;
if(resourceType)
URL += `&type=${resourceType}`
window.location.hash = URL
}
}
}
handleExactMatchChange = () => {
const isOn = this.state.exactMatch === 'on'
this.setState({exactMatch: isOn ? 'off' : 'on'}, this.performSearch)
}
render() {
const { input, exactMatch, siteTitle } = this.state
const { searchInputPlaceholder, nested, noExactMatch } = this.props
const marginBottom = (isAtGlobalSearch() || nested) ? '5px' : '0px';
return (
<div className='col-xs-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 ${siteTitle}`}
inputProps={{ 'aria-label': 'search ocl' }}
value={input || ''}
fullWidth
onChange={this.handleInputChange}
onKeyPress={this.handleKeyPress}
inputRef={this.inputRef}
/>
{
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>
);
}
}
export default SearchInput;