94 lines
2.0 KiB
JavaScript
Raw Normal View History

2018-06-06 18:51:24 +02:00
/*
*
* Search
*
*/
import React from 'react';
2018-06-07 14:19:34 +02:00
import { isEmpty, upperFirst } from 'lodash';
2018-06-07 10:38:30 +02:00
import PropTypes from 'prop-types';
import { FormattedMessage } from 'react-intl';
2018-06-06 18:51:24 +02:00
2018-06-07 10:38:30 +02:00
import Logo from 'assets/images/icon_filter.png';
2018-06-06 18:51:24 +02:00
import styles from './styles.scss';
2018-06-07 14:19:34 +02:00
const WAIT = 400;
2018-06-07 10:38:30 +02:00
2018-06-06 18:51:24 +02:00
class Search extends React.Component {
2018-06-07 18:50:43 +02:00
state = { value: this.props.initValue };
2018-06-06 18:51:24 +02:00
2018-06-07 14:19:34 +02:00
componentDidUpdate(prevProps) {
const { model, value } = this.props;
2018-06-07 18:50:43 +02:00
if (prevProps.model !== model || !isEmpty(prevProps.value) && isEmpty(value)) {
2018-06-07 14:19:34 +02:00
this.resetState();
}
}
2018-06-07 10:38:30 +02:00
timer = null;
2018-06-07 14:19:34 +02:00
resetState = () => this.setState({ value: '' });
2018-06-06 18:51:24 +02:00
handleChange = ({ target }) => {
2018-06-07 10:38:30 +02:00
clearTimeout(this.timer);
2018-06-06 18:51:24 +02:00
this.setState({ value: target.value });
2018-06-07 14:19:34 +02:00
this.timer = setTimeout(() => this.triggerChange(target.value), WAIT);
2018-06-07 10:38:30 +02:00
}
2018-06-07 14:19:34 +02:00
handleClick = () => {
this.setState({ value: '' });
this.triggerChange('');
}
2018-06-07 10:38:30 +02:00
triggerChange = (value) => (
this.props.changeParams({
2018-06-07 14:19:34 +02:00
target: {
name: 'params.q',
value,
},
})
);
2018-06-06 18:51:24 +02:00
render() {
2018-06-07 10:38:30 +02:00
const { model } = this.props;
2018-06-07 14:19:34 +02:00
const { value } = this.state;
2018-06-07 10:38:30 +02:00
2018-06-06 18:51:24 +02:00
return (
<div className={styles.search}>
2018-06-07 10:38:30 +02:00
<div>
<FormattedMessage id="content-manager.components.Search.placeholder">
{(message) => (
<input
onChange={this.handleChange}
placeholder={message}
type="text"
2018-06-07 14:19:34 +02:00
value={value}
2018-06-07 10:38:30 +02:00
/>
)}
</FormattedMessage>
2018-06-07 14:19:34 +02:00
{value !== '' && <div className={styles.clearable} onClick={this.handleClick} />}
2018-06-07 10:38:30 +02:00
</div>
<div className={styles.searchLabel}>
<img src={Logo} alt="filter_logo" />
{upperFirst(model)}
</div>
2018-06-06 18:51:24 +02:00
</div>
);
}
}
2018-06-07 10:38:30 +02:00
Search.defaultProps = {
changeParams: () => {},
2018-06-07 10:38:30 +02:00
model: '',
2018-06-07 14:19:34 +02:00
value: '',
2018-06-07 10:38:30 +02:00
};
Search.propTypes = {
changeParams: PropTypes.func,
2018-06-07 18:50:43 +02:00
initValue: PropTypes.string.isRequired,
2018-06-07 10:38:30 +02:00
model: PropTypes.string,
2018-06-07 14:19:34 +02:00
value: PropTypes.string,
2018-06-07 10:38:30 +02:00
};
2018-06-06 18:51:24 +02:00
export default Search;