index.jsx 13 KB

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