SearchForm.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import React from 'react';
  2. // Header.SearchForm
  3. export default class SearchForm extends React.Component {
  4. constructor(props) {
  5. super(props);
  6. this.state = {
  7. keyword: '',
  8. searchedKeyword: '',
  9. };
  10. this.handleChange = this.handleChange.bind(this);
  11. this.handleFocus = this.handleFocus.bind(this);
  12. this.handleBlur = this.handleBlur.bind(this);
  13. this.clearForm = this.clearForm.bind(this);
  14. this.ticker = null;
  15. }
  16. componentDidMount() {
  17. this.ticker = setInterval(this.searchFieldTicker.bind(this), this.props.pollInterval);
  18. }
  19. componentWillUnmount() {
  20. clearInterval(this.ticker);
  21. }
  22. search() {
  23. if (this.state.searchedKeyword != this.state.keyword) {
  24. this.props.onSearchFormChanged({keyword: this.state.keyword});
  25. this.setState({searchedKeyword: this.state.keyword});
  26. }
  27. }
  28. getFormClearComponent() {
  29. let isHidden = (this.state.keyword === '');
  30. return (
  31. <a className="search-top-clear" onClick={this.clearForm} hidden={isHidden}>
  32. <i className="fa fa-times-circle" />
  33. </a>
  34. );
  35. }
  36. clearForm() {
  37. this.setState({keyword: ''});
  38. this.search();
  39. }
  40. searchFieldTicker() {
  41. this.search();
  42. }
  43. handleFocus(event) {
  44. this.props.isShown(true);
  45. }
  46. handleBlur(event) {
  47. //this.props.isShown(false);
  48. }
  49. handleChange(event) {
  50. const keyword = event.target.value;
  51. this.setState({keyword});
  52. }
  53. render() {
  54. const formClear = this.getFormClearComponent();
  55. return (
  56. <form
  57. action="/_search"
  58. className="search-form form-group input-group search-top-input-group"
  59. >
  60. <div className="input-group">
  61. <input
  62. autocomplete="off"
  63. type="text"
  64. className="search-top-input form-control"
  65. placeholder="Search ... Page Title (Path) and Content"
  66. name="q"
  67. value={this.state.keyword}
  68. onFocus={this.handleFocus}
  69. onBlur={this.handleBlur}
  70. onChange={this.handleChange}
  71. />
  72. {formClear}
  73. <span className="input-group-btn">
  74. <button type="submit" className="btn btn-default">
  75. <i className="search-top-icon fa fa-search"></i>
  76. </button>
  77. </span>
  78. </div>// /.input-group
  79. </form>
  80. );
  81. }
  82. }
  83. SearchForm.propTypes = {
  84. onSearchFormChanged: React.PropTypes.func.isRequired,
  85. isShown: React.PropTypes.func.isRequired,
  86. pollInterval: React.PropTypes.number,
  87. };
  88. SearchForm.defaultProps = {
  89. pollInterval: 1000,
  90. };