闭社主体 forked from https://github.com/tootsuite/mastodon
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

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