index.jsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. const {handleClick} = this.props;
  33. handleClick && handleClick(e);//为了获取鼠标位置,显示搜索结果框;
  34. }
  35. handleInput(e){
  36. const {handleChange,data} = this.props;
  37. if(!data || (data.length==0)){//避免结构化下触发onchange,导致下拉要点两下
  38. handleChange&&handleChange(e)
  39. }
  40. }
  41. componentDidMount(){
  42. const {setRef} = this.props;
  43. this.$div.current.innerHTML=''; //bug2276,FF26初始光标位置问题
  44. setRef&&setRef(this.$div);
  45. if(isIE()){
  46. $(this.$div.current).onIe8Input(function(e){
  47. this.handleInput(e)
  48. },this);
  49. }
  50. }
  51. componentWillReceiveProps(nextP){
  52. if(nextP.children.length>1){
  53. const that = this;
  54. //数据渲染后更新滚动条
  55. setTimeout(function(){
  56. that.context.scrollArea&&that.context.scrollArea.refresh();
  57. });
  58. }
  59. }
  60. render(){
  61. const {title,children,editable,className,handleFocus,fuzhen,border,handleBlur,titleTop,backgroundColor,boxId} = this.props;
  62. return <div className={style["box"]+" "+"clearfix"}>
  63. <div className={`${style["title"]} ${setFontColorSize(2,4)}` + ' '+(className||'')} style={{marginTop:titleTop?'22px':''}}>{title}</div>
  64. <div ref={this.$div} className={`${style["content"]} ${border?style["border"]:''} ${backgroundColor?style["noBorder"]:'' } ${setFontColorSize(2,5)}`} contentEditable={editable} style={this.getBoxStyle()} onFocus={editable?handleFocus:null} onInput={this.handleInput} onClick={(e)=>{this.handleClick(e);}} onBlur={handleBlur} id={boxId} onkeydown={handleEnter}>
  65. {fuzhen?children||fuzhen:children}
  66. </div>
  67. </div>
  68. }
  69. }
  70. ItemBox.contextTypes = {
  71. scrollArea: React.PropTypes.object
  72. };
  73. export default ItemBox;