index.jsx 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import React,{Component} from 'react';
  2. import style from './index.less';
  3. import store from '@store';
  4. import {isIE,handleEnter,setFontColorSize} from '@utils/tools.js';
  5. import $ from 'jquery';
  6. /***
  7. * author:zn@2018-11-13
  8. * 电子病历项内容框
  9. * 接收参数:
  10. * boxHeight:框高度
  11. * boxWidth:框宽度
  12. * title:框名称(如主诉、现病史)
  13. * editable:是否可编辑
  14. * children:框内内容
  15. * border:边框样式,
  16. * style
  17. *
  18. * ***/
  19. class ItemBox extends Component {
  20. constructor(props){
  21. super(props);
  22. this.$div = React.createRef();
  23. this.handleClick = this.handleClick.bind(this);
  24. this.handleInput = this.handleInput.bind(this);
  25. }
  26. getBoxStyle(){
  27. const {boxHeight,boxWidth,boxLineHeight,marginTop,backgroundColor,style} = this.props;
  28. const sty = {width:boxWidth?boxWidth:undefined,height:boxHeight?boxHeight:undefined,lineHeight:boxLineHeight?boxLineHeight:'22px',marginTop:marginTop,backgroundColor:backgroundColor?backgroundColor:''};
  29. return style?Object.assign(style,sty):sty;
  30. }
  31. handleClick(e){
  32. e.stopPropagation();
  33. const {handleClick,hideAllDrop} = this.props;
  34. handleClick && handleClick(e);//为了获取鼠标位置,显示搜索结果框;
  35. hideAllDrop&&hideAllDrop();
  36. }
  37. handleInput(e){
  38. const {handleChange,data} = this.props;
  39. if(!data || (data.length==0)){//避免结构化下触发onchange,导致下拉要点两下
  40. handleChange&&handleChange(e)
  41. }
  42. }
  43. componentDidMount(){
  44. const {setRef} = this.props;
  45. this.$div.current.innerHTML=''; //bug2276,FF26初始光标位置问题
  46. setRef&&setRef(this.$div);
  47. if(isIE()){
  48. $(this.$div.current).onIe8Input(function(e){
  49. this.handleInput(e)
  50. },this);
  51. }
  52. }
  53. componentWillReceiveProps(nextP){
  54. if(nextP.children.length>1){
  55. const that = this;
  56. //数据渲染后更新滚动条
  57. setTimeout(function(){
  58. that.context.scrollArea&&that.context.scrollArea.refresh();
  59. });
  60. }
  61. }
  62. setFontColorSize(){
  63. const {data,saveText,title} = this.props;
  64. if(data&&data.length==0&&saveText&&saveText[0]){
  65. return setFontColorSize(2,7)
  66. }
  67. return setFontColorSize(2,5)
  68. }
  69. render(){
  70. const {title,children,editable,className,handleFocus,fuzhen,border,handleBlur,titleTop,backgroundColor,boxId} = this.props;
  71. return <div className={style["box"]+" "+"clearfix"}>
  72. <div className={`${style["title"]} ${setFontColorSize(2,4)}` + ' '+(className||'')} style={{marginTop:titleTop?'22px':''}}>{title}</div>
  73. <div ref={this.$div} className={`${style["content"]} ${border?style["border"]:''} ${backgroundColor?style["noBorder"]:'' } ${this.setFontColorSize()}`} contentEditable={editable} style={this.getBoxStyle()} onFocus={editable?handleFocus:null} onInput={this.handleInput} onClick={(e)=>{this.handleClick(e);}} onBlur={handleBlur} id={boxId} onkeydown={handleEnter}>
  74. {fuzhen?children||fuzhen:children}
  75. </div>
  76. </div>
  77. }
  78. }
  79. ItemBox.contextTypes = {
  80. scrollArea: React.PropTypes.object
  81. };
  82. export default ItemBox;