|
1 |
| -import React from 'react' |
| 1 | +import React, { useState, useEffect, useRef } from 'react' |
2 | 2 | import PropTypes from 'prop-types'
|
3 | 3 | import ClearIcon from '@mui/icons-material/Clear'
|
| 4 | +import KeyboardCommandKeyIcon from '@mui/icons-material/KeyboardCommandKey' |
| 5 | +import hotkeys from 'hotkeys-js' |
4 | 6 |
|
5 | 7 | import { debounce } from '@core/utils'
|
6 | 8 |
|
7 | 9 | import './representatives-search.styl'
|
8 | 10 |
|
9 |
| -export default class RepresentativesSearch extends React.Component { |
10 |
| - constructor(props) { |
11 |
| - super(props) |
| 11 | +const RepresentativesSearch = ({ |
| 12 | + value: initialValue, |
| 13 | + search, |
| 14 | + align = 'center' |
| 15 | +}) => { |
| 16 | + const [value, setValue] = useState(initialValue || '') |
| 17 | + const inputRef = useRef(null) |
12 | 18 |
|
13 |
| - this.state = { |
14 |
| - value: this.props.value || '' |
| 19 | + const debouncedSearch = debounce((value) => { |
| 20 | + search(value) |
| 21 | + }, 300) |
| 22 | + |
| 23 | + useEffect(() => { |
| 24 | + const handleHotkeys = (event, handler) => { |
| 25 | + event.preventDefault() |
| 26 | + inputRef.current.focus() |
15 | 27 | }
|
16 | 28 |
|
17 |
| - this.search = debounce((value) => { |
18 |
| - this.props.search(value) |
19 |
| - }, 300) |
20 |
| - } |
| 29 | + hotkeys('command+k,ctrl+k', handleHotkeys) |
21 | 30 |
|
22 |
| - handleClick = () => { |
23 |
| - const value = '' |
24 |
| - this.setState({ value }) |
25 |
| - this.props.search(value) |
| 31 | + return () => { |
| 32 | + hotkeys.unbind('command+k,ctrl+k') |
| 33 | + } |
| 34 | + }, []) |
| 35 | + |
| 36 | + const handleClick = () => { |
| 37 | + setValue('') |
| 38 | + search('') |
26 | 39 | }
|
27 | 40 |
|
28 |
| - handleChange = (event) => { |
| 41 | + const handleChange = (event) => { |
29 | 42 | const { value } = event.target
|
30 |
| - this.setState({ value }) |
31 |
| - this.search(value) |
| 43 | + setValue(value) |
| 44 | + debouncedSearch(value) |
32 | 45 | }
|
33 | 46 |
|
34 |
| - render = () => { |
35 |
| - return ( |
36 |
| - <div className='representatives__search'> |
37 |
| - <input |
38 |
| - className='search__input' |
39 |
| - type='text' |
40 |
| - placeholder='Filter by account, alias, ip' |
41 |
| - value={this.state.value} |
42 |
| - onChange={this.handleChange} |
43 |
| - /> |
44 |
| - {this.state.value && ( |
45 |
| - <div className='search__input-clear' onClick={this.handleClick}> |
46 |
| - <ClearIcon /> |
47 |
| - </div> |
48 |
| - )} |
49 |
| - </div> |
50 |
| - ) |
| 47 | + const classNames = ['representatives__search'] |
| 48 | + |
| 49 | + if (align === 'left') { |
| 50 | + classNames.push('left') |
| 51 | + } else { |
| 52 | + classNames.push('center') |
51 | 53 | }
|
| 54 | + |
| 55 | + return ( |
| 56 | + <div className={classNames.join(' ')}> |
| 57 | + <input |
| 58 | + ref={inputRef} |
| 59 | + className='search__input' |
| 60 | + type='text' |
| 61 | + placeholder='Filter by account, alias, ip' |
| 62 | + value={value} |
| 63 | + onChange={handleChange} |
| 64 | + /> |
| 65 | + <div className='search__shortcut-icon'> |
| 66 | + <KeyboardCommandKeyIcon fontSize='small' />K |
| 67 | + </div> |
| 68 | + {value && ( |
| 69 | + <div className='search__input-clear' onClick={handleClick}> |
| 70 | + <ClearIcon /> |
| 71 | + </div> |
| 72 | + )} |
| 73 | + </div> |
| 74 | + ) |
52 | 75 | }
|
53 | 76 |
|
54 | 77 | RepresentativesSearch.propTypes = {
|
55 | 78 | value: PropTypes.string,
|
56 |
| - search: PropTypes.func |
| 79 | + search: PropTypes.func, |
| 80 | + align: PropTypes.oneOf(['left', 'center']) |
57 | 81 | }
|
| 82 | + |
| 83 | +export default RepresentativesSearch |
0 commit comments