index.jsx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import React,{Component} from 'react';
  2. import classNames from 'classnames';
  3. import ReactDom from "react-dom";
  4. import style from "./index.less";
  5. /****
  6. * 搜索结果下拉
  7. * 接收参数:
  8. * data: json数组
  9. * show:true/false
  10. * textKey: 选项文字字段名
  11. * idKey: 选项id字段名
  12. * type: text选项为纯文本,label选项为标签
  13. * onSelect: 选中事件
  14. * showType:1-本体,2-同义词,3-子项,同义词展示在括号内
  15. * retrievalName:同义词
  16. * name:本体
  17. **/
  18. class SearchDrop extends Component{
  19. constructor(props){
  20. super(props);
  21. this.handleSelect = this.handleSelect.bind(this);
  22. this.state={
  23. data:props.data||[]
  24. }
  25. }
  26. getClass(){
  27. let name = style['text-list'];
  28. let isHide = this.props.show?'':style['hide'];
  29. switch (this.props.type){
  30. case 'text':
  31. name = style['text-list'];
  32. break;
  33. case 'label':
  34. name = style['label-list'];
  35. break;
  36. default:
  37. name = style['text-list'];
  38. }
  39. return classNames(style['list'],name,isHide);
  40. }
  41. getStyle(){
  42. const {left,top} = this.props;
  43. return {
  44. left:left?left+'px':'',
  45. top:top?top+'px':''
  46. }
  47. }
  48. handleSelect(e,item){
  49. // onClick事件换成onmouseup--点击清除后谷歌下搜索结果点击不上去的情况
  50. e.stopPropagation();
  51. const {onSelect,onShow} = this.props;
  52. onSelect&&onSelect(item);
  53. // onShow&&onShow(e,false);
  54. }
  55. render(){
  56. const {mainEmpty} = this.props;
  57. let litext = '';
  58. if(mainEmpty){
  59. return <div className={this.getClass()} contenteditable="false" id="searchBox" style={this.getStyle()}>
  60. <ul>
  61. {this.props.data&&this.props.data.map((it)=>{
  62. litext = it.showType==1?it.name:it.name+'('+it.retrievalName+')';
  63. return <li onClick={(e)=>this.handleSelect(e,it)} title={litext}>{litext}</li>
  64. })}
  65. </ul>
  66. </div>
  67. }else{
  68. const domNode = document.getElementById('root');
  69. return ReactDom.createPortal(
  70. <div className={this.getClass()} contenteditable="false" id="searchBox" style={this.getStyle()}>
  71. <ul>
  72. {this.props.data&&this.props.data.map((it)=>{
  73. litext = it.showType==1?it.name:it.name+'('+it.retrievalName+')';
  74. return <li onClick={(e)=>this.handleSelect(e,it)} title={litext}>{litext}</li>
  75. })}
  76. </ul>
  77. </div>
  78. ,domNode)
  79. }
  80. }
  81. }
  82. export default SearchDrop;