index.jsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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 {deepClone,filterArr,handleEnter,isIE,getPageCoordinate,windowEventHandler,filterDataArr} from '@utils/tools.js';
  6. import {Notify} from '@commonComp';
  7. import ReactDom from "react-dom";
  8. import {getIds} from '@common/js/func.js';
  9. import $ from 'jquery';
  10. /****
  11. * 标签组合下拉,选中的项目展开
  12. * author:zn@2018-11-21
  13. * 接收参数:
  14. * keepSelf:展开标签后是否保留原标签
  15. * data:下拉内容
  16. * placeholder:灰显文字
  17. *
  18. * ***/
  19. class SpreadDrop extends Component{
  20. constructor(props){
  21. super(props);
  22. const {nones,noneOn,noneIds,withOn,exists,nowOn,withs,exclusion,excluName} = deepClone(props.selecteds||[]);
  23. this.state = {
  24. nones:nones||'', //无,字符串拼接
  25. exists:exists||[], //主症状id
  26. excluName:excluName||'',
  27. withs:withs||[], //伴随 id
  28. noneIds:noneIds||[],
  29. noneOn:noneOn||false, //无是否选中
  30. withOn:withOn||false, //伴是否选中
  31. nowOn:nowOn||'', //最近选中“无”还是“伴”
  32. exclusion:exclusion||'', //选中互斥项id
  33. timer:null, //延时,区分单击双击
  34. ban:{}, //放'伴'字段
  35. editable:false, //双击编辑
  36. labelVal:'', //存放标签原有的值--主诉字数限制用
  37. boxLeft:0,
  38. boxTop:0,
  39. tmpScroll:0,
  40. tmpTop:0,
  41. };
  42. this.$div = React.createRef();
  43. this.handleSelect = this.handleSelect.bind(this);
  44. this.clearState = this.clearState.bind(this);
  45. this.handleClear = this.handleClear.bind(this);
  46. this.handleShow = this.handleShow.bind(this);
  47. this.handleConfirm = this.handleConfirm.bind(this);
  48. this.changeToEdit = this.changeToEdit.bind(this);
  49. this.handleBlur = this.handleBlur.bind(this);
  50. this.onChange = this.onChange.bind(this);
  51. /*是否点击确定按钮标记,处理点击其他等同于确定操作bug,
  52. 如不区分确定按钮提交和点击其他提交,确定按钮提交后会多更新一次状态导致数据重复*/
  53. this.btnClickFlag = false;
  54. }
  55. handleShow(e){//单击
  56. e&&e.stopPropagation();
  57. // let boxLeft = e.pageX -175 + 'px';
  58. // let offsetTop = e.target.offsetTop;
  59. // const ht = e.target.offsetHeight; //如杂音选中文字有多行时,写死会遮挡
  60. // let boxTop = offsetTop + ht +2 + 'px';
  61. // this.setState({
  62. // boxLeft:boxLeft,
  63. // boxTop:boxTop
  64. // })
  65. this.setState({
  66. boxLeft:getPageCoordinate(e).boxLeft,
  67. boxTop:getPageCoordinate(e).boxTop,
  68. tmpScroll: $("#addScrollEvent")[0].scrollTop,
  69. tmpTop:getPageCoordinate(e).boxTop
  70. });
  71. windowEventHandler('scroll',()=>{ //弹窗跟随滚动条滚动或者关闭弹窗
  72. let scrollYs = $("#addScrollEvent")[0].scrollTop;
  73. this.setState({
  74. boxTop:this.state.tmpTop - scrollYs + this.state.tmpScroll
  75. })
  76. },$("#addScrollEvent")[0])
  77. // window.event? window.event.cancelBubble = true : e.stopPropagation();
  78. this.setStateInit(); //恢复初始选中状态
  79. const {ikey,handleShow,placeholder,flag,id,value,tagType,type} = this.props;
  80. const that = this;
  81. this.btnClickFlag = false;
  82. clearTimeout(this.state.timer);
  83. this.state.timer = setTimeout(function(){
  84. if (that.state.editable) {//如果处于编辑状态点击不显示下拉框
  85. return
  86. }else{
  87. handleShow&&handleShow({ikey,placeholder,flag,id,value,tagType,type});
  88. }
  89. },300)
  90. }
  91. changeToEdit(e){//双击
  92. const {value,id,placeholder,handleDbclick,handleHide} = this.props;
  93. let text = e.target.innerText || e.target.innerHTML;
  94. handleHide&&handleHide(); //展开情况下双击收起
  95. // clearTimeout(this.state.timer);//取消延时的单击事件
  96. e.stopPropagation();
  97. // e.preventDefault();
  98. if(value&&value.trim()){//有选中值的标签才能双击编辑
  99. this.setState({
  100. labelVal:text,
  101. editable:true
  102. });
  103. //失焦关闭编辑状态
  104. setTimeout(()=>{
  105. e.target.focus();
  106. })
  107. handleDbclick && handleDbclick({value,id,placeholder});
  108. }
  109. }
  110. onChange(e){
  111. const {mainSaveText,ikey,type,handleLabelChange} = this.props;
  112. const {labelVal,editable} = this.state;
  113. let mainText = filterDataArr(mainSaveText);//主诉字数
  114. if(editable){//避免IE中点击标签也会触发
  115. let val = e.target.innerText || e.target.innerHTML;
  116. if(+type==1){// 主诉字数达到上限时不允许输入
  117. if(mainText.length >= config.limited){
  118. if(val.length > labelVal.length){
  119. e.target.innerText?(e.target.innerText = labelVal):(e.target.innerHTML = labelVal);
  120. Notify.info(config.limitText);
  121. return
  122. }else if(val.length == labelVal.length){
  123. this.setState({
  124. labelVal:val
  125. });
  126. }else{
  127. handleLabelChange && handleLabelChange({ikey,changeVal:val,type});
  128. }
  129. }
  130. }
  131. }
  132. }
  133. handleBlur(e){
  134. e.stopPropagation();
  135. const {ikey,type,handleLabelChange} = this.props;
  136. const {editable} = this.state;
  137. const ev = e || window.event;
  138. if(editable){
  139. // 更改标签的value值
  140. let changeVal = ev.target.innerText || ev.target.innerHTML;
  141. if(!isIE()){
  142. e.target.innerText?(e.target.innerText = ''):(e.target.innerHTML = ''); //避免出现重复输入值
  143. }
  144. handleLabelChange && handleLabelChange({ikey,changeVal,type});
  145. }
  146. this.setState({
  147. editable:false
  148. });
  149. }
  150. setStateInit(){
  151. const {nones,noneOn,noneIds,withOn,exists,nowOn,withs,exclusion,excluName} = deepClone(this.props.selecteds||[]);
  152. this.setState({
  153. nones:nones||'',
  154. exists:exists||[],
  155. excluName:excluName||'',
  156. withs:withs||[],
  157. noneIds:noneIds||[],
  158. noneOn:noneOn||false,
  159. withOn:withOn||false,
  160. nowOn:nowOn||'',
  161. exclusion:exclusion||'',
  162. ban:{},
  163. });
  164. }
  165. clearState(){
  166. this.setState({
  167. nones:'',
  168. exists:[],
  169. //existsName:{},
  170. //withsName:{},
  171. withs:[],
  172. noneIds:[],
  173. noneOn:false,
  174. withOn:false,
  175. nowOn:'',
  176. exclusion:'',
  177. excluName:'',
  178. ban:{}
  179. });
  180. this.btnClickFlag = false;
  181. }
  182. handleClear(e){
  183. e.stopPropagation();
  184. this.clearState();
  185. }
  186. handleConfirm(e){
  187. e.stopPropagation();
  188. const {handleConfirm,ikey,type,tagType,order,mainSaveText,copyType,value,mainData} = this.props;
  189. const params = Object.assign({},this.state,{ikey,type,tagType,order,mainSaveText,copyType,value,mainData});
  190. handleConfirm&&handleConfirm(params);
  191. this.btnClickFlag = true;
  192. //点确定后隐藏弹窗
  193. this.props.handleHide();
  194. }
  195. handleSelect(item,isExclu,joint,listIndex,selected){//console.log(item,selected)
  196. let {withOn,withs,noneOn,exclusion,exists,nowOn,nones,noneIds,ban} = this.state;
  197. /*if(this.props.selecteds)
  198. console.log(exists,this.props.selecteds.exists,exists===this.props.selecteds.exists)*/
  199. const id = item.id;
  200. const linkStr = joint||'';
  201. const name = item.name+linkStr;
  202. if(isExclu){ //操作“互斥项”
  203. if([...noneIds,...exists,...withs].length>0){ //已选非互斥项,互斥项不可操作
  204. return;
  205. }
  206. let temp = '';
  207. if(exclusion==''){
  208. temp = id;
  209. }else if(exclusion!=''&&exclusion!==id){
  210. temp = exclusion;
  211. }
  212. this.setState({
  213. exclusion:temp,
  214. excluName:temp ===''?'':name
  215. });
  216. return;
  217. }
  218. //操作单选项
  219. if(!noneOn&&selected){
  220. const tIndex= exists.findIndex((it)=>it.questionId===item.questionId);
  221. const bIndex= withs.findIndex((it)=>it.questionId===item.questionId);
  222. if(tIndex!=-1){
  223. exists.splice(tIndex,1,Object.assign({},item,{name})); //修改单选列连接字符不显示bug
  224. this.setState({
  225. exists,
  226. })
  227. }
  228. if(bIndex!=-1){
  229. withs.splice(tIndex,1,item);
  230. this.setState({
  231. withs,
  232. })
  233. }
  234. return;
  235. }
  236. if(exclusion!==''){ //互斥项被选中,其他不可操作
  237. return;
  238. }
  239. if(+item.code===1){ //操作“伴”类型
  240. this.setState({
  241. withOn:!withOn,
  242. // withs:withOn?[]:[...withs,id], //取消“伴”选中,伴随症状全部取消选中
  243. // withs:withOn?[]:[...withs,{id:item.id,name:name}],
  244. withs:withOn?[]:withs,
  245. ban:withOn?{}:{id:id,name:name},
  246. //withsName:withOn?"":withsName+name, //取消“伴”选中,伴随症状全部取消选中
  247. nowOn:withOn?(noneOn?'none':''):'with'
  248. });
  249. return;
  250. }
  251. if(+item.code===2){ //操作“无”类型
  252. this.setState({
  253. noneOn:!noneOn,
  254. noneIds:noneOn?[]:[...noneIds,id],
  255. nones:noneOn?'':name,
  256. nowOn:noneOn?(withOn?'with':''):'none'
  257. });
  258. return;
  259. }
  260. //操作普通项
  261. let existsIds = exists.length>0? getIds(exists):[];
  262. let withsIds = withs.length>0? getIds(withs):[];
  263. if(existsIds.includes(id)){
  264. let existsData = exists;
  265. exists.forEach((it,i)=>{
  266. if(it.id==id){
  267. existsData.splice(i,1);
  268. }
  269. })
  270. exists = existsData;
  271. }else if(noneIds.includes(id)){
  272. nones = nones.replace(name+'、','');
  273. noneIds.splice(noneIds.indexOf(id),1);
  274. }else if(withsIds.includes(id)){
  275. let withsData = withs;
  276. withs.forEach((it,i)=>{
  277. if(it.id==id){
  278. withsData.splice(i,1);
  279. }
  280. })
  281. withs = withsData;
  282. }else{ //选中普通项
  283. if(nowOn=='none'){
  284. nones += name+'、';
  285. noneIds.push(id);
  286. }else if(nowOn=='with'){
  287. withs.push({id:id,name:name,questionId:item.questionId});
  288. }else{
  289. exists.push({id:id,name:name,listIndex,questionId:item.questionId});
  290. }
  291. }
  292. this.setState({
  293. nones,
  294. noneIds,
  295. exists,
  296. withs,
  297. ban
  298. //existsName,
  299. //withsName,
  300. });
  301. }
  302. getClass(){
  303. const blueBorder = this.state.editable?style['blue-border']:'';
  304. if(this.props.show){
  305. $(this.$div.current).addClass(style['borderd']);
  306. }else{
  307. $(this.$div.current).removeClass(style['borderd']);
  308. }
  309. if(this.props.value){
  310. return classNames(style['selected-tag'],blueBorder);
  311. }
  312. return style['tag'];
  313. }
  314. componentDidMount(){
  315. if(isIE()){
  316. $(this.$div.current).onIe8Input(function(e){
  317. this.onChange(e)
  318. },this);
  319. }
  320. }
  321. render(){
  322. const {placeholder,value,show,data} = this.props;
  323. const {editable,boxLeft,boxTop} = this.state;
  324. return <div className={style['container']}
  325. onFocus={(e)=>e.stopPropagation()}
  326. onBlur={(e)=>e.stopPropagation()}
  327. onInput={(e)=>e.stopPropagation()}>
  328. <div
  329. ref={this.$div}
  330. onClick={this.handleShow}
  331. className={this.getClass()}
  332. contentEditable={editable}
  333. onDoubleClick={this.changeToEdit}
  334. onBlur={this.handleBlur}
  335. onInput={this.onChange}
  336. onkeydown={handleEnter}
  337. >{value||placeholder}</div>
  338. <ListItems data={data} left={boxLeft}
  339. top={boxTop} show={show} handleSelect={this.handleSelect} handleConfirm={this.handleConfirm} handleClear={this.handleClear} {...this.state}></ListItems>
  340. </div>
  341. }
  342. }
  343. class ListItems extends Component{
  344. constructor(props){
  345. super(props);
  346. }
  347. getLabels(){
  348. const {data,handleSelect} = this.props;
  349. let detail = [];
  350. let isSpecialPos = false; //是否特殊位置(单行在上面,如无殊)
  351. let isExclu = false; //是否与其他互斥
  352. let isRadio; //是否为单选列,默认多选列
  353. const list = data&&data.map((it,i)=>{
  354. isSpecialPos = (+it.formPosition === 1);
  355. isExclu = (+it.exclusionType===1);
  356. isRadio = (+it.tagType ===1&&+it.controlType === 1);
  357. if(+it.controlType===0){
  358. detail = it.questionMapping;
  359. }else{
  360. detail = it.questionDetailList;
  361. }
  362. return <ListItem datas={detail}
  363. isRadio={isRadio}
  364. joint={it.joint}
  365. listIndex={i}
  366. isSpecialPos={isSpecialPos}
  367. isExclu={isExclu}
  368. handleClick={handleSelect}
  369. {...this.props}></ListItem>;
  370. });
  371. return list;
  372. }
  373. getStyle(){
  374. const {left,top,show} = this.props;
  375. return {
  376. left:left?left+'px':'',
  377. top:top?top+'px':'',
  378. display:show?'block':'none'
  379. }
  380. }
  381. render (){
  382. const {handleClear,handleConfirm} = this.props;
  383. const domNode = document.getElementById('root');
  384. return ReactDom.createPortal(
  385. <div className={style["drop-list"]} style={this.getStyle()} contentEditable="false" onClick={(e)=>{e.stopPropagation();}}>
  386. {this.getLabels()}
  387. <div className="oper">
  388. <span className={style['clear']} onClick={handleClear}>清空选项</span>
  389. <span className={style['confirm']} onClick={handleConfirm}>确定</span>
  390. </div>
  391. </div>
  392. ,domNode)
  393. }
  394. }
  395. class ListItem extends Component{
  396. constructor(props){
  397. super(props);
  398. }
  399. handleClick(e,item,i){
  400. e.stopPropagation();
  401. // window.event? window.event.cancelBubble = true : e.stopPropagation();
  402. const {handleClick,isExclu,isRadio,data,exists,noneIds,withs,joint,listIndex} = this.props;
  403. const index=listIndex+''+i;
  404. //列单选处理
  405. if(isRadio){
  406. let selected = exists.find((i)=>{
  407. return i.questionId===item.questionId;
  408. })||withs.find((i)=>{
  409. return i.questionId===item.questionId;
  410. })||noneIds.find((i)=>{
  411. return i.id===item.questionId;
  412. });
  413. /*const selected = data.find((it)=>{console.log(exists)
  414. return exists.findIndex((i)=>i.questionId===it.id)!==-1||noneIds.includes(it.id)||withs.findIndex((i)=>i.questionId===it.id)!==-1;
  415. });*/
  416. if(selected&&selected.id!=item.id){ //该列已有选中项,传回已选中的id,name取消选中
  417. handleClick&&handleClick(item,isExclu,joint,index,{id:selected.id,name:selected.name,questionId:selected.questionId});
  418. }else{
  419. handleClick&&handleClick(item,isExclu,joint,index);
  420. }
  421. return;
  422. }
  423. handleClick&&handleClick(item,isExclu,joint,index);
  424. }
  425. getClass(id){ //无之后显示黑色,之前显示蓝色
  426. const {exclusion,nones,noneIds,exists,withs,isExclu,ban} = this.props;
  427. if(exclusion!=''){
  428. if(+id===+exclusion){
  429. return style['selected'];
  430. }else{
  431. return style['exclusion'];
  432. }
  433. }else{
  434. if(isExclu&&([...noneIds,...exists,...withs].length>0||ban.id)){
  435. return style['exclusion'];
  436. }
  437. if(noneIds.includes(id)){
  438. return style['none-selected'];
  439. }
  440. let existsIds = getIds(exists);
  441. let withsIds = getIds(withs);
  442. // if(existsIds.includes(id)||withsIds.includes(id)){
  443. if(existsIds.includes(id)||withsIds.includes(id)||ban.id && ban.id==id){
  444. return style['selected'];
  445. }
  446. return '';
  447. }
  448. }
  449. render(){
  450. const {datas,isSpecialPos} = this.props;
  451. const pos = isSpecialPos?style['independent']:'';
  452. return <ul className={classNames(style['row'],pos)} onBlur={(e)=>e.stopPropagation()}>
  453. {datas&&datas.map((it,i)=>{
  454. /*return <li onClick={(e)=>this.handleClick(e,it,i)}
  455. className={this.getClass(it.id)}>{it.labelPrefix}{it.name}{it.labelSuffix}</li>*/
  456. return <li onClick={(e)=>this.handleClick(e,it,i)} className={this.getClass(it.id)} title={it.name.length>8?it.name:''}>{it.name&&it.name.length>8?it.name.slice(0,8)+'...':it.name}</li>
  457. })}
  458. </ul>;
  459. }
  460. }
  461. export default SpreadDrop;