104 lines
2.1 KiB
JavaScript
Raw Normal View History

2018-06-06 18:51:24 +02:00
/*
2019-07-08 20:27:38 +02:00
*
* Search
*
*/
2018-06-06 18:51:24 +02:00
2019-07-25 16:14:50 +02:00
import React, { memo } 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';
2019-12-13 15:37:34 +01:00
import Cross from '../../icons/Cross';
import Filter from '../../icons/Filter';
import SearchIcon from '../../icons/Search';
2019-07-25 16:06:45 +02:00
import { Wrapper, Infos, Clear } from './components';
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
timer = null;
2018-06-07 14:19:34 +02:00
componentDidUpdate(prevProps) {
const { model, value } = this.props;
2019-07-08 20:27:38 +02:00
if (
prevProps.model !== model ||
(!isEmpty(prevProps.value) && isEmpty(value))
) {
2018-06-07 14:19:34 +02:00
this.resetState();
}
}
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);
2019-07-08 20:27:38 +02:00
};
2018-06-07 10:38:30 +02:00
2018-06-07 14:19:34 +02:00
handleClick = () => {
this.setState({ value: '' });
this.triggerChange('');
2019-07-08 20:27:38 +02:00
};
2018-06-07 10:38:30 +02:00
2019-07-08 20:27:38 +02:00
triggerChange = value =>
this.props.changeParams({
2018-06-07 14:19:34 +02:00
target: {
2019-07-08 20:27:38 +02:00
name: '_q',
2018-06-07 14:19:34 +02:00
value,
},
2019-07-08 20:27:38 +02:00
});
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 (
2019-07-25 16:06:45 +02:00
<Wrapper>
2019-12-13 15:37:34 +01:00
<div>
<SearchIcon />
</div>
2018-06-07 10:38:30 +02:00
<div>
<FormattedMessage id="content-manager.components.Search.placeholder">
2019-07-08 20:27:38 +02:00
{message => (
2018-06-07 10:38:30 +02:00
<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>
2019-12-13 15:37:34 +01:00
{value !== '' && (
<Clear onClick={this.handleClick}>
<Cross />
</Clear>
)}
2018-06-07 10:38:30 +02:00
</div>
2019-07-25 16:06:45 +02:00
<Infos>
2019-12-13 15:37:34 +01:00
<Filter />
2018-06-07 10:38:30 +02:00
{upperFirst(model)}
2019-07-25 16:06:45 +02:00
</Infos>
</Wrapper>
2018-06-06 18:51:24 +02:00
);
}
}
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
};
2019-07-25 16:14:50 +02:00
export default memo(Search);