Skip to content

Commit c059494

Browse files
author
patricktr
committed
bug fix for hidden columns throwing off reorder calculations
1 parent 8b962dd commit c059494

File tree

4 files changed

+49
-7
lines changed

4 files changed

+49
-7
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# 1.2.3 (2020-4-10)
2+
3+
Bug fix that prevented `onDraggedColumnChange` from being called if columns dragged back to their original order
4+
Bug fix for column re-ordering not working if any of the React Table columns have `show: false` defined
5+
16
# 1.2.2 (2020-1-17)
27

38
Bug fix that prevented sorting on a draggable column

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ render () {
5858
}
5959
```
6060

61+
## Special Considerations
62+
63+
If any of your React Table columns are hidden (show: false), this HOC will automatically move those columns to the end of the list. This is to prevent hidden columns from causing this HOC from improperly calculating the new column order.
64+
65+
ReactTable Columns Props [https://github.com/tannerlinsley/react-table/tree/v6.10.0#columns]
66+
6167
## draggableColumns Prop
6268

6369
| Property | Description | Default value | Type | Required |

src/dom-helper.js

+6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ const index = element => {
2121
return -1
2222
}
2323

24+
const getAttribute = (element, attributeName) => {
25+
console.log('getattr', element)
26+
return element.getAttribute(attributeName)
27+
}
28+
2429
const addClass = (element, className) => {
2530
if (element.classList) element.classList.add(className)
2631
else element.className += ' ' + className
@@ -104,6 +109,7 @@ export default {
104109
parseStrDimensionToInt,
105110
getOffset,
106111
index,
112+
getAttribute,
107113
addClass,
108114
removeClass,
109115
hasClass,

src/index.js

+32-7
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,26 @@ export default Component => {
5252

5353
// helper methods
5454
findParentHeader(element) {
55-
if (element.className.includes('rt-th')) {
55+
// need to check typeof to avoid errors when traversing special elements like SVGs
56+
if (element.className && typeof element.className === 'string' && element.className.includes('rt-th')) {
5657
return element
5758
} else {
5859
let parent = element.parentElement
59-
while (!parent.className.includes('rt-th')) {
60+
while (typeof parent.className !== 'string' || !parent.className.includes('rt-th')) {
6061
parent = parent.parentElement
6162
if (!parent) break
6263
}
6364
return parent
6465
}
6566
}
67+
68+
getFirstChildWithColumnIndexAttributeValue(element) {
69+
const header = DomHelper.findFirstChildWithClassName(element, 'draggable-header')
70+
71+
if (header) {
72+
return DomHelper.getAttribute(header, 'data-column-index')
73+
}
74+
}
6675
// end helper methods
6776

6877
createDragEvents() {
@@ -288,6 +297,10 @@ export default Component => {
288297
// only move if the dragged column is meets position threshold
289298
let dragIndex = DomHelper.index(this.draggedColumn)
290299
let dropIndex = DomHelper.index(this.findParentHeader(e.target))
300+
301+
// for the future
302+
// this.getFirstChildWithColumnIndexAttributeValue(this.draggedColumn)
303+
291304
let allowDrop = dragIndex !== dropIndex
292305

293306
if (
@@ -312,6 +325,8 @@ export default Component => {
312325
}
313326
}
314327

328+
console.log('ddropIndex', dropIndex, 'this.dragged', this.dragged)
329+
315330
this.reorder.push({ a: dropIndex, b: this.dragged })
316331

317332
if (onDropSuccess) {
@@ -408,7 +423,7 @@ export default Component => {
408423
};
409424

410425
render() {
411-
const { columns, draggableColumns, ...rest } = this.props
426+
const { columns: origColumns, draggableColumns, ...rest } = this.props
412427
const {
413428
draggable = defaultProps.draggable,
414429
mode = defaultProps.mode,
@@ -437,7 +452,15 @@ export default Component => {
437452
/>
438453
)
439454

440-
const cols = columns.map(col => {
455+
// separate out visible and hidden columns
456+
const visibleColumns = origColumns.filter(col => col.show === true || col.show === undefined)
457+
const hiddenColumns = origColumns.filter(col => col.show === false)
458+
459+
// place hidden columns at very end of array
460+
// having a hidden column inbetween two draggable columns will cause this HOC to improperly calculate the new column index positions
461+
const adjustedOrigColumns = [...visibleColumns, ...hiddenColumns]
462+
463+
const cols = adjustedOrigColumns.map((col, index) => {
441464
let headerClassName = `${this.uniqueId} draggable-header`
442465

443466
// add additional className if column is draggable enabled
@@ -452,17 +475,19 @@ export default Component => {
452475
...col,
453476
Header:
454477
typeof col.Header === 'function' ? (
455-
<div className={headerClassName}>
478+
<div className={headerClassName} data-column-index={index}>
456479
{col.Header()}
457480
</div>
458481
) : (
459-
<div className={headerClassName}>
482+
<div className={headerClassName} data-column-index={index}>
460483
{col.Header}
461484
</div>
462485
)
463486
}
464487
})
465488

489+
const previousOrder = [...this.currentColumnOrder]
490+
466491
// run all reorder events
467492
if (mode && mode === DragMode.SWAP) {
468493
this.reorder.forEach(o => (cols[o.a] = cols.splice(o.b, 1, cols[o.a])[0]))
@@ -476,7 +501,7 @@ export default Component => {
476501

477502
// fire change event?
478503
if (!this.state.firstLoad) {
479-
const originalOrder = columns.map(col => {
504+
const originalOrder = previousOrder.map(col => {
480505
if (typeof col.accessor === 'function') return col.id
481506
return col.accessor
482507
})

0 commit comments

Comments
 (0)