2019-08-21 18:08:08 +00:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React from 'react';
|
|
|
|
import { throttle } from 'lodash';
|
2020-08-26 15:28:53 +00:00
|
|
|
import { withTranslation } from 'react-i18next';
|
|
|
|
import i18next from 'i18next';
|
2020-04-29 22:34:37 +00:00
|
|
|
import SearchIcon from '../../../../images/magnifyingglass.svg';
|
2019-08-21 18:08:08 +00:00
|
|
|
|
2020-08-26 15:28:53 +00:00
|
|
|
|
2019-08-21 18:08:08 +00:00
|
|
|
class Searchbar extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
searchValue: this.props.searchTerm
|
|
|
|
};
|
|
|
|
this.throttledSearchChange = throttle(this.searchChange, 500);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.props.resetSearchTerm();
|
|
|
|
}
|
|
|
|
|
|
|
|
handleResetSearch = () => {
|
|
|
|
this.setState({ searchValue: '' }, () => {
|
|
|
|
this.props.resetSearchTerm();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-06 03:51:09 +00:00
|
|
|
searchChange = () => {
|
|
|
|
this.props.setSearchTerm(this.state.searchValue.trim());
|
2019-08-21 18:08:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
handleSearchChange = (e) => {
|
|
|
|
this.setState({ searchValue: e.target.value }, () => {
|
2020-07-28 07:18:52 +00:00
|
|
|
this.throttledSearchChange(this.state.searchValue.trim());
|
2019-08-21 18:08:08 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { searchValue } = this.state;
|
|
|
|
return (
|
2019-10-20 19:55:00 +00:00
|
|
|
<div className={`searchbar ${searchValue === '' ? 'searchbar--is-empty' : ''}`}>
|
2020-02-25 17:52:52 +00:00
|
|
|
<div className="searchbar__button">
|
2020-05-05 23:03:58 +00:00
|
|
|
<SearchIcon className="searchbar__icon" focusable="false" aria-hidden="true" />
|
2020-02-25 17:52:52 +00:00
|
|
|
</div>
|
2019-08-21 18:08:08 +00:00
|
|
|
<input
|
|
|
|
className="searchbar__input"
|
|
|
|
type="text"
|
|
|
|
value={searchValue}
|
2019-10-20 19:58:13 +00:00
|
|
|
placeholder={this.props.searchLabel}
|
2019-08-21 18:08:08 +00:00
|
|
|
onChange={this.handleSearchChange}
|
|
|
|
/>
|
|
|
|
<button
|
|
|
|
className="searchbar__clear-button"
|
|
|
|
onClick={this.handleResetSearch}
|
2020-08-26 15:28:53 +00:00
|
|
|
>{this.props.t('Searchbar.ClearTerm')}
|
2019-08-21 18:08:08 +00:00
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Searchbar.propTypes = {
|
|
|
|
searchTerm: PropTypes.string.isRequired,
|
|
|
|
setSearchTerm: PropTypes.func.isRequired,
|
2019-10-20 19:58:13 +00:00
|
|
|
resetSearchTerm: PropTypes.func.isRequired,
|
|
|
|
searchLabel: PropTypes.string,
|
2020-08-26 15:28:53 +00:00
|
|
|
t: PropTypes.func.isRequired
|
2019-08-21 18:08:08 +00:00
|
|
|
};
|
|
|
|
|
2019-10-20 19:58:13 +00:00
|
|
|
Searchbar.defaultProps = {
|
2020-08-26 15:28:53 +00:00
|
|
|
searchLabel: i18next.t('Searchbar.SearchSketch')
|
2019-10-20 19:58:13 +00:00
|
|
|
};
|
2019-08-21 18:08:08 +00:00
|
|
|
|
2020-08-26 15:28:53 +00:00
|
|
|
export default withTranslation()(Searchbar);
|