index.jsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import React,{Component} from 'react';
  2. import {handleEnter,windowEventHandler,setFontColorSize,handleMouseUp} 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,mouseSelect} = 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. const selectedArea = mouseSelect?style['selected-area']:'';
  40. if(show){
  41. $(this.$cont.current).addClass(style['borderd'],);
  42. }else{
  43. $(this.$cont.current).removeClass(style['borderd']);
  44. }
  45. // if(hideTag){
  46. // return classNames(style['no-tag'],ext,setFontColorSize(isExtBlue?2:''));
  47. // }
  48. if(value){
  49. if(hideTag){
  50. return classNames(style['no-tag'],ext,setFontColorSize(isExtBlue?2:''),selectedArea);
  51. }
  52. return classNames(style['selected-tag'],ext,editBorder,setFontColorSize(),selectedArea);
  53. }else{
  54. if(hideTag){
  55. return isExtBlue?classNames(setFontColorSize(2)):classNames(setFontColorSize(2),'prefixUnsetColor',)
  56. }
  57. return classNames(style['tag'],orgBorder,ext,setFontColorSize(1),selectedArea);
  58. }
  59. }
  60. handleSelect(item){
  61. const {handleSelect,ikey,mainSaveText,value} = this.props;
  62. if(!item){ //清空
  63. handleSelect&&handleSelect({ikey});
  64. return ;
  65. }
  66. let text = '';
  67. switch(true){
  68. case +item.controlType ===6:
  69. case +item.controlType===7:
  70. case +item.tagType===3:
  71. text = item;
  72. break;
  73. case 3:
  74. default:
  75. // text = item.labelPrefix+item.name+item.labelSuffix;
  76. text = item.name;
  77. // text = item.questionDetailList&&item.questionDetailList.length>0?item.questionDetailList[0].name: item.name;
  78. }
  79. handleSelect&&handleSelect({ikey,id:item.id,text,mainSaveText,value});
  80. }
  81. handleShow(e){
  82. // e.stopPropagation();
  83. const {handleShow,ikey,id,patId} = this.props;
  84. const that = this;
  85. const timer = setTimeout(()=>{
  86. if (that.state.editable) {//如果处于编辑状态点击不显示下拉框
  87. return
  88. }else {
  89. document.activeElement.blur()//chrome41有效,但是失去焦点的span仍能编辑
  90. $(e.target).parent().prev().attr({"contentEditable":false})
  91. this.setState({
  92. tmpDom:e.target
  93. })
  94. handleShow && handleShow({ikey,id:patId||id});
  95. }
  96. },300);
  97. this.setState({
  98. timer,
  99. });
  100. }
  101. componentDidMount(){ //默认值选中
  102. const {data,ikey,handleSelect,hideTag,mainSaveText,value,boxMark,readDefault,confDefault,isGeneray} = this.props;
  103. const showDefault = readDefault===-1||readDefault===undefined?confDefault:readDefault;
  104. const configed = (+showDefault===1&&isGeneray)||!isGeneray; //是一般情况且设置默认值显示或者不是一般情况
  105. const selected = data.find((it)=>{
  106. return it.selected === undefined&&+it.defaultSelect===1&&configed;
  107. });
  108. if(boxMark!=1&&!hideTag&&selected){
  109. // const text = selected.labelPrefix+selected.name+selected.labelSuffix;
  110. const text = selected.name;
  111. handleSelect&&handleSelect({ikey,id:selected.id,text,mainSaveText,value});
  112. }
  113. }
  114. handleEditLabel(e){
  115. e.stopPropagation();
  116. if(!this.state.editable){ //ie8点开下拉未选值存值bug修改
  117. return;
  118. }
  119. const {ikey,boxMark,handleLabelEdit} = this.props;
  120. this.setState({
  121. editable:false
  122. });
  123. // 更改标签的value值
  124. const ev = e || window.event;
  125. let changeVal = ev.target.innerText || ev.target.innerHTML;
  126. //fireFox括号删完有<br>2270
  127. changeVal=changeVal=="<br>"?'':changeVal;
  128. if(!this.isIE){
  129. ev.target.innerText?(ev.target.innerText = ''):(ev.target.innerHTML = '');
  130. }
  131. handleLabelEdit && handleLabelEdit({ikey,changeVal,type:boxMark});
  132. }
  133. handledbClick(e){
  134. window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
  135. const {value,id,handleDbclick,patId} = this.props;
  136. clearTimeout(this.state.timer);//取消延时的单击事件
  137. //e.preventDefault();
  138. if(value&&value.trim()) {//有选中值的标签才能双击编辑
  139. this.setState({
  140. editable: true
  141. });
  142. };
  143. //失焦关闭编辑状态
  144. setTimeout(()=>{
  145. e.target.focus();
  146. })
  147. handleDbclick&&handleDbclick({id:patId||id});
  148. }
  149. handleMouseDown(){
  150. const {i,setSelectArea,boxMark}= this.props;
  151. setSelectArea({i,boxMark,dir:'start'});
  152. }
  153. render(){
  154. const {data,placeholder,show,value,hideTag,boxMark,select_start,i} = this.props;
  155. const {tmpDom} = this.state
  156. if(!show&&tmpDom){
  157. $(tmpDom).parent().prev().attr({"contentEditable":true})
  158. }
  159. return <div className={`${style['container']} prefixUnsetColor`} ref = {this.$cont}>
  160. <div className={this.getClass()}
  161. onBlur={this.handleEditLabel}
  162. contentEditable={this.state.editable}
  163. onDoubleClick={hideTag?null:this.handledbClick}
  164. onFocus={(e)=>{e.stopPropagation()}}
  165. onClick={(e)=>this.handleShow(e,true)}
  166. onMouseUp={()=>handleMouseUp({select_start,i,boxMark})}
  167. onMouseDown={this.handleMouseDown.bind(this)}
  168. onKeyDown={handleEnter}>
  169. {value||placeholder}
  170. </div>
  171. <DropList onSelect={this.handleSelect} boxMark={boxMark} data={data} show={show} hideTag={hideTag}/>
  172. </div>
  173. }
  174. }
  175. export default RadioDrop;