index.jsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import React,{Component} from 'react';
  2. import {handleEnter,windowEventHandler,setFontColorSize} from '@utils/tools.js';
  3. import {DropList} from '@commonComp';
  4. import classNames from 'classnames';
  5. import style from "./index.less";
  6. import $ from "jquery";
  7. /****
  8. * 单选下拉
  9. * author:zn@2018-11.13
  10. * 接收参数:
  11. * placeholder:灰显文字
  12. * data:下拉内容数据
  13. * hideTag:是否隐藏括号
  14. * handleSelect:选中事件
  15. * prefix:前缀
  16. * suffix:后缀
  17. *
  18. * ***/
  19. class RadioDrop extends Component{
  20. constructor(props){
  21. super(props);
  22. this.state={
  23. editable:false,
  24. timer:null,
  25. tmpDom:null
  26. };
  27. this.$cont = React.createRef();
  28. this.isIE = navigator.appName=="Microsoft Internet Explorer" && navigator.appVersion.split(";")[1].replace(/[ ]/g,"")=="MSIE8.0";
  29. this.handleSelect = this.handleSelect.bind(this);
  30. this.handleShow = this.handleShow.bind(this);
  31. this.handledbClick = this.handledbClick.bind(this);
  32. this.handleEditLabel = this.handleEditLabel.bind(this);
  33. }
  34. getClass(){
  35. const {value,hideTag,show,isImports,isExtBlue} = this.props;
  36. const ext = isExtBlue?style['ext']:'';
  37. const orgBorder = isImports?style['orange-border']:''; //橙色框高亮
  38. const editBorder = this.state.editable?style['blue-border']:'';
  39. if(show){
  40. $(this.$cont.current).addClass(style['borderd']);
  41. }else{
  42. $(this.$cont.current).removeClass(style['borderd']);
  43. }
  44. // if(hideTag){
  45. // return classNames(style['no-tag'],ext,setFontColorSize(isExtBlue?2:''));
  46. // }
  47. if(value){
  48. if(hideTag){
  49. return classNames(style['no-tag'],ext,setFontColorSize(isExtBlue?2:''));
  50. }
  51. return classNames(style['selected-tag'],ext,editBorder,setFontColorSize());
  52. }else{
  53. if(hideTag){
  54. return isExtBlue?classNames(setFontColorSize(2)):classNames(setFontColorSize(2),'prefixUnsetColor')
  55. }
  56. return classNames(style['tag'],orgBorder,ext,setFontColorSize(1));
  57. }
  58. }
  59. handleSelect(item){
  60. const {handleSelect,ikey,mainSaveText,value} = this.props;
  61. if(!item){ //清空
  62. handleSelect&&handleSelect({ikey});
  63. return ;
  64. }
  65. let text = '';
  66. switch(true){
  67. case +item.controlType ===6:
  68. case +item.controlType===7:
  69. case +item.tagType===3:
  70. text = item;
  71. break;
  72. case 3:
  73. default:
  74. // text = item.labelPrefix+item.name+item.labelSuffix;
  75. text = item.name;
  76. // text = item.questionDetailList&&item.questionDetailList.length>0?item.questionDetailList[0].name: item.name;
  77. }
  78. handleSelect&&handleSelect({ikey,id:item.id,text,mainSaveText,value});
  79. }
  80. handleShow(e){
  81. // e.stopPropagation();
  82. const {handleShow,ikey,id,patId} = this.props;
  83. const that = this;
  84. const timer = setTimeout(()=>{
  85. if (that.state.editable) {//如果处于编辑状态点击不显示下拉框
  86. return
  87. }else {
  88. document.activeElement.blur()//chrome41有效,但是失去焦点的span仍能编辑
  89. $(e.target).parent().prev().attr({"contentEditable":false})
  90. this.setState({
  91. tmpDom:e.target
  92. })
  93. handleShow && handleShow({ikey,id:patId||id});
  94. }
  95. },300);
  96. this.setState({
  97. timer,
  98. });
  99. }
  100. componentDidMount(){ //默认值选中
  101. const {data,ikey,handleSelect,hideTag,mainSaveText,value,boxMark,readDefault,confDefault,isGeneray} = this.props;
  102. const showDefault = readDefault===-1||readDefault===undefined?confDefault:readDefault;
  103. const configed = (+showDefault===1&&isGeneray)||!isGeneray; //是一般情况且设置默认值显示或者不是一般情况
  104. const selected = data.find((it)=>{
  105. return it.selected === undefined&&+it.defaultSelect===1&&configed;
  106. });
  107. if(boxMark!=1&&!hideTag&&selected){
  108. // const text = selected.labelPrefix+selected.name+selected.labelSuffix;
  109. const text = selected.name;
  110. handleSelect&&handleSelect({ikey,id:selected.id,text,mainSaveText,value});
  111. }
  112. }
  113. handleEditLabel(e){
  114. e.stopPropagation();
  115. if(!this.state.editable){ //ie8点开下拉未选值存值bug修改
  116. return;
  117. }
  118. const {ikey,boxMark,handleLabelEdit} = this.props;
  119. this.setState({
  120. editable:false
  121. });
  122. // 更改标签的value值
  123. const ev = e || window.event;
  124. let changeVal = ev.target.innerText || ev.target.innerHTML;
  125. //fireFox括号删完有<br>2270
  126. changeVal=changeVal=="<br>"?'':changeVal;
  127. if(!this.isIE){
  128. ev.target.innerText?(ev.target.innerText = ''):(ev.target.innerHTML = '');
  129. }
  130. handleLabelEdit && handleLabelEdit({ikey,changeVal,type:boxMark});
  131. }
  132. handledbClick(e){
  133. window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
  134. const {value,id,handleDbclick,patId} = this.props;
  135. clearTimeout(this.state.timer);//取消延时的单击事件
  136. //e.preventDefault();
  137. if(value&&value.trim()) {//有选中值的标签才能双击编辑
  138. this.setState({
  139. editable: true
  140. });
  141. };
  142. //失焦关闭编辑状态
  143. setTimeout(()=>{
  144. e.target.focus();
  145. })
  146. handleDbclick&&handleDbclick({id:patId||id});
  147. }
  148. render(){
  149. const {data,placeholder,show,value,hideTag,boxMark} = this.props;
  150. const {tmpDom} = this.state
  151. if(!show&&tmpDom){
  152. $(tmpDom).parent().prev().attr({"contentEditable":true})
  153. }
  154. return <div className={`${style['container']} prefixUnsetColor`} ref = {this.$cont}>
  155. <div className={this.getClass()}
  156. onBlur={this.handleEditLabel}
  157. contentEditable={this.state.editable}
  158. onDoubleClick={hideTag?null:this.handledbClick}
  159. onFocus={(e)=>{e.stopPropagation()}}
  160. onClick={(e)=>this.handleShow(e,true)}
  161. onKeyDown={handleEnter}>
  162. {value||placeholder}
  163. </div>
  164. <DropList onSelect={this.handleSelect} boxMark={boxMark} data={data} show={show} hideTag={hideTag}/>
  165. </div>
  166. }
  167. }
  168. export default RadioDrop;