2
0

SearchForm.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. if (this.state.keyword !== '') {
  30. return <a className="search-top-clear" onClick={this.clearForm}><i className="fa fa-times-circle" /></a>;
  31. } else {
  32. return '';
  33. }
  34. }
  35. clearForm() {
  36. this.setState({keyword: ''});
  37. this.search();
  38. }
  39. searchFieldTicker() {
  40. this.search();
  41. }
  42. handleFocus(event) {
  43. this.props.isShown(true);
  44. }
  45. handleBlur(event) {
  46. //this.props.isShown(false);
  47. }
  48. handleChange(event) {
  49. const keyword = event.target.value;
  50. this.setState({keyword});
  51. }
  52. render() {
  53. const formClear = this.getFormClearComponent();
  54. return (
  55. <form
  56. action="/_search"
  57. className="search-form form-group input-group search-top-input-group"
  58. >
  59. <input
  60. autocomplete="off"
  61. type="text"
  62. className="search-top-input form-control"
  63. placeholder="Search ... Page Title (Path) and Content"
  64. name="q"
  65. value={this.state.keyword}
  66. onFocus={this.handleFocus}
  67. onBlur={this.handleBlur}
  68. onChange={this.handleChange}
  69. />
  70. <span className="input-group-btn">
  71. <button type="submit" className="btn btn-default">
  72. <i className="search-top-icon fa fa-search"></i>
  73. </button>
  74. </span>
  75. {formClear}
  76. </form>
  77. );
  78. }
  79. }
  80. SearchForm.propTypes = {
  81. onSearchFormChanged: React.PropTypes.func.isRequired,
  82. isShown: React.PropTypes.func.isRequired,
  83. pollInterval: React.PropTypes.number,
  84. };
  85. SearchForm.defaultProps = {
  86. pollInterval: 1000,
  87. };