-
Notifications
You must be signed in to change notification settings - Fork 964
Expand file tree
/
Copy pathSearchBar.js
More file actions
156 lines (138 loc) · 5.46 KB
/
SearchBar.js
File metadata and controls
156 lines (138 loc) · 5.46 KB
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
import React, { useEffect, useRef, useState } from 'react';
import { Dropdown, Icon, Input } from 'semantic-ui-react';
import SearchEngineSelector from './SearchEngineSelector';
import { SEARCH_ENGINES } from '../constants/Configs';
// http://githut.info/
const topProgramLan = [
{ id: '22,106', language: 'JavaScript, CoffeeScript' },
{ id: '133,135', language: 'CSS' },
{ id: '3,39', language: 'HTML' },
{ id: 137, language: 'Swift' },
{ id: 35, language: 'Objective-C' },
{ id: 23, language: 'Java' },
{ id: 19, language: 'Python' },
{ id: 24, language: 'PHP' },
{ id: 32, language: 'Ruby' },
{ id: 28, language: 'C' },
{ id: 16, language: 'C++' },
{ id: 6, language: 'C#' },
{ id: 55, language: 'Go' },
{ id: 51, language: 'Perl' },
{ id: '104,109', language: 'Clojure, ClojureScript' },
{ id: 40, language: 'Haskell' },
{ id: 54, language: 'Lua' },
{ id: 20, language: 'Matlab' },
{ id: 144, language: 'R' },
{ id: 47, language: 'Scala' },
{ id: '69,78,146', language: 'Shell' },
{ id: 29, language: 'Lisp' },
{ id: 42, language: 'ActionScript' }
];
export default function SearchBar(props) {
const inputEl = useRef(null);
const inputSize = useInputSize('huge');
const [state, setState] = useState({
lang: props.searchLang || [],
searchEngine: props.searchEngine || SEARCH_ENGINES.SEARCHCODE,
valChanged: false
});
function updateState(vals) {
setState(prevState => {
return { ...prevState, ...vals };
});
}
function handleSearch() {
props.onSearch(inputEl.current.inputRef.current.value, state.lang, state.searchEngine);
inputEl.current.inputRef.current.blur();
updateState({ valChanged: false });
}
function handleRestLang() {
updateState({ lang: [], valChanged: true });
}
function handleSelectLang(id) {
updateState({ lang: state.lang.concat(id).sort(), valChanged: true });
}
function handleDeselectLang(id) {
let lang = state.lang;
lang.splice(state.lang.indexOf(id), 1);
updateState({ lang: lang.sort(), valChanged: true });
}
function handleToggleSelectLang(id) {
state.lang.indexOf(id) === -1 ? handleSelectLang(id) : handleDeselectLang(id);
}
function handleSearchEngineChange(engine) {
updateState({ searchEngine: engine, valChanged: true });
props.onSearchEngineChange && props.onSearchEngineChange(engine);
}
const langItems = topProgramLan.map(key => {
const active = state.lang.indexOf(key.id) !== -1;
return <Dropdown.Item key={key.id}
active={active}
onClick={() => handleToggleSelectLang(key.id)}>
<Icon name={active ? 'check circle outline' : 'circle outline'} />{key.language}
</Dropdown.Item>;
});
return (
<div className='search-bar'>
<div className='search-bar__desc'>
Search over GitHub, Bitbucket, GitLab to find real-world usage variable names
</div>
{/* Search Engine Selector */}
<div className='search-bar__engine-selector'>
<SearchEngineSelector
currentEngine={state.searchEngine}
onSearchEngineChange={handleSearchEngineChange}
/>
</div>
<form action="javascript:void(0);">
<Input ref={inputEl}
onChange={() => updateState({ valChanged: true })}
className='search-bar__input'
icon fluid placeholder={props.placeholder} size={inputSize}>
<Dropdown floating text='' icon='filter' className='search-bar__dropdown'>
<Dropdown.Menu>
<Dropdown.Item icon='undo' text='All 90 Languages (Reset)' onClick={handleRestLang} />
<Dropdown.Menu scrolling className='fix-dropdown-menu'>
{langItems}
</Dropdown.Menu>
</Dropdown.Menu>
</Dropdown>
<input type='search' name='search' defaultValue={props.searchValue} list='search-data-list'
onKeyPress={e => {
e.key === 'Enter' && handleSearch()
}} />
<Icon name={(props.variableList.length && !state.valChanged) ? 'search plus' : 'search'}
link onClick={handleSearch} />
<datalist id='search-data-list'>
{props.luckyKeyWords.map((item, i) => <option value={item} key={i} />)}
</datalist>
</Input>
</form>
<div className='search-bar__plugins'>
Extensions:
<a href='https://github.com/unbug/codelf#codelf-for-vs-code'
target='_blank' rel='noopener noreferrer'>VS Code</a>,
<a className='text-muted' href='https://atom.io/packages/codelf'
target='_blank' rel='noopener noreferrer'>Atom</a>,
<a className='text-muted' href='https://github.com/unbug/codelf#codelf-for-sublime-text'
target='_blank' rel='noopener noreferrer'>Sublime</a>,
<a href='https://github.com/unbug/codelf/issues/24'
target='_blank' rel='noopener noreferrer'>WebStorm</a>,
<a href='https://github.com/unbug/codelf/issues/63'
target='_blank' rel='noopener noreferrer'>Alfred</a>
</div>
</div>
)
}
function useInputSize(val) {
const [size, setSize] = useState(val);
useEffect(() => {
resizeInput();
window.addEventListener('resize', resizeInput, false);
return () => window.removeEventListener('resize', resizeInput, false);
}, []);// run an effect and clean it up only once (on mount and unmount), you can pass an empty array ([])
function resizeInput() {
setSize(document.body.offsetWidth < 800 ? '' : val);
}
return size;
}