You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
2.0 KiB
83 lines
2.0 KiB
8 years ago
|
import React from 'react';
|
||
8 years ago
|
import PropTypes from 'prop-types';
|
||
8 years ago
|
import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
|
||
|
|
||
|
const messages = defineMessages({
|
||
|
placeholder: { id: 'search.placeholder', defaultMessage: 'Search' }
|
||
|
});
|
||
8 years ago
|
|
||
8 years ago
|
class Search extends React.PureComponent {
|
||
8 years ago
|
|
||
8 years ago
|
constructor (props, context) {
|
||
|
super(props, context);
|
||
|
this.handleChange = this.handleChange.bind(this);
|
||
|
this.handleKeyDown = this.handleKeyDown.bind(this);
|
||
|
this.handleFocus = this.handleFocus.bind(this);
|
||
8 years ago
|
this.handleClear = this.handleClear.bind(this);
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
handleChange (e) {
|
||
|
this.props.onChange(e.target.value);
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
handleClear (e) {
|
||
|
e.preventDefault();
|
||
8 years ago
|
|
||
|
if (this.props.value.length > 0 || this.props.submitted) {
|
||
|
this.props.onClear();
|
||
|
}
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
handleKeyDown (e) {
|
||
|
if (e.key === 'Enter') {
|
||
|
e.preventDefault();
|
||
|
this.props.onSubmit();
|
||
|
}
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
noop () {
|
||
|
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
handleFocus () {
|
||
|
this.props.onShow();
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
|
render () {
|
||
8 years ago
|
const { intl, value, submitted } = this.props;
|
||
|
const hasValue = value.length > 0 || submitted;
|
||
8 years ago
|
|
||
|
return (
|
||
8 years ago
|
<div className='search'>
|
||
|
<input
|
||
|
className='search__input'
|
||
|
type='text'
|
||
|
placeholder={intl.formatMessage(messages.placeholder)}
|
||
|
value={value}
|
||
|
onChange={this.handleChange}
|
||
|
onKeyUp={this.handleKeyDown}
|
||
|
onFocus={this.handleFocus}
|
||
8 years ago
|
/>
|
||
|
|
||
8 years ago
|
<div role='button' tabIndex='0' className='search__icon' onClick={this.handleClear}>
|
||
8 years ago
|
<i className={`fa fa-search ${hasValue ? '' : 'active'}`} />
|
||
8 years ago
|
<i aria-label={intl.formatMessage(messages.placeholder)} className={`fa fa-times-circle ${hasValue ? 'active' : ''}`} />
|
||
8 years ago
|
</div>
|
||
8 years ago
|
</div>
|
||
|
);
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
}
|
||
|
|
||
|
Search.propTypes = {
|
||
|
value: PropTypes.string.isRequired,
|
||
|
submitted: PropTypes.bool,
|
||
|
onChange: PropTypes.func.isRequired,
|
||
|
onSubmit: PropTypes.func.isRequired,
|
||
|
onClear: PropTypes.func.isRequired,
|
||
|
onShow: PropTypes.func.isRequired,
|
||
|
intl: PropTypes.object.isRequired
|
||
|
};
|
||
8 years ago
|
|
||
8 years ago
|
export default injectIntl(Search);
|