Skip to content

Commit 95c167f

Browse files
authored
Merge pull request #2431 from VarunMotiyani/main
changed the searchbar to functional
2 parents 7c50147 + 838071d commit 95c167f

File tree

1 file changed

+47
-53
lines changed

1 file changed

+47
-53
lines changed

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

Lines changed: 47 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,65 @@
1+
import React, { useState, useCallback, useEffect } from 'react';
12
import PropTypes from 'prop-types';
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-
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);
1616

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

21-
handleResetSearch = () => {
22-
this.setState({ searchValue: '' }, () => {
23-
this.props.resetSearchTerm();
24-
});
24+
const handleResetSearch = () => {
25+
setSearchValue('');
26+
resetSearchTerm();
2527
};
2628

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());
2933
};
3034

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]);
3638

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

7064
Searchbar.propTypes = {
7165
searchTerm: PropTypes.string.isRequired,

0 commit comments

Comments
 (0)