import React, { Component } from 'react'; import { PageComponent,} from '../../types/index'; import classname from 'classnames' import * as Tools from 'jad-tool' const resetMap = function(weekMap,len?){ const length = len? len: 48 return weekMap.map( e =>{ return new Array(length).fill(false) }) } const makeUpNumber = function(num, length = 2){ num = Math.floor( num * (24/48)) num = num === 24? 0: num; return (num / Math.pow(10, length)).toFixed(length).substr(2); } type timeMapType = boolean[][] export class MultiSelectTable extends Component timeMapType, onChange?:(selectMap)=>void disabled?: boolean }>> { week = ['星期一','星期二','星期三','星期四','星期五','星期六','星期天']; dayHour= [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23] timeMap = resetMap(this.week) state = { drafting: false, selectFlag: false, afterXY: {x:false,y:false}, checkBuff: resetMap(this.week), activeBuff : resetMap(this.week), hasMove: false, } componentDidMount(){ const { value=null } = this.props if(value){ this.initValue() } } UNSAFE_componentWillReceiveProps(nextProps){ if(nextProps.value && !this.props.value){ setTimeout(()=>this.initValue()) } } initValue(){ const { value=null,onAdapter=(e)=>e } = this.props const timeMap = resetMap(this.week) const checkBuff = onAdapter(timeMap,value) if(Tools.isEmptyArray(checkBuff) || checkBuff.length !== 7 || Tools.isEmptyArray(checkBuff[0]) || checkBuff[0].length !== 48 ){ return false; } this.setState({ checkBuff }) } ifCheck(){ const { activeBuff,checkBuff,selectFlag } = this.state return checkBuff.map( (item,x) => item.map( (e,y) => { if( e && activeBuff[x][y]){ return selectFlag; } if( !e && activeBuff[x][y]){ return selectFlag; } return e })) } onHandleCallBack(){ const { onChange } = this.props const { checkBuff} = this.state if(Tools.isExist(onChange) && Tools.isFunction(onChange)){ onChange(checkBuff) } } onHandleClick(x,y){ let { checkBuff, hasMove} = this.state; let { disabled } = this.props if(disabled) return if(!hasMove){ // 非移动时可能触发 checkBuff[x][y] = !checkBuff[x][y]; // ---此处 this.setState({ checkBuff: checkBuff, hasMove: false, },()=>{ this.onHandleCallBack() }) } } onHandleMouseUp(x,y){ let { activeBuff} = this.state let { disabled } = this.props if(disabled) return this.setState({ drafting: false, afterXY: {x:x,y:y}, activeBuff:resetMap(this.week), checkBuff: this.ifCheck() },()=>{ this.onHandleCallBack() }) } onHandleMouseMove(x,y){ let { activeBuff,afterXY,drafting} = this.state; let { disabled } = this.props if(disabled) return if(!drafting){ return false; } activeBuff = activeBuff.map( (item,indexX) => { return item.map( (e,indexY) =>{ //第四象限 if(indexX >= afterXY.x && indexX <= x && indexY>=afterXY.y && indexY<=y){ return true } //第三象限 if(indexX <= x&& indexX >= afterXY.x && indexY>=y && indexY<=afterXY.y){ return true } //第一象限 if(indexX >= x&& indexX <= afterXY.x && indexY>=afterXY.y && indexY<=y){ return true } //第二象限 if(indexX >= x && indexX <= afterXY.x && indexY>=y && indexY<=afterXY.y ){ return true } return false }) }) this.setState({ activeBuff:activeBuff, hasMove: true, }) } onHandleMouseDown(x,y){ let { checkBuff } = this.state let { disabled } = this.props if(disabled) return this.setState({ selectFlag:!checkBuff[x][y], afterXY:{x:x,y:y}, drafting:true, hasMove: false, }) } renderFormat(){ const { checkBuff } = this.state const checkWeek = this.week.filter( (item,index)=>{ const newItem = checkBuff[index].filter( (e,i)=>{ return e }) return newItem.length > 0 }) const renderSpan = (indexX)=>{ return checkBuff[indexX].map( (item,index) =>{ const even = index%2 return item? { !even? `${makeUpNumber(index)}:00~${makeUpNumber(index)}:30` : `${makeUpNumber(index)}:30~${makeUpNumber(index+1)}:00` } :'' }) } if(checkWeek.length === 0){ return null } return (
) } renderTd(item,x){ const { activeBuff,checkBuff } = this.state return item.map( (el,y) => { const select = activeBuff[x][y]? 'active':'' const check = checkBuff[x][y]? 'checked':'' return {this.onHandleClick(x,y)}} onMouseDown={()=>{this.onHandleMouseDown(x,y)}} onMouseUp={()=>{this.onHandleMouseUp(x,y)}} onMouseMove={()=>{this.onHandleMouseMove(x,y)}} className={classname(select,'time',check)} > }) } renderThead(){ return ( 星期/时间 上午 下午 { this.dayHour.map( e =>
{e}
) }
) } renderTbody(){ return this.timeMap.map( (item,index) =>{ return {this.week[index]} {this.renderTd(item,index)} }) } render() { return (
单击方格【可以选中/取消时间点】,单击拖动方格【可以选中/取消一连续时间段】 已选择 未选择
{/* table */} {this.renderThead()} {this.renderTbody()}
{/* 已选择 */} {this.renderFormat()}
) } }