Skip to content

Commit 225b745

Browse files
committed
Convert Searchbar.jsx to a functional component
1 parent 7d774d1 commit 225b745

File tree

1 file changed

+48
-53
lines changed

1 file changed

+48
-53
lines changed

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

Lines changed: 48 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,66 @@
11
import PropTypes from 'prop-types';
2-
import React from 'react';
2+
import React, { useState, useEffect, useCallback } 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-
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 = i18next.t('Searchbar.SearchSketch'),
13+
t
14+
}) => {
15+
const [searchValue, setSearchValue] = useState(searchTerm);
1616

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

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

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

31-
handleSearchChange = (e) => {
32-
this.setState({ searchValue: e.target.value }, () => {
33-
this.throttledSearchChange(this.state.searchValue.trim());
34-
});
34+
const handleSearchChange = (e) => {
35+
const { value } = e.target;
36+
setSearchValue(value);
37+
throttledSearchChange(value.trim());
3538
};
3639

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}
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"
5849
/>
59-
<button
60-
className="searchbar__clear-button"
61-
onClick={this.handleResetSearch}
62-
>
63-
{this.props.t('Searchbar.ClearTerm')}
64-
</button>
6550
</div>
66-
);
67-
}
68-
}
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+
};
6964

7065
Searchbar.propTypes = {
7166
searchTerm: PropTypes.string.isRequired,

0 commit comments

Comments
 (0)