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.
 
 
 
 

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