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.

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