123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- import React,{Component} from 'react';
- import className from 'classnames';
- import {NumberPan} from '@commonComp';
- import style from './index.less';
- import $ from "jquery";
- import {handleEnter,getPageCoordinate} from '@utils/tools.js';
- /***
- * author:zn@2018-11-19
- * 接收参数:
- * value: 默认选中的值
- * placeholder:灰显文字
- * handleSelect: 选中事件
- * show: 是否显示下拉
- *
- * ***/
- class NumberDrop extends Component{
- constructor(props){
- super(props);
- this.state={
- editable:false, //标签是否可输入
- timer:null,
- hasSelect:false, //是否点过下拉键盘
- boxLeft:0,
- boxTop:0,
- tmpTop:0,
- tmpScroll:0
- };
- this.$span = React.createRef();
- this.$pre = React.createRef();
- this.$suf = React.createRef();
- this.$cont = React.createRef();
- this.select = this.select.bind(this);
- this.numInpBlur = this.numInpBlur.bind(this);
- this.handleSpanInp = this.handleSpanInp.bind(this);
- this.handleNumClick = this.handleNumClick.bind(this);
- this.handleNumFocus = this.handleNumFocus.bind(this);
- this.handleBlur = this.handleBlur.bind(this);
- this.changeToEdit = this.changeToEdit.bind(this);
- }
- select(text){ //选中键盘上数字事件
- const {handleSelect,ikey,suffix,prefix,mainSaveText} = this.props;
- this.setState({
- hasSelect:true
- });
- handleSelect&&handleSelect({ikey,text,suffix,prefix,mainSaveText});
- }
- handleNumFocus(e){ //数字框可编辑状态下聚集事件,处理是否显示下拉等
- e.stopPropagation();
- }
- handleNumClick(e){ //数字框不可编辑的状态时点击事件,点击将数字框变为可输入且下拉不再显示直到失焦后再次聚集
- const {show,handleShow,ikey,id,patId,handleHide,value} = this.props;
- if(show) {
- handleHide && handleHide();
- return;
- }else{
- const {editable} = this.state;
- if(editable){
- return;
- }
- const that = this;
- //双击时不显示下拉
- clearTimeout(that.state.timer);
- const timer = setTimeout(function(){
- handleShow&&handleShow({ikey,id:patId||id});
- },300);
- this.setState({
- timer,
- boxLeft:getPageCoordinate(e).boxLeft,
- boxTop:getPageCoordinate(e).boxTop,
- tmpScroll: $("#addScrollEvent")[0].scrollTop,
- tmpTop:getPageCoordinate(e).boxTop
- });
- $("#addScrollEvent").scroll(()=>{
- let scrollYs = $("#addScrollEvent")[0].scrollTop;
- this.setState({
- boxTop:this.state.tmpTop - scrollYs + this.state.tmpScroll
- })
- })
- }
- e.stopPropagation();
- }
- numInpBlur(e){ //数字框失焦,保存值到store中
- e.stopPropagation();
- this.setState({
- hasSelect:false
- });
- const val = e.target.innerText.replace(/^\s*/,'');
- const {handleSelect,ikey,suffix,prefix,mainSaveText,placeholder} = this.props;
- const text = val===placeholder?'':val;
- e.target.innerText = ''; //避免出现重复输入值
- handleSelect&&handleSelect({ikey,text,suffix,prefix,mainSaveText});
- }
- handleSpanInp(e){ //数字框输入事件
- e.stopPropagation();
- const {handleHide} = this.props;
- handleHide&&handleHide();
- }
- getClasses(){ //整个标签是否有值的状态
- const {value,hideTag,placeholder} = this.props;
- const blueBorder = this.state.editable?style['blue-border']:'';
- const isSelected = value&&value!=placeholder?style['selected']:style['container'];
- const noTag = hideTag?style['no-tag']:'';
- return className(isSelected,noTag,blueBorder);
- }
- changeToEdit(e){ //整个标签双击编辑状态
- const {value,id,handleDbclick,patId,handleHide,show} = this.props;
- clearTimeout(this.state.timer);//取消延时的单击事件
- e.preventDefault();
- if(show){
- handleHide&&handleHide();
- }
- if(value&&value.trim()) {//有选中值的标签才能双击编辑
- this.setState({
- editable: true
- });
- setTimeout(()=>{
- this.$cont.current.focus();
- })
- //双击埋点记录
- handleDbclick && handleDbclick({id:patId||id});
- }
- }
- handleBlur(e){ //双击编辑blur
- const {handleLabelChange,ikey,boxMark,value} = this.props;
- //if(!this.state.editable) return;
- this.setState({
- editable: false
- });
- let totalVal = e.target.innerText;
- let changeVal = this.$span.current.innerText.replace(/^\s*/,'');//数字框值-修改后;去掉前空格避免多空格叠加
- let prefix = this.$pre.current.innerText.replace(/^\s*/,''); //前缀值-修改后
- let suffix = this.$suf.current.innerText.replace(/^\s*/,''); //后缀值-修改后
- //console.log('数字框:'+changeVal,";全部:"+totalVal,";前缀:"+prefix+";后缀:"+suffix);
- handleLabelChange && handleLabelChange({ikey,changeVal,type:boxMark,totalVal,prefix,suffix});
- }
- getSpanClass(){ //将被替换的文字选中状态显示
- //const {hasSelect} = this.state;
- const cls = this.props.show?style['blued']:'';
- return cls;
- }
- render(){
- const {placeholder,prefix,suffix,show,value,handleHide} = this.props;
- const {numEditable,editable,hasSelect,boxTop,boxLeft} = this.state;
- return <div className={this.getClasses()}
- ref={this.$cont}
- onDoubleClick={this.changeToEdit}
- contentEditable={editable}
- onBlur={this.handleBlur}
- onkeydown={handleEnter}>
- <span ref = {this.$pre}> {prefix}</span>
- <span onFocus={this.handleNumFocus}
- onClick={this.handleNumClick}
- contentEditable={true}
- style={{minWidth:'10px',display:'inline-block'}}
- ref = {this.$span}
- onBlur={this.numInpBlur}
- onInput={this.handleSpanInp}
- className={this.getSpanClass()}
- onkeydown={handleEnter}> {value||placeholder}</span>
- <span ref = {this.$suf}> {suffix}</span>
- <NumberPan handleSelect={(text)=>this.select(text)}
- onClose={handleHide}
- show={show}
- toClear={!hasSelect}
- left={boxLeft}
- top={boxTop}/>
- </div>
- }
- }
- export default NumberDrop;
|