Skip to content

Moved Search bar to be a functional component #2509

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 48 additions & 55 deletions client/modules/IDE/components/Searchbar/Searchbar.jsx
Original file line number Diff line number Diff line change
@@ -1,71 +1,64 @@
import PropTypes from 'prop-types';
import React from 'react';
import React, { useState, useEffect, useCallback } from 'react';
import { throttle } from 'lodash';
import { withTranslation } from 'react-i18next';
import i18next from 'i18next';
import PropTypes from 'prop-types';
import SearchIcon from '../../../../images/magnifyingglass.svg';

class Searchbar extends React.Component {
constructor(props) {
super(props);
this.state = {
searchValue: this.props.searchTerm
};
this.throttledSearchChange = throttle(this.searchChange, 500);
}
const Searchbar = ({
searchTerm: initialSearchTerm,
setSearchTerm,
resetSearchTerm,
searchLabel,
t
}) => {
const [searchValue, setSearchValue] = useState(initialSearchTerm);

componentWillUnmount() {
this.props.resetSearchTerm();
}
const throttledSearchChange = useCallback(
throttle((value) => {
setSearchTerm(value.trim());
}, 500),
[setSearchTerm]
);

handleResetSearch = () => {
this.setState({ searchValue: '' }, () => {
this.props.resetSearchTerm();
});
useEffect(() => {
resetSearchTerm();
}, [resetSearchTerm]);
const handleResetSearch = () => {
setSearchValue('');
resetSearchTerm();
};

searchChange = () => {
this.props.setSearchTerm(this.state.searchValue.trim());
const handleSearchChange = (e) => {
const { value } = e.target;
setSearchValue(value);
throttledSearchChange(value);
};

handleSearchChange = (e) => {
this.setState({ searchValue: e.target.value }, () => {
this.throttledSearchChange(this.state.searchValue.trim());
});
};

render() {
const { searchValue } = this.state;
return (
<div
className={`searchbar ${
searchValue === '' ? 'searchbar--is-empty' : ''
}`}
>
<div className="searchbar__button">
<SearchIcon
className="searchbar__icon"
focusable="false"
aria-hidden="true"
/>
</div>
<input
className="searchbar__input"
type="text"
value={searchValue}
placeholder={this.props.searchLabel}
onChange={this.handleSearchChange}
return (
<div
className={`searchbar ${searchValue === '' ? 'searchbar--is-empty' : ''}`}
>
<div className="searchbar__button">
<SearchIcon
className="searchbar__icon"
focusable="false"
aria-hidden="true"
/>
<button
className="searchbar__clear-button"
onClick={this.handleResetSearch}
>
{this.props.t('Searchbar.ClearTerm')}
</button>
</div>
);
}
}
<input
className="searchbar__input"
type="text"
value={searchValue}
placeholder={searchLabel}
onChange={handleSearchChange}
/>
<button className="searchbar__clear-button" onClick={handleResetSearch}>
{t('Searchbar.ClearTerm')}
</button>
</div>
);
};

Searchbar.propTypes = {
searchTerm: PropTypes.string.isRequired,
Expand Down