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.

126 lines
3.8 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';
  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. }
  63. }
  64. noop () {
  65. }
  66. handleFocus = () => {
  67. this.setState({ expanded: true });
  68. this.props.onShow();
  69. }
  70. handleBlur = () => {
  71. this.setState({ expanded: false });
  72. }
  73. render () {
  74. const { intl, value, submitted } = this.props;
  75. const { expanded } = this.state;
  76. const hasValue = value.length > 0 || submitted;
  77. return (
  78. <div className='search'>
  79. <label>
  80. <span style={{ display: 'none' }}>{intl.formatMessage(messages.placeholder)}</span>
  81. <input
  82. className='search__input'
  83. type='text'
  84. placeholder={intl.formatMessage(messages.placeholder)}
  85. value={value}
  86. onChange={this.handleChange}
  87. onKeyUp={this.handleKeyDown}
  88. onFocus={this.handleFocus}
  89. onBlur={this.handleBlur}
  90. />
  91. </label>
  92. <div role='button' tabIndex='0' className='search__icon' onClick={this.handleClear}>
  93. <i className={`fa fa-search ${hasValue ? '' : 'active'}`} />
  94. <i aria-label={intl.formatMessage(messages.placeholder)} className={`fa fa-times-circle ${hasValue ? 'active' : ''}`} />
  95. </div>
  96. <Overlay show={expanded && !hasValue} placement='bottom' target={this}>
  97. <SearchPopout />
  98. </Overlay>
  99. </div>
  100. );
  101. }
  102. }