index.jsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import React, { Component } from 'react';
  2. import style from './index.less';
  3. import delIcon from '@common/images/del_nor.png';
  4. import {windowEventHandler,getCurrentDate,getWindowInnerHeight} from '@utils/tools'
  5. class ScaleSearch extends Component {
  6. constructor(props) {
  7. super(props);
  8. this.$inp = React.createRef();
  9. this.$cont = React.createRef();
  10. this.$ul = React.createRef();
  11. this.state={
  12. val:'',
  13. hasSearch: false,
  14. msg:''
  15. };
  16. this.search = this.search.bind(this);
  17. this.handleChange = this.handleChange.bind(this);
  18. this.clear = this.clear.bind(this);
  19. this.handleEnter = this.handleEnter.bind(this);
  20. }
  21. getResult(id){
  22. const {formulaResult} = this.props;
  23. const scale=formulaResult&&formulaResult[id];
  24. let result = scale&&scale.calcalculate?scale.calcalculate.result:null;
  25. if(result){
  26. return <i>( 结果:{result.value} {result.text} )</i>;
  27. }
  28. return '';
  29. }
  30. showScale(item){
  31. const {scaleInfo,getScale,showScaleFn} = this.props;
  32. if(scaleInfo&&scaleInfo[item.conceptId]){
  33. showScaleFn&&showScaleFn(item);
  34. }else{
  35. getScale(item);
  36. }
  37. }
  38. getSearchList() {
  39. const { searchResult } = this.props;
  40. const that = this;
  41. if(searchResult&&searchResult.length>0){
  42. setTimeout(function(){
  43. that.$ul.current.style.height = getWindowInnerHeight()-248+'px';
  44. },100);
  45. }
  46. return searchResult && searchResult.map((item) => {
  47. return <li key={item.conceptId}
  48. title='点击查看详情'
  49. onClick={this.showScale.bind(this,{conceptId:item.conceptId,name:item.name})}>
  50. <span className={style['scale-name']}>{item.name}{this.getResult(item.conceptId)}</span>
  51. {item.showType===0?<p>• {item.searchName}</p>:''}
  52. {/*<button>查看</button>*/}
  53. </li>;
  54. });
  55. }
  56. search(){
  57. if(this.state.hasSearch === false){
  58. this.setState({
  59. hasSearch: true,
  60. msg:'暂无搜索结果!'
  61. })
  62. }
  63. const {handleChangeValue} = this.props;
  64. const val = this.$inp.current.value;
  65. handleChangeValue&&handleChangeValue(val);
  66. }
  67. handleChange(){
  68. const value = this.$inp.current.value;
  69. const {clearResult} = this.props;
  70. this.setState({
  71. val: value
  72. });
  73. if (value === '') {
  74. this.setState({
  75. val: '',
  76. hasSearch: false,
  77. msg: ''
  78. });
  79. clearResult && clearResult();
  80. }
  81. }
  82. handleEnter(e){
  83. if(e.keyCode==13){
  84. this.search();
  85. }
  86. }
  87. clear(){
  88. const {clearResult} = this.props;
  89. this.$inp.current.value = '';
  90. this.setState({
  91. val:'',
  92. hasSearch: false,
  93. msg:''
  94. });
  95. this.$inp.current.focus();
  96. clearResult&&clearResult();
  97. }
  98. componentDidMount(){
  99. const height = getWindowInnerHeight()-148;
  100. this.$cont.current.style.height = height+"px";
  101. windowEventHandler('resize', ()=>{
  102. if(this.$cont.current){
  103. const height = getWindowInnerHeight()-148;
  104. this.$cont.current.style.height = height+"px";
  105. }
  106. if(this.$ul.current){
  107. const height = getWindowInnerHeight()-248;
  108. this.$ul.current.style.height = height+"px";
  109. }
  110. });
  111. }
  112. componentWillReceiveProps(){
  113. this.setState({
  114. hasSearch: false
  115. });
  116. }
  117. render() {
  118. const {searchResult} = this.props;
  119. const {val, hasSearch,msg} = this.state;
  120. return (
  121. <div className={style['search-cont']} ref={this.$cont}>
  122. <div className={style['search-box']}>
  123. <p className={style['cont']}>
  124. <input type="text" placeholder="量表搜索" className={style['input']} ref={this.$inp} onInput={this.handleChange} onKeyUp={this.handleEnter}/>
  125. {val?<img src={delIcon} alt="清空" onClick={this.clear}/>:''}
  126. <button onClick={this.search}>搜索</button>
  127. </p>
  128. </div>
  129. {searchResult&&searchResult.length>0?<div className={style['result']}>
  130. <p className={style['title']}>搜索内容</p>
  131. <ul ref={this.$ul}>
  132. {this.getSearchList()}
  133. </ul>
  134. </div>:<p className={style['no-data']}>{hasSearch?'搜索中...':msg}</p>}
  135. </div>
  136. )
  137. }
  138. }
  139. export default ScaleSearch;