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.

75 lines
1.8 KiB

import React from 'react';
import PropTypes from 'prop-types';
import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
const messages = defineMessages({
placeholder: { id: 'search.placeholder', defaultMessage: 'Search' }
});
8 years ago
class Search extends React.PureComponent {
8 years ago
static 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
};
handleChange = (e) => {
8 years ago
this.props.onChange(e.target.value);
}
8 years ago
handleClear = (e) => {
8 years ago
e.preventDefault();
if (this.props.value.length > 0 || this.props.submitted) {
this.props.onClear();
}
}
8 years ago
handleKeyDown = (e) => {
8 years ago
if (e.key === 'Enter') {
e.preventDefault();
this.props.onSubmit();
}
}
8 years ago
noop () {
}
handleFocus = () => {
8 years ago
this.props.onShow();
}
8 years ago
render () {
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
/>
<div role='button' tabIndex='0' className='search__icon' onClick={this.handleClear}>
8 years ago
<i className={`fa fa-search ${hasValue ? '' : 'active'}`} />
<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
}
export default injectIntl(Search);