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.

76 lines
2.2 KiB

  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { connect } from 'react-redux';
  4. import { defineMessages, injectIntl } from 'react-intl';
  5. import { fetchListSuggestions, clearListSuggestions, changeListSuggestions } from '../../../actions/lists';
  6. import classNames from 'classnames';
  7. import Icon from 'mastodon/components/icon';
  8. const messages = defineMessages({
  9. search: { id: 'lists.search', defaultMessage: 'Search among people you follow' },
  10. });
  11. const mapStateToProps = state => ({
  12. value: state.getIn(['listEditor', 'suggestions', 'value']),
  13. });
  14. const mapDispatchToProps = dispatch => ({
  15. onSubmit: value => dispatch(fetchListSuggestions(value)),
  16. onClear: () => dispatch(clearListSuggestions()),
  17. onChange: value => dispatch(changeListSuggestions(value)),
  18. });
  19. export default @connect(mapStateToProps, mapDispatchToProps)
  20. @injectIntl
  21. class Search extends React.PureComponent {
  22. static propTypes = {
  23. intl: PropTypes.object.isRequired,
  24. value: PropTypes.string.isRequired,
  25. onChange: PropTypes.func.isRequired,
  26. onSubmit: PropTypes.func.isRequired,
  27. onClear: PropTypes.func.isRequired,
  28. };
  29. handleChange = e => {
  30. this.props.onChange(e.target.value);
  31. }
  32. handleKeyUp = e => {
  33. if (e.keyCode === 13) {
  34. this.props.onSubmit(this.props.value);
  35. }
  36. }
  37. handleClear = () => {
  38. this.props.onClear();
  39. }
  40. render () {
  41. const { value, intl } = this.props;
  42. const hasValue = value.length > 0;
  43. return (
  44. <div className='list-editor__search search'>
  45. <label>
  46. <span style={{ display: 'none' }}>{intl.formatMessage(messages.search)}</span>
  47. <input
  48. className='search__input'
  49. type='text'
  50. value={value}
  51. onChange={this.handleChange}
  52. onKeyUp={this.handleKeyUp}
  53. placeholder={intl.formatMessage(messages.search)}
  54. />
  55. </label>
  56. <div role='button' tabIndex='0' className='search__icon' onClick={this.handleClear}>
  57. <Icon id='search' className={classNames({ active: !hasValue })} />
  58. <Icon id='times-circle' aria-label={intl.formatMessage(messages.search)} className={classNames({ active: hasValue })} />
  59. </div>
  60. </div>
  61. );
  62. }
  63. }