index.jsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import React from 'react';
  2. import styles from './index.less';
  3. import clear from './imgs/clear.png';
  4. import search from './imgs/search.png';
  5. import PropTypes from "prop-types";
  6. import config from '@config/index';
  7. import $ from 'jquery';
  8. import classNames from 'classnames';
  9. import SearchDrop from '@components/SearchDrop';
  10. import ScrollArea from 'react-scrollbar';
  11. /**
  12. * 搜索框
  13. * handleChangeValue 函数参数为输入框的value值
  14. */
  15. class SearchBox extends React.Component {
  16. constructor(props) {
  17. super(props);
  18. this.state = {
  19. val:'',
  20. showBox:false, //显示搜索结果
  21. border:'',
  22. show:false, //显示清空icon
  23. timer:null,
  24. searchData:[]
  25. }
  26. this.textInput = React.createRef();
  27. this.handleClearVal = this.handleClearVal.bind(this);
  28. this.handleFocus = this.handleFocus.bind(this);
  29. this.handleBlur = this.handleBlur.bind(this);
  30. this.clickIcon = this.clickIcon.bind(this);
  31. this.handleSearchSelect = this.handleSearchSelect.bind(this);
  32. }
  33. componentDidMount(){
  34. // this.props.handleChangeValue('');
  35. }
  36. handleClearVal(){
  37. const { handleChangeValue } = this.props;
  38. this.textInput.current.value = '';
  39. this.textInput.current.focus();
  40. this.setState({
  41. val:'',
  42. show:false
  43. });
  44. handleChangeValue('');
  45. }
  46. handleSearchSelect(e,item){
  47. e.stopPropagation();
  48. e.preventDefault();
  49. const {cliIndex,chooseSearch,clearSearch} = this.props;
  50. console.log("选择数据:",item);
  51. this.setState({
  52. val:'',
  53. show:false
  54. });
  55. chooseSearch&&chooseSearch({item:item,index:cliIndex})
  56. clearSearch&&clearSearch();
  57. this.textInput.current.innerText = "";
  58. }
  59. handleInput(e){
  60. e.stopPropagation();
  61. e.preventDefault();
  62. const { getSearchData ,mainIds} = this.props;
  63. clearTimeout(this.state.timer);
  64. let timer = setTimeout(()=>{
  65. clearTimeout(this.state.timer);
  66. if(e.target.value.trim() == ''){
  67. this.setState({
  68. show:false
  69. })
  70. return
  71. }
  72. this.setState({
  73. val:e.target.value,
  74. show:true
  75. })
  76. getSearchData && getSearchData({inpStr:e.target.value.replace('<br>',''),boxMark:1,itemType:0,mainIds:mainIds});
  77. },config.delayTime);
  78. this.setState({
  79. timer
  80. });
  81. }
  82. handleFocus(e){
  83. e.stopPropagation();
  84. e.preventDefault();
  85. if(this.state.val.trim() != ''){
  86. return;
  87. }else{
  88. this.setState({border:true})
  89. }
  90. }
  91. handleBlur(e){
  92. e.stopPropagation();
  93. e.preventDefault();
  94. this.setState({border:false,val:''})
  95. }
  96. clickIcon(){
  97. this.setState({showBox:true,val:''})
  98. setTimeout(()=>{
  99. this.textInput.current.focus();
  100. },0)
  101. }
  102. componentWillReceiveProps(next){
  103. // 隐藏时,清空搜索框内文字,清空搜索结果,隐藏搜索框
  104. const { clearSearch } = this.props;
  105. if(!next.show && next.show != this.props.show){
  106. clearSearch();
  107. this.textInput.current.innerText = "";
  108. this.setState({
  109. showBox:false,
  110. val:'',
  111. border:false,
  112. searchData:[]
  113. })
  114. }
  115. }
  116. render() {
  117. const { children,visible,mainSearchData } = this.props;
  118. const { show ,border, showBox,searchData} = this.state;
  119. const showBd = showBox?styles['borderNone']:'';
  120. const borderCor = border?styles['border']:'';
  121. const isShow = showBox?styles['show']:styles['hide'];
  122. let litext = '';
  123. const contStyle={
  124. opacity:'0.4',
  125. right:'0',
  126. top:'1px',
  127. zIndex:'15',
  128. width:'14px',
  129. background:'#f1f1f1'};
  130. const barStyle={background:'#777',width:'100%'};
  131. return (
  132. <div className={classNames(styles['search'])} onClick={(e)=>{e.stopPropagation();}}>
  133. <img className={styles.searchVal} src={search} alt="搜索" onClick={this.clickIcon}/>
  134. <img style={{display:show?'block':'none'}} className={styles.clearVal} src={clear} onClick={this.handleClearVal} alt="清空" />
  135. <input
  136. className={classNames(isShow,showBd,borderCor)}
  137. type="text"
  138. maxLength="30"
  139. ref={this.textInput}
  140. onFocus={this.handleFocus}
  141. onBlur={this.handleBlur}
  142. onInput={(e) => {
  143. this.handleInput(e)
  144. }}
  145. onPropertyChange={(e) => { // 兼容ie
  146. this.handleInput(e)
  147. }}
  148. />
  149. {mainSearchData&&mainSearchData.length>0 ? <div className={styles.autoList}>
  150. <ScrollArea speed={0.8}
  151. horizontal={false}
  152. stopScrollPropagation={mainSearchData.length>6?true:false}
  153. style={{maxHeight:'225px'}}
  154. verticalContainerStyle={contStyle}
  155. verticalScrollbarStyle={barStyle}
  156. contentClassName="content">
  157. <ul>
  158. {mainSearchData&&mainSearchData.map((it)=>{
  159. litext = it.showType==1?it.name:it.name+'('+it.retrievalName+')';
  160. return <li key={it.conceptId} onClick={(e)=>this.handleSearchSelect(e,it)} title={litext}>{litext}</li>
  161. })}
  162. </ul>
  163. </ScrollArea>
  164. </div>:''}
  165. </div>
  166. )
  167. }
  168. }
  169. /*SearchBox.defaultProps = {
  170. visible: false
  171. };
  172. SearchBox.propTypes = {
  173. visible:PropTypes.bool,
  174. handleChangeValue: PropTypes.func
  175. };*/
  176. export default SearchBox;