The code powering m.abunchtell.com https://m.abunchtell.com
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

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