/
SearchInput.jsx
168 lines (151 loc) · 4.55 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
import 'core-js/features/url-search-params';
import React from 'react';
import {
Search as SearchIcon,
Clear as ClearIcon,
} from '@mui/icons-material';
import { InputBase, IconButton, Tooltip } from '@mui/material';
import { get } from 'lodash';
import { isAtGlobalSearch, getSiteTitle } from '../../common/utils';
import { withTranslation } from 'react-i18next';
class SearchInput extends React.Component {
constructor(props){
super(props);
this.inputRef = React.createRef();
this.state = {
siteTitle: getSiteTitle(),
input: undefined,
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')
})
}
componentDidUpdate(prevProps) {
const currentSearchStrFromURL = this.getValueFromURL('q')
const prevSearchStrFromURL = this.getValueFromURL('q', '', prevProps)
if(
(currentSearchStrFromURL !== prevSearchStrFromURL) ||
(currentSearchStrFromURL && this.state.input === undefined)
)
this.setState({input: currentSearchStrFromURL})
}
handleKeyPress = event => {
if (event.key === 'Enter') {
event.preventDefault();
event.stopPropagation();
this.performSearch()
}
return false
}
performSearch = event => {
if(event) {
event.preventDefault()
event.stopPropagation()
}
const { input } = this.state
if(this.props.onSearch)
this.props.onSearch(input)
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 } = this.state
let _input = input || '';
const queryParams = new URLSearchParams(window.location.hash.split('?')[1])
const resourceType = queryParams.get('type')
let URL = '/search/'
if(!_input) {
setTimeout(() => {
URL = '/search/'
if(resourceType)
URL += `?type=${resourceType}`
window.location.hash = URL
}, 500)
}
else {
if(this.props.location.pathname === '/search/') {
queryParams.set('q', _input)
URL += `?${queryParams.toString()}`
} else {
URL +=`?q=${_input}`;
if(resourceType)
URL += `&type=${resourceType}`
}
window.location.hash = URL
}
}
}
render() {
const { input, siteTitle } = this.state
const { searchInputPlaceholder, nested, t } = 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 || `${t('search.label')} ${siteTitle}`}
inputProps={{ 'aria-label': 'search ocl' }}
value={input || ''}
fullWidth
onChange={this.handleInputChange}
onKeyPress={this.handleKeyPress}
inputRef={this.inputRef}
/>
{
input &&
<Tooltip arrow title={t('common.clear')}>
<IconButton
type="submit"
style={{padding: '10px'}}
aria-label="clear"
onClick={this.clearSearch}
size="large">
<ClearIcon />
</IconButton>
</Tooltip>
}
<Tooltip arrow title={t('search.label')}>
<IconButton
type="submit"
style={{padding: '10px'}}
aria-label="search"
onClick={this.performSearch}
size="large">
<SearchIcon />
</IconButton>
</Tooltip>
</div>
);
}
}
export default withTranslation('translations')(SearchInput);