2019-08-21 20:08:08 +02:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React from 'react';
|
|
|
|
import InlineSVG from 'react-inlinesvg';
|
|
|
|
import { throttle } from 'lodash';
|
|
|
|
|
2019-11-10 21:39:22 +01:00
|
|
|
const searchIcon = require('../../../../images/magnifyingglass.svg');
|
2019-08-21 20:08:08 +02: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();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSearchEnter = (e) => {
|
|
|
|
if (e.key === 'Enter') {
|
2020-04-06 05:51:09 +02:00
|
|
|
this.searchChange();
|
2019-08-21 20:08:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-06 05:51:09 +02:00
|
|
|
searchChange = () => {
|
|
|
|
if (this.state.searchValue.trim().length === 0) return;
|
|
|
|
this.props.setSearchTerm(this.state.searchValue.trim());
|
2019-08-21 20:08:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
handleSearchChange = (e) => {
|
|
|
|
this.setState({ searchValue: e.target.value }, () => {
|
|
|
|
this.throttledSearchChange(this.state.searchValue);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { searchValue } = this.state;
|
|
|
|
return (
|
2019-10-20 21:55:00 +02:00
|
|
|
<div className={`searchbar ${searchValue === '' ? 'searchbar--is-empty' : ''}`}>
|
2020-02-25 18:52:52 +01:00
|
|
|
<div className="searchbar__button">
|
2019-08-21 20:08:08 +02:00
|
|
|
<InlineSVG className="searchbar__icon" src={searchIcon} />
|
2020-02-25 18:52:52 +01:00
|
|
|
</div>
|
2019-08-21 20:08:08 +02:00
|
|
|
<input
|
|
|
|
className="searchbar__input"
|
|
|
|
type="text"
|
|
|
|
value={searchValue}
|
2019-10-20 21:58:13 +02:00
|
|
|
placeholder={this.props.searchLabel}
|
2019-08-21 20:08:08 +02:00
|
|
|
onChange={this.handleSearchChange}
|
|
|
|
onKeyUp={this.handleSearchEnter}
|
|
|
|
/>
|
|
|
|
<button
|
|
|
|
className="searchbar__clear-button"
|
|
|
|
onClick={this.handleResetSearch}
|
|
|
|
>clear
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Searchbar.propTypes = {
|
|
|
|
searchTerm: PropTypes.string.isRequired,
|
|
|
|
setSearchTerm: PropTypes.func.isRequired,
|
2019-10-20 21:58:13 +02:00
|
|
|
resetSearchTerm: PropTypes.func.isRequired,
|
|
|
|
searchLabel: PropTypes.string,
|
2019-08-21 20:08:08 +02:00
|
|
|
};
|
|
|
|
|
2019-10-20 21:58:13 +02:00
|
|
|
Searchbar.defaultProps = {
|
|
|
|
searchLabel: 'Search sketches...',
|
|
|
|
};
|
2019-08-21 20:08:08 +02:00
|
|
|
|
2019-11-10 21:39:22 +01:00
|
|
|
export default Searchbar;
|