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