index.jsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. handleLabelChange && handleLabelChange({ikey,changeVal,type});
  122. }
  123. this.setState({
  124. editable:false
  125. });
  126. }
  127. setStateInit(){
  128. const {nones,noneOn,noneIds,withOn,exists,nowOn,withs,exclusion,excluName} = deepClone(this.props.selecteds||[]);
  129. this.setState({
  130. nones:nones||'',
  131. exists:exists||[],
  132. excluName:excluName||'',
  133. withs:withs||[],
  134. noneIds:noneIds||[],
  135. noneOn:noneOn||false,
  136. withOn:withOn||false,
  137. nowOn:nowOn||'',
  138. exclusion:exclusion||'',
  139. ban:{},
  140. });
  141. }
  142. clearState(){
  143. this.setState({
  144. nones:'',
  145. exists:[],
  146. //existsName:{},
  147. //withsName:{},
  148. withs:[],
  149. noneIds:[],
  150. noneOn:false,
  151. withOn:false,
  152. nowOn:'',
  153. exclusion:'',
  154. excluName:'',
  155. ban:{}
  156. });
  157. this.btnClickFlag = false;
  158. }
  159. handleClear(e){
  160. e.stopPropagation();
  161. this.clearState();
  162. }
  163. handleConfirm(e){
  164. e.stopPropagation();
  165. const {handleConfirm,ikey,type,tagType,order,mainSaveText,copyType} = this.props;
  166. const params = Object.assign({},this.state,{ikey,type,tagType,order,mainSaveText,copyType});
  167. handleConfirm&&handleConfirm(params);
  168. this.btnClickFlag = true;
  169. //点确定后隐藏弹窗
  170. this.props.handleHide();
  171. }
  172. handleSelect(item,isExclu,joint,listIndex,selected){
  173. let {withOn,withs,noneOn,exclusion,exists,nowOn,nones,noneIds,ban} = this.state;
  174. /*if(this.props.selecteds)
  175. console.log(exists,this.props.selecteds.exists,exists===this.props.selecteds.exists)*/
  176. const id = item.id;
  177. const linkStr = joint||'';
  178. const name = item.name+linkStr;
  179. if(isExclu){ //操作“互斥项”
  180. if([...noneIds,...exists,...withs].length>0){ //已选非互斥项,互斥项不可操作
  181. return;
  182. }
  183. let temp = '';
  184. if(exclusion==''){
  185. temp = id;
  186. }else if(exclusion!=''&&exclusion!==id){
  187. temp = exclusion;
  188. }
  189. this.setState({
  190. exclusion:temp,
  191. excluName:temp ===''?'':name
  192. });
  193. return;
  194. }
  195. //操作单选项
  196. if(selected){
  197. const tIndex= exists.findIndex((it)=>it.id===selected.id);
  198. if(tIndex!=-1){
  199. exists.splice(tIndex,1,{id,name,listIndex});
  200. this.setState({
  201. exists,
  202. })
  203. }
  204. return;
  205. }
  206. if(exclusion!==''){ //互斥项被选中,其他不可操作
  207. return;
  208. }
  209. if(+item.code===1){ //操作“伴”类型
  210. this.setState({
  211. withOn:!withOn,
  212. // withs:withOn?[]:[...withs,id], //取消“伴”选中,伴随症状全部取消选中
  213. // withs:withOn?[]:[...withs,{id:item.id,name:name}],
  214. withs:withOn?[]:withs,
  215. ban:withOn?{}:{id:id,name:name},
  216. //withsName:withOn?"":withsName+name, //取消“伴”选中,伴随症状全部取消选中
  217. nowOn:withOn?(noneOn?'none':''):'with'
  218. });
  219. return;
  220. }
  221. if(+item.code===2){ //操作“无”类型
  222. this.setState({
  223. noneOn:!noneOn,
  224. noneIds:noneOn?[]:[...noneIds,id],
  225. nones:noneOn?'':name,
  226. nowOn:noneOn?(withOn?'with':''):'none'
  227. });
  228. return;
  229. }
  230. //操作普通项
  231. let existsIds = exists.length>0? getIds(exists):[];
  232. let withsIds = withs.length>0? getIds(withs):[];
  233. if(existsIds.includes(id)){
  234. let existsData = exists;
  235. exists.forEach((it,i)=>{
  236. if(it.id==id){
  237. existsData.splice(i,1);
  238. }
  239. })
  240. exists = existsData;
  241. }else if(noneIds.includes(id)){
  242. nones = nones.replace(name+'、','');
  243. noneIds.splice(noneIds.indexOf(id),1);
  244. }else if(withsIds.includes(id)){
  245. let withsData = withs;
  246. withs.forEach((it,i)=>{
  247. if(it.id==id){
  248. withsData.splice(i,1);
  249. }
  250. })
  251. withs = withsData;
  252. }else{ //选中普通项
  253. if(nowOn=='none'){
  254. nones += name+'、';
  255. noneIds.push(id);
  256. }else if(nowOn=='with'){
  257. withs.push({id:id,name:name});
  258. }else{
  259. exists.push({id:id,name:name,listIndex});
  260. }
  261. }
  262. this.setState({
  263. nones,
  264. noneIds,
  265. exists,
  266. withs,
  267. ban
  268. //existsName,
  269. //withsName,
  270. });
  271. }
  272. getClass(){
  273. if(this.props.value){
  274. return style['selected-tag'];
  275. }
  276. return style['tag'];
  277. }
  278. getLabels(){
  279. const {data} = this.props;
  280. let detail = [];
  281. let isSpecialPos = false; //是否特殊位置(单行在上面,如无殊)
  282. let isExclu = false; //是否与其他互斥
  283. let isRadio; //是否为单选列,默认多选列
  284. const list = data&&data.map((it,i)=>{
  285. isSpecialPos = (+it.formPosition === 1);
  286. isExclu = (+it.exclusionType===1);
  287. isRadio = (+it.tagType ===1&&+it.controlType === 1);
  288. if(+it.controlType===0){
  289. detail = it.questionMapping;
  290. }else{
  291. detail = it.questionDetailList;
  292. }
  293. return <ListItem data={detail}
  294. isRadio={isRadio}
  295. joint={it.joint}
  296. listIndex={i}
  297. isSpecialPos={isSpecialPos}
  298. isExclu={isExclu}
  299. handleClick={this.handleSelect}
  300. {...this.state}></ListItem>;
  301. });
  302. return list;
  303. }
  304. componentDidMount(){
  305. if(isIE()){
  306. $(this.$div.current).onIe8Input(function(e){
  307. this.onChange(e)
  308. },this);
  309. }
  310. }
  311. render(){
  312. const {placeholder,value,show} = this.props;
  313. const {editable,boxLeft,boxTop} = this.state;
  314. return <div className={style['container']}
  315. onFocus={(e)=>e.stopPropagation()}
  316. onBlur={(e)=>e.stopPropagation()}
  317. onInput={(e)=>e.stopPropagation()}>
  318. <div
  319. ref={this.$div}
  320. onClick={this.handleShow}
  321. className={this.getClass()}
  322. contentEditable={editable}
  323. onDoubleClick={this.changeToEdit}
  324. onBlur={this.handleBlur}
  325. onInput={this.onChange}
  326. onkeydown={handleEnter}
  327. >{value||placeholder}</div>
  328. <div className={style["drop-list"]} style={{display:show?'block':'none',top:boxTop,left:boxLeft}} contentEditable="false" onClick={(e)=>{e.stopPropagation();}}>
  329. {this.getLabels()}
  330. <div className="oper">
  331. <span className={style['clear']} onClick={this.handleClear}>清空选项</span>
  332. <span className={style['confirm']} onClick={this.handleConfirm}>确定</span>
  333. </div>
  334. </div>
  335. </div>
  336. }
  337. }
  338. class ListItem extends Component{
  339. constructor(props){
  340. super(props);
  341. }
  342. handleClick(e,item,i){
  343. e.stopPropagation();
  344. // window.event? window.event.cancelBubble = true : e.stopPropagation();
  345. const {handleClick,isExclu,isRadio,data,exists,noneIds,withs,joint,listIndex} = this.props;
  346. const index=listIndex+''+i;
  347. //列单选处理
  348. if(isRadio){
  349. const selected = data.find((it)=>{
  350. return exists.findIndex((i)=>i.id===it.id)!==-1||noneIds.includes(it.id)||withs.findIndex((i)=>i.id===it.id)!==-1;
  351. });
  352. if(selected&&selected.id!=item.id){ //该列已有选中项,传回已选中的id,name取消选中
  353. handleClick&&handleClick(item,isExclu,joint,index,{id:selected.id,name:selected.name});
  354. }else{
  355. handleClick&&handleClick(item,isExclu,joint,index);
  356. }
  357. return;
  358. }
  359. handleClick&&handleClick(item,isExclu,joint,index);
  360. }
  361. getClass(id){ //无之后显示黑色,之前显示蓝色
  362. const {exclusion,nones,noneIds,exists,withs,isExclu,ban} = this.props;
  363. if(exclusion!=''){
  364. if(+id===+exclusion){
  365. return style['selected'];
  366. }else{
  367. return style['exclusion'];
  368. }
  369. }else{
  370. if(isExclu&&[...noneIds,...exists,...withs].length>0){
  371. return style['exclusion'];
  372. }
  373. if(noneIds.includes(id)){
  374. return style['none-selected'];
  375. }
  376. let existsIds = getIds(exists);
  377. let withsIds = getIds(withs);
  378. // if(existsIds.includes(id)||withsIds.includes(id)){
  379. if(existsIds.includes(id)||withsIds.includes(id)||ban.id && ban.id==id){
  380. return style['selected'];
  381. }
  382. return '';
  383. }
  384. }
  385. render(){
  386. const {data,isSpecialPos} = this.props;
  387. const pos = isSpecialPos?style['independent']:'';
  388. return <ul className={classNames(style['row'],pos)} onBlur={(e)=>e.stopPropagation()}>
  389. {data&&data.map((it,i)=>{
  390. return <li onClick={(e)=>this.handleClick(e,it,i)}
  391. className={this.getClass(it.id)}>{it.labelPrefix}{it.name}{it.labelSuffix}</li>
  392. })}
  393. </ul>;
  394. }
  395. }
  396. export default SpreadDrop;