Repository navigation
Expand file tree
/
Copy pathSearchBar.js
More file actions
139 lines (126 loc) · 4.58 KB
/
Copy pathSearchBar.js
File metadata and controls
139 lines (126 loc) · 4.58 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
import React from 'react';
import {Dropdown, Icon, Input} from 'semantic-ui-react';
// 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 class SearchBar extends React.Component {
input = React.createRef();
select = React.createRef();
constructor(props) {
super(props);
this.state = {
lang: [],
prevProps: props,
inputSize: 'huge'
}
window.addEventListener('resize', this.resizeInput, false)
}
static getDerivedStateFromProps(nextProps, prevState) {
// avoid calculating expensive derived data
if (prevState.prevProps === nextProps) {
return null
}
let newState = {
prevProps: nextProps // prevProps memoization
}
// derived state from props
if (prevState.prevProps.searchLang != nextProps.searchLang) {
newState.lang = nextProps.searchLang;
}
return newState;
}
componentDidMount() {
this.resizeInput();
}
resizeInput = () => {
this.setState({inputSize: document.body.offsetWidth < 800 ? '' : 'huge'})
}
handleSearch = () => {
this.props.onSearch(this.input.current.inputRef.value, this.state.lang);
}
handleRestLang = () => {
this.setState({lang: []})
}
handleSelectLang = id => {
this.setState({lang: this.state.lang.concat(id)})
}
handleDeselectLang = id => {
let lang = this.state.lang;
lang.splice(this.state.lang.indexOf(id), 1);
this.setState({lang: lang})
}
handleToggleSelectLang = id => {
this.state.lang.indexOf(id) === -1 ? this.handleSelectLang(id) : this.handleDeselectLang(id);
}
renderItems() {
return topProgramLan.map(key => {
const active = this.state.lang.indexOf(key.id) !== -1;
return <Dropdown.Item key={key.id}
active={active}
onClick={() => this.handleToggleSelectLang(key.id)}>
<Icon name={active ? 'check circle outline' : 'circle outline'}/>{key.language}
</Dropdown.Item>;
})
}
render() {
return (
<div className='search-bar'>
<div className='search-bar__desc'>
Search over GitHub, Bitbucket, GitLab to find real-world usage variable names
</div>
<Input ref={this.input}
className={`search-bar__input${this.props.variableList.length ? ' search-bar__input--dark' : ''}`}
icon fluid placeholder={this.props.placeholder} size={this.state.inputSize}>
<Dropdown floating text='' icon='filter' className='search-bar__dropdown'>
<Dropdown.Menu>
<Dropdown.Item icon='undo' text='All 90 Languages (Reset)' onClick={this.handleRestLang}/>
<Dropdown.Menu scrolling>
{this.renderItems()}
</Dropdown.Menu>
</Dropdown.Menu>
</Dropdown>
<input defaultValue={this.props.searchValue}
onKeyPress={e => {
e.key === 'Enter' && this.handleSearch()
}}/>
<Icon name={this.props.variableList.length ? 'search plus' : 'search'}
link onClick={() => this.handleSearch()}/>
</Input>
<div className='search-bar__plugins'>
<Icon name='plug'/>For:
<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 Text</a>,
<a href='https://chrome.google.com/webstore/detail/codelf-best-github-stars/jnmjaglhmmcplekpfnblniiammmdpaan'
target='_blank' rel='noopener noreferrer'>Chrome</a>
</div>
</div>
)
}
}