Skip to content

Commit 30cb182

Browse files
vaadin-botbwajtr
andauthored
fix: clear dragover row and cell parts on dragover change (#5624) (#5643)
Co-authored-by: Břetislav Wajtr <bretislav@vaadin.com>
1 parent b0cf651 commit 30cb182

4 files changed

Lines changed: 109 additions & 14 deletions

File tree

‎packages/grid/src/vaadin-grid-drag-and-drop-mixin.js‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Copyright (c) 2016 - 2023 Vaadin Ltd.
44
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
55
*/
6-
import { iterateChildren, updateRowStates } from './vaadin-grid-helpers.js';
6+
import { iterateChildren, updateBooleanRowStates, updateStringRowStates } from './vaadin-grid-helpers.js';
77

88
const DropMode = {
99
BETWEEN: 'between',
@@ -156,12 +156,12 @@ export const DragAndDropMixin = (superClass) =>
156156
// Set the default transfer data
157157
e.dataTransfer.setData('text', this.__formatDefaultTransferData(rows));
158158

159-
updateRowStates(row, { dragstart: rows.length > 1 ? `${rows.length}` : '' });
159+
updateBooleanRowStates(row, { dragstart: rows.length > 1 ? `${rows.length}` : '' });
160160
this.style.setProperty('--_grid-drag-start-x', `${e.clientX - rowRect.left + 20}px`);
161161
this.style.setProperty('--_grid-drag-start-y', `${e.clientY - rowRect.top + 10}px`);
162162

163163
requestAnimationFrame(() => {
164-
updateRowStates(row, { dragstart: null });
164+
updateBooleanRowStates(row, { dragstart: false });
165165
this.style.setProperty('--_grid-drag-start-x', '');
166166
this.style.setProperty('--_grid-drag-start-y', '');
167167
});
@@ -255,7 +255,7 @@ export const DragAndDropMixin = (superClass) =>
255255
} else if (row) {
256256
this._dragOverItem = row._item;
257257
if (row.getAttribute('dragover') !== this._dropLocation) {
258-
updateRowStates(row, { dragover: this._dropLocation }, true);
258+
updateStringRowStates(row, { dragover: this._dropLocation });
259259
}
260260
} else {
261261
this._clearDragStyles();
@@ -310,7 +310,7 @@ export const DragAndDropMixin = (superClass) =>
310310
_clearDragStyles() {
311311
this.removeAttribute('dragover');
312312
iterateChildren(this.$.items, (row) => {
313-
updateRowStates(row, { dragover: null }, true);
313+
updateStringRowStates(row, { dragover: null });
314314
});
315315
}
316316

@@ -398,7 +398,7 @@ export const DragAndDropMixin = (superClass) =>
398398
}
399399
});
400400

