Skip to content
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
105 changes: 50 additions & 55 deletions client/modules/IDE/components/Searchbar/Searchbar.jsx
Original file line number Diff line number Diff line change
@@ -1,82 +1,77 @@
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 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);
}
function Searchbar(props) {
const [searchValue, setSearchValue] = useState(props.searchTerm);

componentWillUnmount() {
this.props.resetSearchTerm();
}
useEffect(() => {
return () => {
props.resetSearchTerm();
};
}, []);

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

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

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

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={props.searchLabel}
onChange={handleSearchChange}
/>
<button
className="searchbar__clear-button"
onClick={handleResetSearch}
>
{props.t('Searchbar.ClearTerm')}
</button>
</div>
);
}

Searchbar.propTypes = {
searchTerm: PropTypes.string.isRequired,
setSearchTerm: PropTypes.func.isRequired,
resetSearchTerm: PropTypes.func.isRequired,
searchLabel: PropTypes.string,
t: PropTypes.func.isRequired
t: PropTypes.func.isRequired,
};

Searchbar.defaultProps = {
searchLabel: i18next.t('Searchbar.SearchSketch')
searchLabel: i18next.t('Searchbar.SearchSketch'),
};

export default withTranslation()(Searchbar);
export default withTranslation()(Searchbar);