index.jsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 { clearSearch } = this.props;
  38. this.textInput.current.value = '';
  39. this.textInput.current.focus();
  40. this.setState({
  41. val:'',
  42. show:false
  43. });
  44. clearSearch&&clearSearch();
  45. }
  46. handleSearchSelect(e,item){
  47. e.stopPropagation();
  48. e.preventDefault();
  49. const {cliIndex,chooseSearch,clearSearch,onSelect} = this.props;
  50. onSelect();//上一级的“确定”
  51. /*this.setState({
  52. val:'',
  53. show:false
  54. });*/
  55. setTimeout(()=>{
  56. chooseSearch&&chooseSearch({item:item,index:cliIndex})
  57. },200)
  58. clearSearch&&clearSearch();
  59. this.textInput.current.value = "";
  60. }
  61. handleInput(e){
  62. e.stopPropagation();
  63. e.preventDefault();
  64. const { getSearchData ,mainIds} = this.props;
  65. clearTimeout(this.state.timer);
  66. let timer = setTimeout(()=>{
  67. clearTimeout(this.state.timer);
  68. if(e.target.value.trim() == ''){
  69. this.setState({
  70. show:false
  71. })
  72. return
  73. }
  74. this.setState({
  75. val:e.target.value,
  76. show:true
  77. })
  78. getSearchData && getSearchData({inpStr:e.target.value.replace('<br>',''),boxMark:1,itemType:0,mainIds:mainIds});
  79. },config.delayTime);
  80. this.setState({
  81. timer
  82. });
  83. }
  84. handleFocus(e){
  85. e.stopPropagation();
  86. e.preventDefault();
  87. if(this.state.val.trim() != ''){
  88. return;
  89. }else{
  90. this.setState({border:true})
  91. }
  92. }
  93. handleBlur(e){
  94. e.stopPropagation();
  95. e.preventDefault();
  96. this.setState({border:false,val:''})
  97. }
  98. clickIcon(){
  99. this.setState({showBox:true,val:''})
  100. setTimeout(()=>{
  101. this.textInput.current.focus();
  102. },0)
  103. }
  104. componentWillReceiveProps(next){
  105. // 隐藏时,清空搜索框内文字,清空搜索结果,隐藏搜索框
  106. const { clearSearch } = this.props;
  107. if(!next.show && next.show != this.props.show){
  108. clearSearch();
  109. this.textInput.current.value = "";
  110. this.setState({
  111. showBox:false,
  112. val:'',
  113. border:false,
  114. searchData:[]
  115. })
  116. }
  117. }
  118. render() {
  119. const { children,visible,mainSearchData } = this.props;
  120. const { show ,border, showBox,searchData} = this.state;
  121. const showBd = showBox?styles['borderNone']:'';
  122. const borderCor = border?styles['border']:'';
  123. const isShow = showBox?styles['show']:styles['hide'];
  124. let litext = '';
  125. const contStyle={
  126. opacity:'0.4',
  127. right:'0',
  128. top:'1px',
  129. zIndex:'15',
  130. width:'14px',
  131. background:'#f1f1f1'};
  132. const barStyle={background:'#777',width:'100%'};
  133. return (
  134. <div className={classNames(styles['search'])} onClick={(e)=>{e.stopPropagation();}} onBlur={(e)=>{e.stopPropagation();}}>
  135. <img className={styles.searchVal} src={search} alt="搜索" onClick={this.clickIcon}/>
  136. <img style={{display:show?'block':'none'}} className={styles.clearVal} src={clear} onClick={this.handleClearVal} alt="清空" />
  137. <input
  138. className={classNames(isShow,showBd,borderCor)}
  139. type="text"
  140. maxLength="30"
  141. ref={this.textInput}
  142. onFocus={this.handleFocus}
  143. onBlur={this.handleBlur}
  144. onInput={(e) => {
  145. this.handleInput(e)
  146. }}
  147. onPropertyChange={(e) => { // 兼容ie
  148. this.handleInput(e)
  149. }}
  150. />
  151. {mainSearchData&&mainSearchData.length>0 ? <div className={styles.autoList}>
  152. <ScrollArea speed={0.8}
  153. horizontal={false}
  154. stopScrollPropagation={mainSearchData.length>6?true:false}
  155. style={{maxHeight:'225px'}}
  156. verticalContainerStyle={contStyle}
  157. verticalScrollbarStyle={barStyle}
  158. contentClassName="content">
  159. <ul>
  160. {mainSearchData&&mainSearchData.map((it)=>{
  161. litext = it.showType==1?it.name:it.name+'('+it.retrievalName+')';
  162. return <li key={it.conceptId} onClick={(e)=>this.handleSearchSelect(e,it)} title={litext}>{litext}</li>
  163. })}
  164. </ul>
  165. </ScrollArea>
  166. </div>:''}
  167. </div>
  168. )
  169. }
  170. }
  171. /*SearchBox.defaultProps = {
  172. visible: false
  173. };
  174. SearchBox.propTypes = {
  175. visible:PropTypes.bool,
  176. handleChangeValue: PropTypes.func
  177. };*/
  178. export default SearchBox;