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