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