index.jsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 slide from '../../images/show.png';
  6. import PropTypes from "prop-types";
  7. import config from '@config/index';
  8. import $ from 'jquery';
  9. /**
  10. * 前提条件父元素有定位
  11. * visible 搜索显示隐藏
  12. * handleChangeValue 函数参数为输入框的value值
  13. */
  14. class SearchOption extends React.Component {
  15. constructor(props) {
  16. super(props);
  17. this.state = {
  18. val:'',
  19. border:'',
  20. show:false,
  21. timer:null,
  22. showInsp:false,
  23. txt:''
  24. }
  25. this.textInput = React.createRef();
  26. this.handleClearVal = this.handleClearVal.bind(this);
  27. this.handleFocus = this.handleFocus.bind(this);
  28. this.handleBlur = this.handleBlur.bind(this);
  29. this.setShowInsp = this.setShowInsp.bind(this);
  30. this.detailClick = this.detailClick.bind(this);
  31. }
  32. componentDidMount(){
  33. this.props.handleChangeValue('');
  34. const {windowHeight,pageTop,height,refreshScroller,searchType} = this.props;
  35. if(searchType==1){
  36. this.setState({txt:'细项'})
  37. }else if(searchType==2){
  38. this.setState({txt:'药品'})
  39. }
  40. refreshScroller()&&refreshScroller().refresh(); //点开搜索弹窗更新滚动条,
  41. if(windowHeight - pageTop < height){
  42. //$("#searchOption")[0].scrollIntoView(false);
  43. refreshScroller()&&refreshScroller().scrollBottom();
  44. }
  45. this.textInput.current.focus();
  46. }
  47. setShowInsp(){
  48. this.setState({
  49. showInsp:!this.state.showInsp
  50. })
  51. }
  52. handleClearVal(){
  53. const { handleChangeValue } = this.props;
  54. this.textInput.current.value = '';
  55. this.textInput.current.focus();
  56. this.setState({
  57. val:'',
  58. show:false
  59. });
  60. handleChangeValue('','clear');
  61. }
  62. detailClick(val,txt){
  63. const { detailClick,handleChangeValue } = this.props;
  64. detailClick(val)
  65. this.setState({txt:txt})
  66. let value = this.textInput.current.value||''
  67. if(value.trim() == ''){
  68. this.setState({
  69. show:false
  70. })
  71. return handleChangeValue('');
  72. }
  73. this.setState({
  74. show:true
  75. })
  76. handleChangeValue(value)
  77. }
  78. handleInput(e){
  79. const { handleChangeValue } = this.props;
  80. clearTimeout(this.state.timer);
  81. let timer = setTimeout(()=>{
  82. clearTimeout(this.state.timer);
  83. if(e.target.value.trim() == ''){
  84. this.setState({
  85. show:false
  86. })
  87. return handleChangeValue('');
  88. }
  89. this.setState({
  90. val:e.target.value,
  91. show:true
  92. })
  93. handleChangeValue(e.target.value);
  94. },config.delayTime);
  95. this.setState({
  96. timer
  97. });
  98. }
  99. handleFocus(){
  100. this.setState({showInsp:false})
  101. if(this.state.val.trim() != ''){
  102. return;
  103. }else{
  104. this.setState({border:true})
  105. }
  106. }
  107. handleBlur(){
  108. this.setState({border:false,val:''})
  109. }
  110. render() {
  111. const { children,visible,searchType } = this.props;
  112. const { show,showInsp,txt } = this.state;
  113. return (
  114. searchType == 1||searchType == 2?
  115. <div id="searchOption" className={visible?`${styles.search} ${styles.searchSpecial} ${styles.show} searchOption`:`${styles.search} ${styles.searchSpecial} ${styles.hide} searchOption`}>
  116. <img className={`${styles.searchVal} ${styles.seleImg}`} src={slide} alt="选择" onClick={this.setShowInsp} />
  117. <img className={`${styles.searchVal} ${styles.searchInsp}`} src={search} alt="搜索" />
  118. <img style={{display:show?'block':'none'}} className={styles.clearVal} src={clear} onClick={this.handleClearVal} alt="清空" />
  119. <div className={styles.selectLis} onClick={this.setShowInsp}>
  120. <span id="shTxt">{txt}</span>
  121. {
  122. searchType == 1?
  123. <ul className={styles.lisul} style={{'display':showInsp?'block':'none'}}>
  124. <li onClick={()=>{this.detailClick(2,'细项')}}>细项</li>
  125. <li onClick={()=>{this.detailClick(1,'套餐')}}>套餐</li>
  126. </ul>:
  127. <ul className={styles.lisul} style={{'display':showInsp?'block':'none'}}>
  128. <li onClick={()=>{this.detailClick(5,'药品')}}>药品</li>
  129. <li onClick={()=>{this.detailClick(6,'手术/操作')}}>手术/操作</li>
  130. <li onClick={()=>{this.detailClick(8,'输血')}}>输血</li>
  131. </ul>
  132. }
  133. </div>
  134. <input
  135. className={this.state.border ?`${styles.border}`:`${styles.borderNone}`}
  136. type="text"
  137. maxLength="30"
  138. ref={this.textInput}
  139. onFocus={this.handleFocus}
  140. onBlur={this.handleBlur}
  141. onInput={(e) => {
  142. this.handleInput(e)
  143. }}
  144. onPropertyChange={(e) => { // 兼容ie
  145. this.handleInput(e)
  146. }}
  147. placeholder="搜索"
  148. />
  149. <div style={{'clear':'both'}}></div>
  150. <div className={styles.autoList}>
  151. {children}
  152. </div>
  153. </div>:<div id="searchOption" className={visible?`${styles.search} ${styles.show} searchOption`:`${styles.search} ${styles.hide} searchOption`}>
  154. <img className={styles.searchVal} src={search} alt="搜索" />
  155. <img style={{display:show?'block':'none'}} className={styles.clearVal} src={clear} onClick={this.handleClearVal} alt="清空" />
  156. <input
  157. className={this.state.border ?`${styles.border}`:`${styles.borderNone}`}
  158. type="text"
  159. maxLength="30"
  160. ref={this.textInput}
  161. onFocus={this.handleFocus}
  162. onBlur={this.handleBlur}
  163. onInput={(e) => {
  164. this.handleInput(e)
  165. }}
  166. onPropertyChange={(e) => { // 兼容ie
  167. this.handleInput(e)
  168. }}
  169. placeholder="搜索"
  170. />
  171. <div className={styles.autoList}>
  172. {children}
  173. </div>
  174. </div>
  175. )
  176. }
  177. }
  178. SearchOption.defaultProps = {
  179. visible: false
  180. };
  181. SearchOption.propTypes = {
  182. visible:PropTypes.bool,
  183. handleChangeValue: PropTypes.func
  184. };
  185. export default SearchOption;