401-
updateRowStates(row, {
401+
updateBooleanRowStates(row, {
402402
'drag-disabled': !!dragDisabled,
403403
'drop-disabled': !!dropDisabled,
404404
});

‎packages/grid/src/vaadin-grid-helpers.js‎

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,15 @@ export function updateCellsPart(cells, part, value) {
8686
/**
8787
* @param {!HTMLElement} row
8888
* @param {Object} states
89-
* @param {boolean} appendValue
9089
*/
91-
export function updateRowStates(row, states, appendValue) {
90+
export function updateBooleanRowStates(row, states) {
9291
const cells = getBodyRowCells(row);
9392

9493
Object.entries(states).forEach(([state, value]) => {
9594
// Row state attribute
9695
updateState(row, state, value);
9796

98-
const rowPart = appendValue ? `${state}-${value}-row` : `${state}-row`;
97+
const rowPart = `${state}-row`;
9998

10099
// Row part attribute
101100
updatePart(row, value, rowPart);
@@ -105,6 +104,35 @@ export function updateRowStates(row, states, appendValue) {
105104
});
106105
}
107106

107+
/**
108+
* @param {!HTMLElement} row
109+
* @param {Object} states
110+
*/
111+
export function updateStringRowStates(row, states) {
112+
const cells = getBodyRowCells(row);
113+
114+
Object.entries(states).forEach(([state, value]) => {
115+
const prevValue = row.getAttribute(state);
116+
117+
// Row state attribute
118+
updateState(row, state, value);
119+
120+
// remove previous part from row and cells if there was any
121+
if (prevValue) {
122+
const prevRowPart = `${state}-${prevValue}-row`;
123+
updatePart(row, false, prevRowPart);
124+
updateCellsPart(cells, `${prevRowPart}-cell`, false);
125+
}
126+
127+
// set new part to rows and cells if there is a value
128+
if (value) {
129+
const rowPart = `${state}-${value}-row`;
130+
updatePart(row, value, rowPart);
131+
updateCellsPart(cells, `${rowPart}-cell`, value);
132+
}
133+
});
134+
}
135+
108136
/**
109137
* @param {!HTMLElement} cell
110138
* @param {string} attribute

‎packages/grid/src/vaadin-grid.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import { DragAndDropMixin } from './vaadin-grid-drag-and-drop-mixin.js';
2727
import { DynamicColumnsMixin } from './vaadin-grid-dynamic-columns-mixin.js';
2828
import { EventContextMixin } from './vaadin-grid-event-context-mixin.js';
2929
import { FilterMixin } from './vaadin-grid-filter-mixin.js';
30-
import { getBodyRowCells, iterateChildren, updateCellsPart, updateRowStates } from './vaadin-grid-helpers.js';
30+
import { getBodyRowCells, iterateChildren, updateBooleanRowStates, updateCellsPart } from './vaadin-grid-helpers.js';
3131
import { KeyboardNavigationMixin } from './vaadin-grid-keyboard-navigation-mixin.js';
3232
import { RowDetailsMixin } from './vaadin-grid-row-details-mixin.js';
3333
import { ScrollMixin } from './vaadin-grid-scroll-mixin.js';
@@ -975,7 +975,7 @@ class Grid extends ElementMixin(
975975

976976
/** @private */
977977
_updateRowOrderParts(row, index = row.index) {
978-
updateRowStates(row, {
978+
updateBooleanRowStates(row, {
979979
first: index === 0,
980980
last: index === this._effectiveSize - 1,
981981
odd: index % 2 !== 0,
@@ -985,7 +985,7 @@ class Grid extends ElementMixin(
985985

986986
/** @private */
987987
_updateRowStateParts(row, { expanded, selected, detailsOpened }) {
988-
updateRowStates(row, {
988+
updateBooleanRowStates(row, {
989989
expanded,
990990
selected,
991991
'details-opened': detailsOpened,

‎packages/grid/test/drag-and-drop.test.js‎

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,27 @@ describe('drag and drop', () => {
579579
fireDragOver(row, 'under');
580580
expect(row.getAttribute('dragover')).to.equal('below');
581581
});
582+
583+
it('should clear previous dragover row part attribute on dragover change', () => {
584+
grid.dropMode = 'between';
585+
const row = grid.$.items.children[0];
586+
fireDragOver(row, 'above');
587+
fireDragOver(row, 'below');
588+
expect(row.getAttribute('part')).to.not.contain('dragover-above-row');
589+
expect(row.getAttribute('part')).to.contain('dragover-below-row');
590+
});
591+
592+
it('should clear previous dragover cells part attribute on dragover change', () => {
593+
grid.dropMode = 'between';
594+
const row = grid.$.items.children[0];
595+
fireDragOver(row, 'above');
596+
fireDragOver(row, 'below');
597+
const cells = getRowBodyCells(row);
598+
cells.forEach((cell) => {
599+
expect(cell.getAttribute('part')).to.not.contain('dragover-above-row-cell');
600+
expect(cell.getAttribute('part')).to.contain('dragover-below-row-cell');
601+
});
602+
});
582603
});
583604

584605
describe('dragleave', () => {
@@ -589,22 +610,45 @@ describe('drag and drop', () => {
589610
expect(spy.called).to.be.false;
590611
});
591612

592-
it('should clear the grid drag styles', () => {
613+
it('should clear the grid dragover attribute', () => {
593614
grid.dropMode = 'on-grid';
594615
fireDragOver();
595616
expect(grid.hasAttribute('dragover')).to.be.true;
596617
fireDragLeave();
597618
expect(grid.hasAttribute('dragover')).to.be.false;
598619
});
599620

600-
it('should clear the row drag styles', () => {
621+
it('should clear the row dragover attribute', () => {
601622
grid.dropMode = 'on-top';
602623
fireDragOver();
603624
const row = grid.$.items.children[0];
604625
expect(row.hasAttribute('dragover')).to.be.true;
605626
fireDragLeave();
606627
expect(row.hasAttribute('dragover')).to.be.false;
607628
});
629+
630+
it('should clear the row dragover parts', () => {
631+
grid.dropMode = 'on-top';
632+
fireDragOver();
633+
const row = grid.$.items.children[0];
634+
expect(row.getAttribute('part')).to.contain('dragover-');
635+
fireDragLeave();
636+
expect(row.getAttribute('part')).to.not.contain('dragover-');
637+
});
638+
639+
it('should clear the cells dragover parts', () => {
640+
grid.dropMode = 'on-top';
641+
fireDragOver();
642+
const row = grid.$.items.children[0];
643+
const cells = getRowBodyCells(row);
644+
cells.forEach((cell) => {
645+
expect(cell.getAttribute('part')).to.contain('dragover-');
646+
});
647+
fireDragLeave();
648+
cells.forEach((cell) => {
649+
expect(cell.getAttribute('part')).to.not.contain('dragover-');
650+
});
651+
});
608652
});
609653

610654
describe('dragenter', () => {
@@ -665,6 +709,29 @@ describe('drag and drop', () => {
665709
expect(row.hasAttribute('dragover')).to.be.false;
666710
});
667711

712+
it('should clear the row dragover parts', () => {
713+
grid.dropMode = 'on-top';
714+
fireDragOver();
715+
const row = grid.$.items.children[0];
716+
expect(row.getAttribute('part')).to.contain('dragover-');
717+
fireDrop();
718+
expect(row.getAttribute('part')).to.not.contain('dragover-');
719+
});
720+
721+
it('should clear the cells dragover parts', () => {
722+
grid.dropMode = 'on-top';
723+
fireDragOver();
724+
const row = grid.$.items.children[0];
725+
const cells = getRowBodyCells(row);
726+
cells.forEach((cell) => {
727+
expect(cell.getAttribute('part')).to.contain('dragover-');
728+
});
729+
fireDrop();
730+
cells.forEach((cell) => {
731+
expect(cell.getAttribute('part')).to.not.contain('dragover-');
732+
});
733+
});
734+
668735
it('should dispatch a grid specific event', () => {
669736
fireDrop();
670737
expect(dropSpy.calledOnce).to.be.true;

0 commit comments

Comments
 (0)