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.

131 lines
4.3 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. 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 }}>
  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 propTypes = {
  42. value: PropTypes.string.isRequired,
  43. submitted: PropTypes.bool,
  44. onChange: PropTypes.func.isRequired,
  45. onSubmit: PropTypes.func.isRequired,
  46. onClear: PropTypes.func.isRequired,
  47. onShow: PropTypes.func.isRequired,
  48. intl: PropTypes.object.isRequired,
  49. };
  50. state = {
  51. expanded: false,
  52. };
  53. handleChange = (e) => {
  54. this.props.onChange(e.target.value);
  55. }
  56. handleClear = (e) => {
  57. e.preventDefault();
  58. if (this.props.value.length > 0 || this.props.submitted) {
  59. this.props.onClear();
  60. }
  61. }
  62. handleKeyDown = (e) => {
  63. if (e.key === 'Enter') {
  64. e.preventDefault();
  65. this.props.onSubmit();
  66. } else if (e.key === 'Escape') {
  67. document.querySelector('.ui').parentElement.focus();
  68. }
  69. }
  70. noop () {
  71. }
  72. handleFocus = () => {
  73. this.setState({ expanded: true });
  74. this.props.onShow();
  75. }
  76. handleBlur = () => {
  77. this.setState({ expanded: false });
  78. }
  79. render () {
  80. const { intl, value, submitted } = this.props;
  81. const { expanded } = this.state;
  82. const hasValue = value.length > 0 || submitted;
  83. return (
  84. <div className='search'>
  85. <label>
  86. <span style={{ display: 'none' }}>{intl.formatMessage(messages.placeholder)}</span>
  87. <input
  88. className='search__input'
  89. type='text'
  90. placeholder={intl.formatMessage(messages.placeholder)}
  91. value={value}
  92. onChange={this.handleChange}
  93. onKeyUp={this.handleKeyDown}
  94. onFocus={this.handleFocus}
  95. onBlur={this.handleBlur}
  96. />
  97. </label>
  98. <div role='button' tabIndex='0' className='search__icon' onClick={this.handleClear}>
  99. <Icon id='search' className={hasValue ? '' : 'active'} />
  100. <Icon id='times-circle' className={hasValue ? 'active' : ''} aria-label={intl.formatMessage(messages.placeholder)} />
  101. </div>
  102. <Overlay show={expanded && !hasValue} placement='bottom' target={this}>
  103. <SearchPopout />
  104. </Overlay>
  105. </div>
  106. );
  107. }
  108. }