闭社主体 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.

76 lines
1.9 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. <label>
  44. <span style={{ display: 'none' }}>{intl.formatMessage(messages.placeholder)}</span>
  45. <input
  46. className='search__input'
  47. type='text'
  48. placeholder={intl.formatMessage(messages.placeholder)}
  49. value={value}
  50. onChange={this.handleChange}
  51. onKeyUp={this.handleKeyDown}
  52. onFocus={this.handleFocus}
  53. />
  54. </label>
  55. <div role='button' tabIndex='0' className='search__icon' onClick={this.handleClear}>
  56. <i className={`fa fa-search ${hasValue ? '' : 'active'}`} />
  57. <i aria-label={intl.formatMessage(messages.placeholder)} className={`fa fa-times-circle ${hasValue ? 'active' : ''}`} />
  58. </div>
  59. </div>
  60. );
  61. }
  62. }