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.

73 lines
1.8 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. import PureRenderMixin from 'react-addons-pure-render-mixin';
  2. import ImmutablePropTypes from 'react-immutable-proptypes';
  3. import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
  4. const messages = defineMessages({
  5. placeholder: { id: 'search.placeholder', defaultMessage: 'Search' }
  6. });
  7. const Search = React.createClass({
  8. propTypes: {
  9. value: React.PropTypes.string.isRequired,
  10. submitted: React.PropTypes.bool,
  11. onChange: React.PropTypes.func.isRequired,
  12. onSubmit: React.PropTypes.func.isRequired,
  13. onClear: React.PropTypes.func.isRequired,
  14. onShow: React.PropTypes.func.isRequired,
  15. intl: React.PropTypes.object.isRequired
  16. },
  17. mixins: [PureRenderMixin],
  18. handleChange (e) {
  19. this.props.onChange(e.target.value);
  20. },
  21. handleClear (e) {
  22. e.preventDefault();
  23. this.props.onClear();
  24. },
  25. handleKeyDown (e) {
  26. if (e.key === 'Enter') {
  27. e.preventDefault();
  28. this.props.onSubmit();
  29. }
  30. },
  31. noop () {
  32. },
  33. handleFocus () {
  34. this.props.onShow();
  35. },
  36. render () {
  37. const { intl, value, submitted } = this.props;
  38. const hasValue = value.length > 0 || submitted;
  39. return (
  40. <div className='search'>
  41. <input
  42. className='search__input'
  43. type='text'
  44. placeholder={intl.formatMessage(messages.placeholder)}
  45. value={value}
  46. onChange={this.handleChange}
  47. onKeyUp={this.handleKeyDown}
  48. onFocus={this.handleFocus}
  49. />
  50. <div role='button' tabIndex='0' className='search__icon' onClick={hasValue ? this.handleClear : this.noop}>
  51. <i className={`fa fa-search ${hasValue ? '' : 'active'}`} />
  52. <i aria-label="Clear search" className={`fa fa-times-circle ${hasValue ? 'active' : ''}`} />
  53. </div>
  54. </div>
  55. );
  56. }
  57. });
  58. export default injectIntl(Search);