index.jsx 3.9 KB

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