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.

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