Skip to content

Commit 1f0b6c0

Browse files
committed
Revert "Convert Searchbar.jsx to a functional component"
This reverts commit 225b745.
1 parent 786052e commit 1f0b6c0

File tree

1 file changed

+53
-48
lines changed

1 file changed

+53
-48
lines changed

client/modules/IDE/components/Searchbar/Searchbar.jsx

Lines changed: 53 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,71 @@
11
import PropTypes from 'prop-types';
2-
import React, { useState, useEffect, useCallback } from 'react';
2+
import React from 'react';
33
import { throttle } from 'lodash';
44
import { withTranslation } from 'react-i18next';
55
import i18next from 'i18next';
66
import SearchIcon from '../../../../images/magnifyingglass.svg';
77

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+
}
1616

17-
const throttledSearchChange = useCallback(
18-
throttle((value) => setSearchTerm(value.trim()), 500),
19-
[setSearchTerm]
20-
);
17+
componentWillUnmount() {
18+
this.props.resetSearchTerm();
19+
}
2120

22-
useEffect(
23-
() => () => {
24-
resetSearchTerm();
25-
},
26-
[resetSearchTerm]
27-
);
21+
handleResetSearch = () => {
22+
this.setState({ searchValue: '' }, () => {
23+
this.props.resetSearchTerm();
24+
});
25+
};
2826

29-
const handleResetSearch = () => {
30-
setSearchValue('');
31-
resetSearchTerm();
27+
searchChange = () => {
28+
this.props.setSearchTerm(this.state.searchValue.trim());
3229
};
3330

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+
});
3835
};
3936

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}
4958
/>
59+
<button
60+
className="searchbar__clear-button"
61+
onClick={this.handleResetSearch}
62+
>
63+
{this.props.t('Searchbar.ClearTerm')}
64+
</button>
5065
</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+
}
6469

6570
Searchbar.propTypes = {
6671
searchTerm: PropTypes.string.isRequired,

0 commit comments

Comments
 (0)