index.jsx 3.7 KB

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