index.jsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import React,{Component} from 'react';
  2. import {handleEnter} from '@utils/tools.js';
  3. import {DropList} from '@commonComp';
  4. import style from "./index.less";
  5. /****
  6. * 单选下拉
  7. * author:zn@2018-11.13
  8. * 接收参数:
  9. * placeholder:灰显文字
  10. * data:下拉内容数据
  11. * hideTag:是否隐藏括号
  12. * handleSelect:选中事件
  13. * prefix:前缀
  14. * suffix:后缀
  15. *
  16. * ***/
  17. class RadioDrop extends Component{
  18. constructor(props){
  19. super(props);
  20. this.state={
  21. editable:false,
  22. timer:null
  23. };
  24. this.handleSelect = this.handleSelect.bind(this);
  25. this.handleShow = this.handleShow.bind(this);
  26. this.handledbClick = this.handledbClick.bind(this);
  27. this.handleEditLabel = this.handleEditLabel.bind(this);
  28. }
  29. getClass(){
  30. const {value,hideTag,placeholder} = this.props;
  31. if(hideTag){
  32. return style['no-tag'];
  33. }
  34. if(value&&value!=placeholder){
  35. return style['selected-tag'];
  36. }
  37. return style['tag'];
  38. }
  39. handleSelect(item){
  40. const {handleSelect,ikey,mainSaveText} = this.props;
  41. if(!item){ //清空
  42. handleSelect&&handleSelect({ikey});
  43. return ;
  44. }
  45. let text = '';
  46. switch(true){
  47. case +item.controlType ===6:
  48. case +item.controlType===7:
  49. case +item.tagType===3:
  50. text = item;
  51. break;
  52. case 3:
  53. default:
  54. // text = item.labelPrefix+item.name+item.labelSuffix;
  55. text = item.name;
  56. // text = item.questionDetailList&&item.questionDetailList.length>0?item.questionDetailList[0].name: item.name;
  57. }
  58. handleSelect&&handleSelect({ikey,id:item.id,text,mainSaveText});
  59. }
  60. handleShow(e){
  61. //e.stopPropagation();
  62. const {handleShow,ikey,id,patId} = this.props;
  63. const that = this;
  64. const timer = setTimeout(function(){
  65. if (that.state.editable) {//如果处于编辑状态点击不显示下拉框
  66. return
  67. }else {
  68. handleShow && handleShow({ikey,id:patId||id});
  69. }
  70. },300);
  71. this.setState({
  72. timer
  73. });
  74. }
  75. componentDidMount(){ //默认值选中
  76. const {data,ikey,handleSelect} = this.props;
  77. const selected = data.find((it)=>{
  78. return it.selected === undefined&&+it.defaultSelect===1;
  79. });
  80. if(selected){
  81. // const text = selected.labelPrefix+selected.name+selected.labelSuffix;
  82. const text = selected.name;
  83. handleSelect&&handleSelect({ikey,id:selected.id,text});
  84. }
  85. }
  86. handleEditLabel(e){
  87. e.stopPropagation();
  88. const {ikey,boxMark,handleLabelEdit} = this.props;
  89. /*const {editable} = this.state;
  90. if(editable){
  91. // 更改标签的value值
  92. let changeVal = e.target.innerText;
  93. handleLabelEdit && handleLabelEdit({ikey,changeVal,type:boxMark});
  94. }*/
  95. this.setState({
  96. editable:false
  97. });
  98. // 更改标签的value值
  99. let changeVal = e.target.innerText;
  100. handleLabelEdit && handleLabelEdit({ikey,changeVal,type:boxMark});
  101. }
  102. handledbClick(e){
  103. const {value,id,handleDbclick,patId} = this.props;
  104. clearTimeout(this.state.timer);//取消延时的单击事件
  105. //e.preventDefault();
  106. if(value&&value.trim()) {//有选中值的标签才能双击编辑
  107. this.setState({
  108. editable: true
  109. });
  110. };
  111. handleDbclick&&handleDbclick({id:patId||id});
  112. }
  113. render(){
  114. const {data,prefix,suffix,placeholder,show,value} = this.props;
  115. return <div className={style['container']}>
  116. {prefix}
  117. <div className={this.getClass()}
  118. onBlur={this.handleEditLabel}
  119. contentEditable={this.state.editable}
  120. onDoubleClick={this.handledbClick}
  121. onClick={(e)=>this.handleShow(e,true)}
  122. onkeydown={handleEnter}>
  123. {value||placeholder}
  124. </div>
  125. {suffix}
  126. <DropList onSelect={this.handleSelect} data={data}
  127. show={show}/>
  128. </div>
  129. }
  130. }
  131. export default RadioDrop;