diff --git a/src/Table.js b/src/Table.js index a702a67..dc650c2 100644 --- a/src/Table.js +++ b/src/Table.js @@ -148,6 +148,7 @@ class Table extends Component{ expandedRowKeys: nextProps.expandedRowKeys, }); } + debugger; if (nextProps.columns && nextProps.columns !== this.props.columns) { this.columnManager.reset(nextProps.columns); } else if (nextProps.children !== this.props.children) { @@ -220,9 +221,8 @@ class Table extends Component{ } getHeader(columns, fixed) { - const { showHeader, expandIconAsCell, clsPrefix ,onDragStart,onDragOver,onDrop,draggable} = this.props; + const { showHeader, expandIconAsCell, clsPrefix ,onDragStart,onDragEnter,onDragOver,onDrop,draggable} = this.props; const rows = this.getHeaderRows(columns); - if (expandIconAsCell && fixed !== 'right') { rows[0].unshift({ key: 'u-table-expandIconAsCell', @@ -233,7 +233,7 @@ class Table extends Component{ } const trStyle = fixed ? this.getHeaderRowStyle(columns, rows) : null; - let drop = draggable?{onDragStart,onDragOver,onDrop,draggable}:{}; + let drop = draggable?{onDragStart,onDragOver,onDrop,onDragEnter,draggable}:{}; return showHeader ? ( { + event.dataTransfer.effectAllowed = "move"; + event.dataTransfer.setData("Text",data.key); + this.currentObj = data; + event.dataTransfer.setDragImage(event.target, 0, 0); + this.props.onDragStart(event,data); + } + + onDragOver=(event,data)=>{ + if(this.currentObj.key == data.key)return; + event.preventDefault(); + this.props.onDragOver(event,data); + } + + onDragEnter=(event,data)=>{ + if(this.currentObj.key == data.key)return; + this.props.onDragEnter(event,data); + } + + onDrop=(event,data)=>{ + if(this.currentObj.key == data.key)return; + this.props.onDrop(event,data); + } + render() { const { clsPrefix, rowStyle ,onDragStart,onDragOver,onDrop,draggable,rows} = this.props; return ( @@ -26,9 +51,17 @@ class TableHeader extends Component{ rows.map((row, index) => ( {row.map((da, i) => { - return draggable?({this.props.onDragStart(event,da)}} onDragOver={this.props.onDragOver} onDrop={(event)=>{this.props.onDrop(event,da)}} draggable={draggable} - {...da} key={i} />):(); + let thHover = da.drgHover?` ${clsPrefix}-thead th-drag-hover`:""; + return draggable?( + {this.onDragStart(event,da)}} + onDragOver={(event)=>{this.onDragOver(event,da)}} + onDrop={(event)=>{this.onDrop(event,da)}} + onDragEnter={(event)=>{this.onDragEnter(event,da)}} + draggable={draggable} + className={`${da.className} ${clsPrefix}-thead th-drag ${thHover}`} + key={i} + >{da.children}):(); })} )) diff --git a/src/lib/dragColumn.js b/src/lib/dragColumn.js index 23d4efb..d23ca8c 100644 --- a/src/lib/dragColumn.js +++ b/src/lib/dragColumn.js @@ -16,7 +16,8 @@ export default function dragColumn(Table) { constructor(props) { super(props); - const {columns} = props; + const {columns} = props; + const { clsPrefix} = props; this.setColumOrderByIndex(columns); } @@ -31,6 +32,7 @@ export default function dragColumn(Table) { Object.assign(_column,columns); _column.forEach((da,i) => { da.dragIndex = i; + da.drgHover = false; }); this.state = { columns:_column @@ -38,12 +40,23 @@ export default function dragColumn(Table) { } - onDragStart=(event,data)=>{ - event.dataTransfer.setData("Text",data.key); + onDragStart=(event,data)=>{ } - onDragOver=(event)=>{ - event.preventDefault(); + onDragOver=(event,data)=>{ + + } + + onDragEnter=(event,data)=>{ + const {columns:_columns} = this.state; + let columns = []; + Object.assign(columns,_columns); + columns.forEach((da)=>da.drgHover = false) + let current = columns.find((da)=>da.key == data.key); + current.drgHover = true; + this.setState({ + columns + }) } onDrop=(event,data)=>{ @@ -53,26 +66,27 @@ export default function dragColumn(Table) { let targetIndex = columns.findIndex((_da,i)=>_da.key == data.key); columns.forEach((da,i)=>{ + da.drgHover = false; if(da.key == id){//obj da.dragIndex = targetIndex } if(da.key == data.key){//targetObj - da.dragIndex = objIndex + da.dragIndex = objIndex; } }); let _columns = sortBy(columns,(da)=>da.dragIndex); - console.log("_columns--",_columns); this.setState({ - columns:_columns - }) + columns:_columns, + }); } render() { const {data} = this.props; const {columns} = this.state; - return () } };