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.

74 lines
1.8 KiB

8 years ago
import PureRenderMixin from 'react-addons-pure-render-mixin';
import ImmutablePropTypes from 'react-immutable-proptypes';
import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
const messages = defineMessages({
placeholder: { id: 'search.placeholder', defaultMessage: 'Search' }
});
8 years ago
const Search = React.createClass({
propTypes: {
value: React.PropTypes.string.isRequired,
submitted: React.PropTypes.bool,
8 years ago
onChange: React.PropTypes.func.isRequired,
7 years ago
onSubmit: React.PropTypes.func.isRequired,
8 years ago
onClear: React.PropTypes.func.isRequired,
7 years ago
onShow: React.PropTypes.func.isRequired,
intl: React.PropTypes.object.isRequired
8 years ago
},
mixins: [PureRenderMixin],
7 years ago
handleChange (e) {
this.props.onChange(e.target.value);
8 years ago
},
7 years ago
handleClear (e) {
e.preventDefault();
8 years ago
this.props.onClear();
},
7 years ago
handleKeyDown (e) {
if (e.key === 'Enter') {
e.preventDefault();
this.props.onSubmit();
}
8 years ago
},
noop () {
},
7 years ago
handleFocus () {
this.props.onShow();
8 years ago
},
render () {
const { intl, value, submitted } = this.props;
const hasValue = value.length > 0 || submitted;
8 years ago
return (
7 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={hasValue ? this.handleClear : this.noop}>
7 years ago
<i className={`fa fa-search ${hasValue ? '' : 'active'}`} />
<i aria-label="Clear search" className={`fa fa-times-circle ${hasValue ? 'active' : ''}`} />
7 years ago
</div>
8 years ago
</div>
);
7 years ago
}
8 years ago
});
export default injectIntl(Search);