index.jsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. import React,{Component} from 'react';
  2. import classNames from 'classnames';
  3. import config from '@config/index.js';
  4. import style from './index.less';
  5. import {setPosition,deepClone,handleEnter,isIE,windowEventHandler,filterDataArr,getIds,getPageCoordinate} from '@utils/tools.js';
  6. import {Notify} from '@commonComp';
  7. import ListItems from '@components/ListItems';
  8. import $ from 'jquery';
  9. /****
  10. * 标签组合下拉,选中的项目展开
  11. * author:zn@2018-11-21
  12. * 接收参数:
  13. * data:下拉内容
  14. * value:选中成文
  15. * placeholder:灰显文字
  16. * ikey:当前标签的index
  17. * copyType:选中后是否复制本身(仅展开类型使用)
  18. * data.selecteds:选中选项(仅展开类型使用)
  19. * show:是否显示下拉
  20. * order:成文顺序,0点选顺序,1从上到下从左到右
  21. * isImports:是否高亮显示(仅查体中使用)
  22. * type:所在模块:现病史、查体等
  23. * tagType:标签类型
  24. * id:id
  25. * flag:仅主诉中使用
  26. * isExtBlue:是否为查体生命体征标签蓝色显示
  27. *
  28. * ***/
  29. class SpreadDrop extends Component{
  30. constructor(props){
  31. super(props);
  32. const {nones,noneOn,noneIds,withOn,exists,nowOn,withs,exclusion,excluName} = deepClone(props.selecteds||[]);
  33. this.state = {
  34. nones:nones||'', //无,字符串拼接
  35. exists:exists||[], //主症状id
  36. excluName:excluName||'',
  37. withs:withs||[], //伴随 id
  38. noneIds:noneIds||[],
  39. noneOn:noneOn||false, //无是否选中
  40. withOn:withOn||false, //伴是否选中
  41. nowOn:nowOn||'', //最近选中“无”还是“伴”
  42. exclusion:exclusion||'', //选中互斥项id
  43. timer:null, //延时,区分单击双击
  44. ban:{}, //放'伴'字段
  45. editable:false, //双击编辑
  46. labelVal:'', //存放标签原有的值--主诉字数限制用
  47. left:'auto',
  48. tmpDom:null
  49. };
  50. this.$div = React.createRef();
  51. this.$list = React.createRef();
  52. this.handleSelect = this.handleSelect.bind(this);
  53. this.clearState = this.clearState.bind(this);
  54. this.handleClear = this.handleClear.bind(this);
  55. this.handleShow = this.handleShow.bind(this);
  56. this.handleConfirm = this.handleConfirm.bind(this);
  57. this.changeToEdit = this.changeToEdit.bind(this);
  58. this.handleBlur = this.handleBlur.bind(this);
  59. this.onChange = this.onChange.bind(this);
  60. /*是否点击确定按钮标记,处理点击其他等同于确定操作bug,
  61. 如不区分确定按钮提交和点击其他提交,确定按钮提交后会多更新一次状态导致数据重复*/
  62. this.btnClickFlag = false;
  63. }
  64. handleShow(e){//单击
  65. e&&e.stopPropagation();
  66. const {ikey,handleShow,placeholder,flag,id,value,tagType,type,data,windowWidth,setHighter} = this.props;
  67. let num = 0;//判断为五类切超出页面
  68. data && data.map((item)=>{
  69. if(item.formPosition != 1){
  70. ++num
  71. }
  72. });
  73. const listWidth = 30+$(this.$list.current).width();
  74. if(num >= 4 && windowWidth-getPageCoordinate(e).boxLeft < listWidth){
  75. this.setState({
  76. left:windowWidth-listWidth-150
  77. })
  78. }
  79. //高度超出时,增加左侧大容器padding
  80. setPosition(e,this.$list.current,setHighter);
  81. // window.event? window.event.cancelBubble = true : e.stopPropagation();
  82. this.setStateInit(); //恢复初始选中状态
  83. const that = this;
  84. this.btnClickFlag = false;
  85. clearTimeout(this.state.timer);
  86. this.state.timer = setTimeout(()=>{
  87. if (that.state.editable) {//如果处于编辑状态点击不显示下拉框
  88. return
  89. }else{
  90. document.activeElement.blur()//chrome41有效,但是失去焦点的span仍能编辑
  91. $(e.target).parent().prev().attr({"contentEditable":false})
  92. this.setState({
  93. tmpDom:e.target
  94. })
  95. handleShow&&handleShow({ikey,placeholder,flag,id,value,tagType,type});
  96. }
  97. },300)
  98. }
  99. changeToEdit(e){//双击
  100. window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
  101. const {value,id,placeholder,handleDbclick,handleHide} = this.props;
  102. let text = e.target.innerText || e.target.innerHTML;
  103. handleHide&&handleHide(); //展开情况下双击收起
  104. // clearTimeout(this.state.timer);//取消延时的单击事件
  105. e.stopPropagation();
  106. // e.preventDefault();
  107. if(value&&value.trim()){//有选中值的标签才能双击编辑
  108. this.setState({
  109. labelVal:text,
  110. editable:true
  111. });
  112. //失焦关闭编辑状态
  113. setTimeout(()=>{
  114. e.target.focus();
  115. })
  116. handleDbclick && handleDbclick({value,id,placeholder});
  117. }
  118. }
  119. onChange(e){
  120. const {mainSaveText,ikey,type,handleLabelChange} = this.props;
  121. const {labelVal,editable} = this.state;
  122. let mainText = filterDataArr(mainSaveText);//主诉字数
  123. if(editable){//避免IE中点击标签也会触发
  124. let val = e.target.innerText || e.target.innerHTML;
  125. if(+type==1){// 主诉字数达到上限时不允许输入
  126. if(mainText.length >= config.limited){
  127. if(val.length > labelVal.length){
  128. e.target.innerText?(e.target.innerText = labelVal):(e.target.innerHTML = labelVal);
  129. Notify.info(config.limitText);
  130. return
  131. }else if(val.length == labelVal.length){
  132. this.setState({
  133. labelVal:val
  134. });
  135. }else{
  136. handleLabelChange && handleLabelChange({ikey,changeVal:val,type});
  137. }
  138. }
  139. }
  140. }
  141. }
  142. handleBlur(e){
  143. e.stopPropagation();
  144. const {ikey,type,handleLabelChange} = this.props;
  145. const {editable} = this.state;
  146. const ev = e || window.event;
  147. if(editable){
  148. // 更改标签的value值
  149. let changeVal = ev.target.innerText || ev.target.innerHTML;
  150. if(!isIE()){
  151. e.target.innerText?(e.target.innerText = ''):(e.target.innerHTML = ''); //避免出现重复输入值
  152. }
  153. handleLabelChange && handleLabelChange({ikey,changeVal,type});
  154. }
  155. this.setState({
  156. editable:false
  157. });
  158. }
  159. setStateInit(){
  160. const {nones,noneOn,noneIds,withOn,exists,nowOn,withs,exclusion,excluName,ban} = deepClone(this.props.selecteds||[]);
  161. this.setState({
  162. nones:nones||'',
  163. exists:exists||[],
  164. excluName:excluName||'',
  165. withs:withs||[],
  166. noneIds:noneIds||[],
  167. noneOn:noneOn||false,
  168. withOn:withOn||false,
  169. nowOn:nowOn||'',
  170. exclusion:exclusion||'',
  171. ban:ban||{},
  172. });
  173. }
  174. clearState(){
  175. this.setState({
  176. nones:'',
  177. exists:[],
  178. //existsName:{},
  179. //withsName:{},
  180. withs:[],
  181. noneIds:[],
  182. noneOn:false,
  183. withOn:false,
  184. nowOn:'',
  185. exclusion:'',
  186. excluName:'',
  187. ban:{}
  188. });
  189. this.btnClickFlag = false;
  190. }
  191. handleClear(e){
  192. e.stopPropagation();
  193. this.clearState();
  194. }
  195. handleConfirm(e){
  196. // e.stopPropagation();
  197. const {handleConfirm,ikey,type,tagType,order,mainSaveText,copyType,value,mainData} = this.props;
  198. const params = Object.assign({},this.state,{ikey,type,tagType,order,mainSaveText,copyType,value,mainData});
  199. delete params.tmpDom; //避免上面deepClone selecteds报错
  200. handleConfirm&&handleConfirm(params);
  201. this.btnClickFlag = true;
  202. //点确定后隐藏弹窗
  203. this.props.handleHide();
  204. }
  205. handleSelect(item,isExclu,joint,listIndex,selected){
  206. let {withOn,withs,noneOn,exclusion,exists,nowOn,nones,noneIds,ban} = this.state;
  207. const id = item.id;
  208. const linkStr = joint||'';
  209. const name = item.name+linkStr;
  210. if(isExclu){ //操作“互斥项”
  211. if([...noneIds,...exists,...withs].length>0){ //已选非互斥项,清空已选项,选中该互斥项
  212. this.clearState();
  213. this.setState({
  214. exclusion:id,
  215. excluName:name
  216. });
  217. return;
  218. }
  219. //未选中互斥项,直接选中该互斥项
  220. let temp = '';
  221. if(exclusion==''){
  222. temp = id;
  223. }else if(exclusion!=''&&exclusion!==id){
  224. temp = exclusion;
  225. }
  226. this.setState({
  227. exclusion:temp,
  228. excluName:temp ===''?'':name
  229. });
  230. return;
  231. }
  232. //操作单选项
  233. if(selected){
  234. const tIndex= exists.findIndex((it)=>it.questionId===item.questionId);
  235. const bIndex= withs.findIndex((it)=>it.questionId===item.questionId);
  236. if(tIndex!=-1){
  237. exists.splice(tIndex,1,Object.assign({},item,{name})); //修改单选列连接字符不显示bug
  238. this.setState({
  239. exists,
  240. })
  241. }
  242. if(bIndex!=-1){
  243. withs.splice(tIndex,1,item);
  244. this.setState({
  245. withs,
  246. })
  247. }
  248. return;
  249. }
  250. if(exclusion!==''){ //互斥项已选中,清空互斥项
  251. this.setState({
  252. exclusion:'',
  253. excluName:''
  254. });
  255. }
  256. if(+item.code===1){ //操作“伴”类型
  257. this.setState({
  258. withOn:!withOn,
  259. // withs:withOn?[]:[...withs,id], //取消“伴”选中,伴随症状全部取消选中
  260. // withs:withOn?[]:[...withs,{id:item.id,name:name}],
  261. withs:withOn?[]:withs,
  262. ban:withOn?{}:{id:id,name:name,value:name},
  263. //withsName:withOn?"":withsName+name, //取消“伴”选中,伴随症状全部取消选中
  264. nowOn:withOn?(noneOn?'none':''):'with'
  265. });
  266. return;
  267. }
  268. if(+item.code===2){ //操作“无”类型
  269. this.setState({
  270. noneOn:!noneOn,
  271. noneIds:noneOn?[]:[...noneIds,id],
  272. nones:noneOn?'':name,
  273. nowOn:noneOn?(withOn?'with':''):'none'
  274. });
  275. return;
  276. }
  277. //操作普通项
  278. let existsIds = exists.length>0? getIds(exists):[];
  279. let withsIds = withs.length>0? getIds(withs):[];
  280. if(existsIds.includes(id)){
  281. let existsData = exists;
  282. exists.forEach((it,i)=>{
  283. if(it.id==id){
  284. existsData.splice(i,1);
  285. }
  286. })
  287. exists = existsData;
  288. }else if(noneIds.includes(id)){
  289. nones = nones.replace(name+'、','');
  290. noneIds.splice(noneIds.indexOf(id),1);
  291. }else if(withsIds.includes(id)){
  292. let withsData = withs;
  293. withs.forEach((it,i)=>{
  294. if(it.id==id){
  295. withsData.splice(i,1);
  296. }
  297. })
  298. withs = withsData;
  299. }else{ //选中普通项
  300. if(nowOn=='none'){
  301. nones += name+'、';
  302. noneIds.push(id);
  303. }else if(nowOn=='with'){
  304. // withs.push({id:id,name:name,questionId:item.questionId});
  305. withs.push({id:id,name:name,questionId:item.questionId,conceptId:item.conceptId});
  306. }else{
  307. // exists.push({id:id,name:name,listIndex,questionId:item.questionId});
  308. exists.push({id:id,name:name,listIndex,questionId:item.questionId,conceptId:item.conceptId});
  309. }
  310. }
  311. this.setState({
  312. nones,
  313. noneIds,
  314. exists,
  315. withs,
  316. ban
  317. //existsName,
  318. //withsName,
  319. });
  320. }
  321. getClass(){
  322. const {isImports,show,value,isExtBlue} = this.props;
  323. const blueBorder = this.state.editable?style['blue-border']:'';
  324. const orgBorder = isImports&&!value?style['orange-border']:'';
  325. const ext = isExtBlue?style['ext']:'';
  326. if(show){
  327. $(this.$div.current).addClass(style['borderd']);
  328. }else{
  329. $(this.$div.current).removeClass(style['borderd']);
  330. }
  331. if(value){
  332. return classNames(style['selected-tag'],blueBorder);
  333. }
  334. return classNames(style['tag'],orgBorder,ext);
  335. }
  336. componentDidMount(){
  337. if(isIE()){
  338. $(this.$div.current).onIe8Input(function(e){
  339. this.onChange(e)
  340. },this);
  341. }
  342. }
  343. /*componentWillReceiveProps(nextProps){
  344. const {setHighter} = this.props;
  345. if(!nextProps.show){console.log(22)
  346. setHighter&&setHighter(48);
  347. }
  348. }*/
  349. render(){
  350. const {placeholder,value,show,data,order,type,tagType,ikey,pos} = this.props;
  351. const {tmpDom,left} = this.state
  352. const clickIndx = ikey.split('-')[1];//展开下拉的index
  353. if(!show&&tmpDom){
  354. $(tmpDom).parent().prev().attr({"contentEditable":true})
  355. }
  356. const {editable} = this.state;
  357. return <div className={style['container']}
  358. /*onFocus={(e)=>e.stopPropagation()}*/
  359. onBlur={(e)=>e.stopPropagation()}
  360. onInput={(e)=>e.stopPropagation()}>
  361. <div
  362. ref={this.$div}
  363. onClick={this.handleShow}
  364. className={this.getClass()}
  365. contentEditable={editable}
  366. onDoubleClick={this.changeToEdit}
  367. onBlur={this.handleBlur}
  368. onInput={this.onChange}
  369. onkeydown={handleEnter}
  370. >{value||placeholder}</div>
  371. <ListItems parDiv={this.$list} pos={pos} data={data} order={order} left={left} boxMark={type} tagType={tagType}
  372. show={show} handleSelect={this.handleSelect} handleConfirm={this.handleConfirm} handleClear={this.handleClear} {...this.state}></ListItems>
  373. </div>
  374. }
  375. }
  376. export default SpreadDrop;