|
1 | | -import React from 'react'; |
| 1 | +import {useEffect, useRef, useState} from 'react'; |
2 | 2 | import {Dropdown, Icon, Input} from 'semantic-ui-react'; |
3 | 3 |
|
4 | 4 | // http://githut.info/ |
@@ -28,121 +28,108 @@ const topProgramLan = [ |
28 | 28 | {id: 42, language: 'ActionScript'} |
29 | 29 | ]; |
30 | 30 |
|
31 | | -export default class SearchBar extends React.Component { |
32 | | - input = React.createRef(); |
33 | | - select = React.createRef(); |
| 31 | +export default function SearchBar(props) { |
| 32 | + const inputEl = useRef(null); |
| 33 | + const inputSize = useInputSize('huge'); |
| 34 | + const [state, setState] = useState({ |
| 35 | + lang: props && props.searchLang ? props.searchLang : [], |
| 36 | + valChanged: false |
| 37 | + }); |
34 | 38 |
|
35 | | - constructor(props) { |
36 | | - super(props); |
37 | | - this.state = { |
38 | | - lang: props && props.searchLang ? props.searchLang : [], |
39 | | - prevProps: props, |
40 | | - inputSize: 'huge', |
41 | | - inputChanged: false |
42 | | - } |
43 | | - window.addEventListener('resize', this.resizeInput, false) |
| 39 | + function updateState(vals) { |
| 40 | + setState(prevState => { |
| 41 | + return {...prevState, ...vals}; |
| 42 | + }); |
44 | 43 | } |
45 | 44 |
|
46 | | - static getDerivedStateFromProps(nextProps, prevState) { |
47 | | - // avoid calculating expensive derived data |
48 | | - if (prevState.prevProps === nextProps) { |
49 | | - return null |
50 | | - } |
51 | | - let newState = { |
52 | | - prevProps: nextProps // prevProps memoization |
53 | | - } |
54 | | - // derived state from props |
55 | | - if (prevState.prevProps.searchLang != nextProps.searchLang) { |
56 | | - newState.lang = nextProps.searchLang; |
57 | | - } |
58 | | - return newState; |
| 45 | + function handleSearch() { |
| 46 | + props.onSearch(inputEl.current.inputRef.value, state.lang); |
| 47 | + inputEl.current.inputRef.blur(); |
| 48 | + updateState({valChanged: false}); |
59 | 49 | } |
60 | 50 |
|
61 | | - componentDidMount() { |
62 | | - this.resizeInput(); |
| 51 | + function handleRestLang() { |
| 52 | + updateState({lang: [], valChanged: true}); |
63 | 53 | } |
64 | 54 |
|
65 | | - resizeInput = () => { |
66 | | - this.setState({inputSize: document.body.offsetWidth < 800 ? '' : 'huge'}) |
| 55 | + function handleSelectLang(id) { |
| 56 | + updateState({lang: state.lang.concat(id).sort(), valChanged: true}); |
67 | 57 | } |
68 | 58 |
|
69 | | - handleSearch = () => { |
70 | | - this.setState({inputChanged: false}); |
71 | | - this.props.onSearch(this.input.current.inputRef.value, this.state.lang); |
72 | | - this.input.current.inputRef.blur(); |
| 59 | + function handleDeselectLang(id) { |
| 60 | + let lang = state.lang; |
| 61 | + lang.splice(state.lang.indexOf(id), 1); |
| 62 | + updateState({lang: lang.sort(), valChanged: true}); |
73 | 63 | } |
74 | 64 |
|
75 | | - handleRestLang = () => { |
76 | | - this.setState({lang: [], inputChanged: true}); |
| 65 | + function handleToggleSelectLang(id) { |
| 66 | + state.lang.indexOf(id) === -1 ? handleSelectLang(id) : handleDeselectLang(id); |
77 | 67 | } |
78 | 68 |
|
79 | | - handleSelectLang = id => { |
80 | | - this.setState({lang: this.state.lang.concat(id).sort(), inputChanged: true}); |
81 | | - } |
| 69 | + const langItems = topProgramLan.map(key => { |
| 70 | + const active = state.lang.indexOf(key.id) !== -1; |
| 71 | + return <Dropdown.Item key={key.id} |
| 72 | + active={active} |
| 73 | + onClick={() => handleToggleSelectLang(key.id)}> |
| 74 | + <Icon name={active ? 'check circle outline' : 'circle outline'}/>{key.language} |
| 75 | + </Dropdown.Item>; |
| 76 | + }); |
82 | 77 |
|
83 | | - handleDeselectLang = id => { |
84 | | - let lang = this.state.lang; |
85 | | - lang.splice(this.state.lang.indexOf(id), 1); |
86 | | - this.setState({lang: lang.sort(), inputChanged: true}); |
87 | | - } |
| 78 | + return ( |
| 79 | + <div className='search-bar'> |
| 80 | + <div className='search-bar__desc'> |
| 81 | + Search over GitHub, Bitbucket, GitLab to find real-world usage variable names |
| 82 | + </div> |
| 83 | + <form action="javascript:void(0);"> |
| 84 | + <Input ref={inputEl} |
| 85 | + onChange={() => updateState({valChanged: true})} |
| 86 | + className='search-bar__input' |
| 87 | + icon fluid placeholder={props.placeholder} size={inputSize}> |
| 88 | + <Dropdown floating text='' icon='filter' className='search-bar__dropdown'> |
| 89 | + <Dropdown.Menu> |
| 90 | + <Dropdown.Item icon='undo' text='All 90 Languages (Reset)' onClick={handleRestLang}/> |
| 91 | + <Dropdown.Menu scrolling className='fix-dropdown-menu'> |
| 92 | + {langItems} |
| 93 | + </Dropdown.Menu> |
| 94 | + </Dropdown.Menu> |
| 95 | + </Dropdown> |
88 | 96 |
|
89 | | - handleToggleSelectLang = id => { |
90 | | - this.state.lang.indexOf(id) === -1 ? this.handleSelectLang(id) : this.handleDeselectLang(id); |
91 | | - } |
| 97 | + <input type='search' name='search' defaultValue={props.searchValue} |
| 98 | + onKeyPress={e => { |
| 99 | + e.key === 'Enter' && handleSearch() |
| 100 | + }}/> |
| 101 | + <Icon name={(props.variableList.length && !state.valChanged) ? 'search plus' : 'search'} |
| 102 | + link onClick={() => handleSearch()}/> |
| 103 | + </Input> |
| 104 | + </form> |
| 105 | + <div className='search-bar__plugins'> |
| 106 | + Extensions: |
| 107 | + <a href='https://github.com/unbug/codelf#codelf-for-vs-code' |
| 108 | + target='_blank' rel='noopener noreferrer'>VS Code</a>, |
| 109 | + <a className='text-muted' href='https://atom.io/packages/codelf' |
| 110 | + target='_blank' rel='noopener noreferrer'>Atom</a>, |
| 111 | + <a className='text-muted' href='https://github.com/unbug/codelf#codelf-for-sublime-text' |
| 112 | + target='_blank' rel='noopener noreferrer'>Sublime</a>, |
| 113 | + <a href='https://github.com/unbug/codelf/issues/24' |
| 114 | + target='_blank' rel='noopener noreferrer'>WebStorm</a>, |
| 115 | + <a href='https://github.com/unbug/codelf/issues/63' |
| 116 | + target='_blank' rel='noopener noreferrer'>Alfred</a> |
| 117 | + </div> |
| 118 | + </div> |
| 119 | + ) |
| 120 | +} |
92 | 121 |
|
93 | | - renderItems() { |
94 | | - return topProgramLan.map(key => { |
95 | | - const active = this.state.lang.indexOf(key.id) !== -1; |
96 | | - return <Dropdown.Item key={key.id} |
97 | | - active={active} |
98 | | - onClick={() => this.handleToggleSelectLang(key.id)}> |
99 | | - <Icon name={active ? 'check circle outline' : 'circle outline'}/>{key.language} |
100 | | - </Dropdown.Item>; |
101 | | - }) |
102 | | - } |
| 122 | +function useInputSize(val) { |
| 123 | + const [size, setSize] = useState(val); |
103 | 124 |
|
104 | | - render() { |
105 | | - return ( |
106 | | - <div className='search-bar'> |
107 | | - <div className='search-bar__desc'> |
108 | | - Search over GitHub, Bitbucket, GitLab to find real-world usage variable names |
109 | | - </div> |
110 | | - <form action="javascript:void(0);"> |
111 | | - <Input ref={this.input} |
112 | | - onChange={() => this.setState({inputChanged: true})} |
113 | | - className='search-bar__input' |
114 | | - icon fluid placeholder={this.props.placeholder} size={this.state.inputSize}> |
115 | | - <Dropdown floating text='' icon='filter' className='search-bar__dropdown'> |
116 | | - <Dropdown.Menu> |
117 | | - <Dropdown.Item icon='undo' text='All 90 Languages (Reset)' onClick={this.handleRestLang}/> |
118 | | - <Dropdown.Menu scrolling className='fix-dropdown-menu'> |
119 | | - {this.renderItems()} |
120 | | - </Dropdown.Menu> |
121 | | - </Dropdown.Menu> |
122 | | - </Dropdown> |
| 125 | + useEffect(() => { |
| 126 | + window.addEventListener('resize', resizeInput, false); |
| 127 | + return () => window.removeEventListener('resize', resizeInput, false); |
| 128 | + }, []);// run an effect and clean it up only once (on mount and unmount), you can pass an empty array ([]) |
123 | 129 |
|
124 | | - <input type='search' name='search' defaultValue={this.props.searchValue} |
125 | | - onKeyPress={e => { |
126 | | - e.key === 'Enter' && this.handleSearch() |
127 | | - }}/> |
128 | | - <Icon name={(this.props.variableList.length && !this.state.inputChanged) ? 'search plus' : 'search'} |
129 | | - link onClick={() => this.handleSearch()}/> |
130 | | - </Input> |
131 | | - </form> |
132 | | - <div className='search-bar__plugins'> |
133 | | - Extensions: |
134 | | - <a href='https://github.com/unbug/codelf#codelf-for-vs-code' |
135 | | - target='_blank' rel='noopener noreferrer'>VS Code</a>, |
136 | | - <a className='text-muted' href='https://atom.io/packages/codelf' |
137 | | - target='_blank' rel='noopener noreferrer'>Atom</a>, |
138 | | - <a className='text-muted' href='https://github.com/unbug/codelf#codelf-for-sublime-text' |
139 | | - target='_blank' rel='noopener noreferrer'>Sublime</a>, |
140 | | - <a href='https://github.com/unbug/codelf/issues/24' |
141 | | - target='_blank' rel='noopener noreferrer'>WebStorm</a>, |
142 | | - <a href='https://github.com/unbug/codelf/issues/63' |
143 | | - target='_blank' rel='noopener noreferrer'>Alfred</a> |
144 | | - </div> |
145 | | - </div> |
146 | | - ) |
| 130 | + function resizeInput() { |
| 131 | + setSize(document.body.offsetWidth < 800 ? '' : val); |
147 | 132 | } |
| 133 | + |
| 134 | + return size; |
148 | 135 | } |
0 commit comments