index.jsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import React, { Component } from 'react';
  2. import style from './index.less';
  3. import close from './img/close.png'
  4. import search from './img/search.png'
  5. import DiagnosticItem from '@containers/DiagnosticItem'
  6. import config from '@config/index';
  7. import $ from 'jquery';
  8. class DiagResultSearch extends Component {
  9. constructor(props) {
  10. super(props);
  11. this.state = {
  12. showClose: false,
  13. focus: true,
  14. timer:null
  15. };
  16. this.searchInput = React.createRef();
  17. this.handleInput = this.handleInput.bind(this);
  18. this.clearInput = this.clearInput.bind(this);
  19. this.handleFocus = this.handleFocus.bind(this);
  20. this.handleBlur = this.handleBlur.bind(this);
  21. }
  22. componentWillReceiveProps(){
  23. const { searchResult, clearSearchResult } = this.props;
  24. this.setState({focus: true})
  25. if( searchResult && searchResult.length > 0) {
  26. clearSearchResult();//清除上次搜索结果
  27. }
  28. }
  29. componentDidMount () {
  30. const that = this
  31. $(document).click(function (e) {
  32. var diagSearch=$('#diagSearch')[0];
  33. var addDiag = $('#addDiag')[0];
  34. var confirm = $('#confirm')[0];
  35. if(diagSearch) {
  36. if(confirm) {
  37. } else {
  38. if (e.target!= diagSearch && e.target!= addDiag && e.target.parentNode!= addDiag && !$.contains(diagSearch, e.target) ) {
  39. that.setState({
  40. showSearch: !that.props.showSearch
  41. });
  42. that.clearInput()
  43. that.props.hideSearch();
  44. }
  45. }
  46. }
  47. })
  48. }
  49. handleInput(value) {
  50. const { setSearchValue, getSearchResult } = this.props;
  51. value.length > 0 ? this.setState({showClose: true}) : this.setState({showClose: false});
  52. clearTimeout(this.state.timer);
  53. let timer = setTimeout(()=>{
  54. clearTimeout(this.state.timer);
  55. if(value.trim() == ''){
  56. return getSearchResult('');
  57. }
  58. getSearchResult(value.trim());
  59. },config.delayTime);
  60. this.setState({
  61. timer
  62. });
  63. }
  64. clearInput() {
  65. const { setSearchValue, getSearchResult } = this.props;
  66. this.searchInput.current.value = '';
  67. getSearchResult('');
  68. this.setState({showClose: false})
  69. }
  70. handleFocus() {
  71. this.setState({focus: true})
  72. }
  73. handleBlur() {
  74. this.setState({focus: false})
  75. }
  76. render(){
  77. const { showClose } = this.state
  78. const { show, searchResult, getSearchResult } = this.props
  79. return(
  80. show && <div id='diagSearch' className={style['search-box']}>
  81. <div className={style['search']}>
  82. <input ref={this.searchInput}
  83. type="text"
  84. placeholder='搜索'
  85. onFocus={this.handleFocus}
  86. onBlur = {this.handleBlur}
  87. className={style['search-input']}
  88. style = {this.state.focus ? {border: '1px solid #3B9ED0'} :{border: '1px solid #979797'}}
  89. onInput={(e)=>this.handleInput(e.target.value)}
  90. onPropertyChange={(e)=>this.handleInput(e.target.value)}
  91. />
  92. <img className={style['search-img']} src={search} alt="搜索"/>
  93. <img className={style['search-close']} onClick={this.clearInput} style={showClose ? '': {display: 'none'}} src={close} alt=""/>
  94. </div>
  95. <div className={style['search-result']}>
  96. {
  97. searchResult && searchResult.map((item) => {
  98. return(<div key={item.id} className={style['search-result-item']}><DiagnosticItem title={true} item={item} clearInput={this.clearInput}/></div>)
  99. })
  100. }
  101. </div>
  102. </div>
  103. )
  104. }
  105. }
  106. export default DiagResultSearch;