SearchForm.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import React from 'react';
  2. import { FormGroup, FormControl, DropdownButton, MenuItem, Button, Checkbox, InputGroup } from 'react-bootstrap';
  3. import { AsyncTypeahead } from 'react-bootstrap-typeahead';
  4. // Header.SearchForm
  5. export default class SearchForm extends React.Component {
  6. constructor(props) {
  7. super(props);
  8. this.state = {
  9. keyword: '',
  10. searchedKeyword: '',
  11. };
  12. this.search = this.search.bind(this);
  13. this.clearForm = this.clearForm.bind(this);
  14. this.getFormClearComponent = this.getFormClearComponent.bind(this);
  15. }
  16. componentDidMount() {
  17. }
  18. componentWillUnmount() {
  19. }
  20. search(keyword) {
  21. this.setState({keyword});
  22. if (this.state.searchedKeyword != keyword) {
  23. this.props.onSearchFormChanged({keyword});
  24. this.setState({searchedKeyword: keyword});
  25. }
  26. }
  27. getFormClearComponent() {
  28. let isHidden = (this.state.keyword.length === 0);
  29. return isHidden ? <span></span> : (
  30. <a className="btn btn-link search-top-clear" onClick={this.clearForm} hidden={isHidden}>
  31. <i className="fa fa-times-circle" />
  32. </a>
  33. );
  34. }
  35. clearForm() {
  36. this._typeahead.getInstance().clear();
  37. this.setState({keyword: ''});
  38. }
  39. render() {
  40. const formClear = this.getFormClearComponent();
  41. return (
  42. <form
  43. action="/_search"
  44. className="search-form form-group input-group search-top-input-group"
  45. >
  46. <FormGroup>
  47. <InputGroup>
  48. <AsyncTypeahead
  49. ref={ref => this._typeahead = ref}
  50. name="q"
  51. placeholder="Search ... Page Title (Path) and Content"
  52. submitFormOnEnter={true}
  53. onSearch={this.search}
  54. />
  55. {formClear}
  56. <InputGroup.Button>
  57. <Button type="submit">
  58. <i className="search-top-icon fa fa-search"></i>
  59. </Button >
  60. </InputGroup.Button>
  61. </InputGroup>
  62. </FormGroup>
  63. </form>
  64. );
  65. }
  66. }
  67. SearchForm.propTypes = {
  68. onSearchFormChanged: React.PropTypes.func.isRequired,
  69. };