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.

148 lines
4.7 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
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
  4. import Overlay from 'react-overlays/Overlay';
  5. import { searchEnabled } from '../../../initial_state';
  6. import Icon from 'mastodon/components/icon';
  7. const messages = defineMessages({
  8. placeholder: { id: 'search.placeholder', defaultMessage: 'Search' },
  9. placeholderSignedIn: { id: 'search.search_or_paste', defaultMessage: 'Search or paste URL' },
  10. });
  11. class SearchPopout extends React.PureComponent {
  12. render () {
  13. const extraInformation = searchEnabled ? <FormattedMessage id='search_popout.tips.full_text' defaultMessage='Simple text returns statuses you have written, favourited, boosted, or have been mentioned in, as well as matching usernames, display names, and hashtags.' /> : <FormattedMessage id='search_popout.tips.text' defaultMessage='Simple text returns matching display names, usernames and hashtags' />;
  14. return (
  15. <div className='search-popout'>
  16. <h4><FormattedMessage id='search_popout.search_format' defaultMessage='Advanced search format' /></h4>
  17. <ul>
  18. <li><em>#example</em> <FormattedMessage id='search_popout.tips.hashtag' defaultMessage='hashtag' /></li>
  19. <li><em>@username@domain</em> <FormattedMessage id='search_popout.tips.user' defaultMessage='user' /></li>
  20. <li><em>URL</em> <FormattedMessage id='search_popout.tips.user' defaultMessage='user' /></li>
  21. <li><em>URL</em> <FormattedMessage id='search_popout.tips.status' defaultMessage='status' /></li>
  22. </ul>
  23. {extraInformation}
  24. </div>
  25. );
  26. }
  27. }
  28. class Search extends React.PureComponent {
  29. static contextTypes = {
  30. router: PropTypes.object.isRequired,
  31. identity: PropTypes.object.isRequired,
  32. };
  33. static propTypes = {
  34. value: PropTypes.string.isRequired,
  35. submitted: PropTypes.bool,
  36. onChange: PropTypes.func.isRequired,
  37. onSubmit: PropTypes.func.isRequired,
  38. onClear: PropTypes.func.isRequired,
  39. onShow: PropTypes.func.isRequired,
  40. openInRoute: PropTypes.bool,
  41. intl: PropTypes.object.isRequired,
  42. singleColumn: PropTypes.bool,
  43. };
  44. state = {
  45. expanded: false,
  46. };
  47. setRef = c => {
  48. this.searchForm = c;
  49. };
  50. handleChange = (e) => {
  51. this.props.onChange(e.target.value);
  52. };
  53. handleClear = (e) => {
  54. e.preventDefault();
  55. if (this.props.value.length > 0 || this.props.submitted) {
  56. this.props.onClear();
  57. }
  58. };
  59. handleKeyUp = (e) => {
  60. if (e.key === 'Enter') {
  61. e.preventDefault();
  62. this.props.onSubmit();
  63. if (this.props.openInRoute) {
  64. this.context.router.history.push('/search');
  65. }
  66. } else if (e.key === 'Escape') {
  67. document.querySelector('.ui').parentElement.focus();
  68. }
  69. };
  70. handleFocus = () => {
  71. this.setState({ expanded: true });
  72. this.props.onShow();
  73. if (this.searchForm && !this.props.singleColumn) {
  74. const { left, right } = this.searchForm.getBoundingClientRect();
  75. if (left < 0 || right > (window.innerWidth || document.documentElement.clientWidth)) {
  76. this.searchForm.scrollIntoView();
  77. }
  78. }
  79. };
  80. handleBlur = () => {
  81. this.setState({ expanded: false });
  82. };
  83. findTarget = () => {
  84. return this.searchForm;
  85. };
  86. render () {
  87. const { intl, value, submitted } = this.props;
  88. const { expanded } = this.state;
  89. const { signedIn } = this.context.identity;
  90. const hasValue = value.length > 0 || submitted;
  91. return (
  92. <div className='search'>
  93. <input
  94. ref={this.setRef}
  95. className='search__input'
  96. type='text'
  97. placeholder={intl.formatMessage(signedIn ? messages.placeholderSignedIn : messages.placeholder)}
  98. aria-label={intl.formatMessage(signedIn ? messages.placeholderSignedIn : messages.placeholder)}
  99. value={value}
  100. onChange={this.handleChange}
  101. onKeyUp={this.handleKeyUp}
  102. onFocus={this.handleFocus}
  103. onBlur={this.handleBlur}
  104. />
  105. <div role='button' tabIndex='0' className='search__icon' onClick={this.handleClear}>
  106. <Icon id='search' className={hasValue ? '' : 'active'} />
  107. <Icon id='times-circle' className={hasValue ? 'active' : ''} aria-label={intl.formatMessage(messages.placeholder)} />
  108. </div>
  109. <Overlay show={expanded && !hasValue} placement='bottom' target={this.findTarget} popperConfig={{ strategy: 'fixed' }}>
  110. {({ props, placement }) => (
  111. <div {...props} style={{ ...props.style, width: 285, zIndex: 2 }}>
  112. <div className={`dropdown-animation ${placement}`}>
  113. <SearchPopout />
  114. </div>
  115. </div>
  116. )}
  117. </Overlay>
  118. </div>
  119. );
  120. }
  121. }
  122. export default injectIntl(Search);