index.jsx 2.5 KB

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