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.

129 lines
4.0 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 'flavours/glitch/util/optional_motion';
  6. import spring from 'react-motion/lib/spring';
  7. const messages = defineMessages({
  8. placeholder: { id: 'search.placeholder', defaultMessage: 'Search' },
  9. });
  10. class SearchPopout extends React.PureComponent {
  11. static propTypes = {
  12. style: PropTypes.object,
  13. };
  14. render () {
  15. const { style } = this.props;
  16. return (
  17. <div style={{ ...style, position: 'absolute', width: 285 }}>
  18. <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 }) }}>
  19. {({ opacity, scaleX, scaleY }) => (
  20. <div className='search-popout' style={{ opacity: opacity, transform: `scale(${scaleX}, ${scaleY})` }}>
  21. <h4><FormattedMessage id='search_popout.search_format' defaultMessage='Advanced search format' /></h4>
  22. <ul>
  23. <li><em>#example</em> <FormattedMessage id='search_popout.tips.hashtag' defaultMessage='hashtag' /></li>
  24. <li><em>@username@domain</em> <FormattedMessage id='search_popout.tips.user' defaultMessage='user' /></li>
  25. <li><em>URL</em> <FormattedMessage id='search_popout.tips.user' defaultMessage='user' /></li>
  26. <li><em>URL</em> <FormattedMessage id='search_popout.tips.status' defaultMessage='status' /></li>
  27. </ul>
  28. <FormattedMessage id='search_popout.tips.text' defaultMessage='Simple text returns matching display names, usernames and hashtags' />
  29. </div>
  30. )}
  31. </Motion>
  32. </div>
  33. );
  34. }
  35. }
  36. @injectIntl
  37. export default class Search extends React.PureComponent {
  38. static propTypes = {
  39. value: PropTypes.string.isRequired,
  40. submitted: PropTypes.bool,
  41. onChange: PropTypes.func.isRequired,
  42. onSubmit: PropTypes.func.isRequired,
  43. onClear: PropTypes.func.isRequired,
  44. onShow: PropTypes.func.isRequired,
  45. intl: PropTypes.object.isRequired,
  46. };
  47. state = {
  48. expanded: false,
  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. handleKeyDown = (e) => {
  60. if (e.key === 'Enter') {
  61. e.preventDefault();
  62. this.props.onSubmit();
  63. } else if (e.key === 'Escape') {
  64. document.querySelector('.ui').parentElement.focus();
  65. }
  66. }
  67. noop () {
  68. }
  69. handleFocus = () => {
  70. this.setState({ expanded: true });
  71. this.props.onShow();
  72. }
  73. handleBlur = () => {
  74. this.setState({ expanded: false });
  75. }
  76. render () {
  77. const { intl, value, submitted } = this.props;
  78. const { expanded } = this.state;
  79. const hasValue = value.length > 0 || submitted;
  80. return (
  81. <div className='search'>
  82. <label>
  83. <span style={{ display: 'none' }}>{intl.formatMessage(messages.placeholder)}</span>
  84. <input
  85. className='search__input'
  86. type='text'
  87. placeholder={intl.formatMessage(messages.placeholder)}
  88. value={value}
  89. onChange={this.handleChange}
  90. onKeyUp={this.handleKeyDown}
  91. onFocus={this.handleFocus}
  92. onBlur={this.handleBlur}
  93. />
  94. </label>
  95. <div role='button' tabIndex='0' className='search__icon' onClick={this.handleClear}>
  96. <i className={`fa fa-search ${hasValue ? '' : 'active'}`} />
  97. <i aria-label={intl.formatMessage(messages.placeholder)} className={`fa fa-times-circle ${hasValue ? 'active' : ''}`} />
  98. </div>
  99. <Overlay show={expanded && !hasValue} placement='bottom' target={this}>
  100. <SearchPopout />
  101. </Overlay>
  102. </div>
  103. );
  104. }
  105. }