|
| 1 | +import React, { useState, useCallback, useEffect } from 'react'; |
1 | 2 | import PropTypes from 'prop-types'; |
2 | | -import React from 'react'; |
3 | 3 | import { throttle } from 'lodash'; |
4 | 4 | import { withTranslation } from 'react-i18next'; |
5 | 5 | import i18next from 'i18next'; |
6 | 6 | import SearchIcon from '../../../../images/magnifyingglass.svg'; |
7 | 7 |
|
8 | | -class Searchbar extends React.Component { |
9 | | - constructor(props) { |
10 | | - super(props); |
11 | | - this.state = { |
12 | | - searchValue: this.props.searchTerm |
13 | | - }; |
14 | | - this.throttledSearchChange = throttle(this.searchChange, 500); |
15 | | - } |
| 8 | +const Searchbar = ({ |
| 9 | + searchTerm, |
| 10 | + setSearchTerm, |
| 11 | + resetSearchTerm, |
| 12 | + searchLabel, |
| 13 | + t |
| 14 | +}) => { |
| 15 | + const [searchValue, setSearchValue] = useState(searchTerm); |
16 | 16 |
|
17 | | - componentWillUnmount() { |
18 | | - this.props.resetSearchTerm(); |
19 | | - } |
| 17 | + const throttledSearchChange = useCallback( |
| 18 | + throttle((value) => { |
| 19 | + setSearchTerm(value.trim()); |
| 20 | + }, 500), |
| 21 | + [] |
| 22 | + ); |
20 | 23 |
|
21 | | - handleResetSearch = () => { |
22 | | - this.setState({ searchValue: '' }, () => { |
23 | | - this.props.resetSearchTerm(); |
24 | | - }); |
| 24 | + const handleResetSearch = () => { |
| 25 | + setSearchValue(''); |
| 26 | + resetSearchTerm(); |
25 | 27 | }; |
26 | 28 |
|
27 | | - searchChange = () => { |
28 | | - this.props.setSearchTerm(this.state.searchValue.trim()); |
| 29 | + const handleSearchChange = (e) => { |
| 30 | + const { value } = e.target; |
| 31 | + setSearchValue(value); |
| 32 | + throttledSearchChange(value.trim()); |
29 | 33 | }; |
30 | 34 |
|
31 | | - handleSearchChange = (e) => { |
32 | | - this.setState({ searchValue: e.target.value }, () => { |
33 | | - this.throttledSearchChange(this.state.searchValue.trim()); |
34 | | - }); |
35 | | - }; |
| 35 | + useEffect(() => { |
| 36 | + setSearchValue(searchTerm); |
| 37 | + }, [searchTerm]); |
36 | 38 |
|
37 | | - render() { |
38 | | - const { searchValue } = this.state; |
39 | | - return ( |
40 | | - <div |
41 | | - className={`searchbar ${ |
42 | | - searchValue === '' ? 'searchbar--is-empty' : '' |
43 | | - }`} |
44 | | - > |
45 | | - <div className="searchbar__button"> |
46 | | - <SearchIcon |
47 | | - className="searchbar__icon" |
48 | | - focusable="false" |
49 | | - aria-hidden="true" |
50 | | - /> |
51 | | - </div> |
52 | | - <input |
53 | | - className="searchbar__input" |
54 | | - type="text" |
55 | | - value={searchValue} |
56 | | - placeholder={this.props.searchLabel} |
57 | | - onChange={this.handleSearchChange} |
| 39 | + return ( |
| 40 | + <div |
| 41 | + className={`searchbar ${searchValue === '' ? 'searchbar--is-empty' : ''}`} |
| 42 | + > |
| 43 | + <div className="searchbar__button"> |
| 44 | + <SearchIcon |
| 45 | + className="searchbar__icon" |
| 46 | + focusable="false" |
| 47 | + aria-hidden="true" |
58 | 48 | /> |
59 | | - <button |
60 | | - className="searchbar__clear-button" |
61 | | - onClick={this.handleResetSearch} |
62 | | - > |
63 | | - {this.props.t('Searchbar.ClearTerm')} |
64 | | - </button> |
65 | 49 | </div> |
66 | | - ); |
67 | | - } |
68 | | -} |
| 50 | + <input |
| 51 | + className="searchbar__input" |
| 52 | + type="text" |
| 53 | + value={searchValue} |
| 54 | + placeholder={searchLabel} |
| 55 | + onChange={handleSearchChange} |
| 56 | + /> |
| 57 | + <button className="searchbar__clear-button" onClick={handleResetSearch}> |
| 58 | + {t('Searchbar.ClearTerm')} |
| 59 | + </button> |
| 60 | + </div> |
| 61 | + ); |
| 62 | +}; |
69 | 63 |
|
70 | 64 | Searchbar.propTypes = { |
71 | 65 | searchTerm: PropTypes.string.isRequired, |
|
0 commit comments