Skip to content

Commit 5f63238

Browse files
authored
feat: expose row state attributes as part names (#4988)
1 parent 9504fd9 commit 5f63238

10 files changed

Lines changed: 903 additions & 50 deletions

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,12 @@ export const DragAndDropMixin = (superClass) =>
154154
// Set the default transfer data
155155
e.dataTransfer.setData('text', this.__formatDefaultTransferData(rows));
156156

157-
row.setAttribute('dragstart', rows.length > 1 ? rows.length : '');
157+
this._updateRowState(row, 'dragstart', rows.length > 1 ? `${rows.length}` : '');
158158
this.style.setProperty('--_grid-drag-start-x', `${e.clientX - rowRect.left + 20}px`);
159159
this.style.setProperty('--_grid-drag-start-y', `${e.clientY - rowRect.top + 10}px`);
160160

161161
requestAnimationFrame(() => {
162-
row.removeAttribute('dragstart');
162+
this._updateRowState(row, 'dragstart', null);
163163
this.updateStyles({ '--_grid-drag-start-x': '', '--_grid-drag-start-y': '' });
164164
});
165165

@@ -252,7 +252,7 @@ export const DragAndDropMixin = (superClass) =>
252252
} else if (row) {
253253
this._dragOverItem = row._item;
254254
if (row.getAttribute('dragover') !== this._dropLocation) {
255-
row.setAttribute('dragover', this._dropLocation);
255+
this._updateRowState(row, 'dragover', this._dropLocation, true);
256256
}
257257
} else {
258258
this._clearDragStyles();
@@ -306,7 +306,9 @@ export const DragAndDropMixin = (superClass) =>
306306
/** @protected */
307307
_clearDragStyles() {
308308
this.removeAttribute('dragover');
309-
Array.from(this.$.items.children).forEach((row) => row.removeAttribute('dragover'));
309+
Array.from(this.$.items.children).forEach((row) => {
310+
this._updateRowState(row, 'dragover', null, true);
311+
});
310312
}
311313

312314
/** @private */
@@ -395,8 +397,8 @@ export const DragAndDropMixin = (superClass) =>
395397
}
396398
});
397399

398-
row.toggleAttribute('drag-disabled', !!dragDisabled);
399-
row.toggleAttribute('drop-disabled', !!dropDisabled);
400+
this._updateRowState(row, 'drag-disabled', !!dragDisabled);
401+
this._updateRowState(row, 'drop-disabled', !!dropDisabled);
400402
}
401403

402404
/**

‎packages/grid/src/vaadin-grid.d.ts‎

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -299,17 +299,29 @@ export interface GridEventMap<TItem> extends HTMLElementEventMap, GridCustomEven
299299
*
300300
* The following shadow DOM parts are available for styling:
301301
*
302-
* Part name | Description
303-
* ----------------|----------------
304-
* `row` | Row in the internal table
305-
* `cell` | Cell in the internal table
306-
* `header-cell` | Header cell in the internal table
307-
* `body-cell` | Body cell in the internal table
308-
* `footer-cell` | Footer cell in the internal table
309-
* `details-cell` | Row details cell in the internal table
310-
* `focused-cell` | Focused cell in the internal table
311-
* `resize-handle` | Handle for resizing the columns
312-
* `reorder-ghost` | Ghost element of the header cell being dragged
302+
* Part name | Description
303+
* -----------------------|----------------
304+
* `row` | Row in the internal table
305+
* `expanded-row` | Expanded row
306+
* `selected-row` | Selected row
307+
* `details-opened-row` | Row with details open
308+
* `odd-row` | Odd row
309+
* `first-row` | The first body row
310+
* `last-row` | The last body row
311+
* `dragstart-row` | Set on the row for one frame when drag is starting. The value is a number when multiple rows are dragged
312+
* `dragover-above-row` | Set on the row when the a row is dragged over above
313+
* `dragover-below-row` | Set on the row when the a row is dragged over below
314+
* `dragover-on-top-row` | Set on the row when the a row is dragged over on top
315+
* `drag-disabled-row` | Set to a row that isn't available for dragging
316+
* `drop-disabled-row` | Set to a row that can't be dropped on top of
317+
* `cell` | Cell in the internal table
318+
* `header-cell` | Header cell in the internal table
319+
* `body-cell` | Body cell in the internal table
320+
* `footer-cell` | Footer cell in the internal table
321+
* `details-cell` | Row details cell in the internal table
322+
* `focused-cell` | Focused cell in the internal table
323+
* `resize-handle` | Handle for resizing the columns
324+
* `reorder-ghost` | Ghost element of the header cell being dragged
313325
*
314326
* The following state attributes are available for styling:
315327
*

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

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { beforeNextRender } from '@polymer/polymer/lib/utils/render-status.js';
99
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
1010
import { isAndroid, isChrome, isFirefox, isIOS, isSafari, isTouch } from '@vaadin/component-base/src/browser-utils.js';
1111
import { ControllerMixin } from '@vaadin/component-base/src/controller-mixin.js';
12+
import { addValueToAttribute, removeValueFromAttribute } from '@vaadin/component-base/src/dom-utils.js';
1213
import { ElementMixin } from '@vaadin/component-base/src/element-mixin.js';
1314
import { TabindexMixin } from '@vaadin/component-base/src/tabindex-mixin.js';
1415
import { processTemplates } from '@vaadin/component-base/src/templates.js';
@@ -172,17 +173,29 @@ import { StylingMixin } from './vaadin-grid-styling-mixin.js';
172173
*
173174
* The following shadow DOM parts are available for styling:
174175
*
175-
* Part name | Description
176-
* ----------------|----------------
177-
* `row` | Row in the internal table
178-
* `cell` | Cell in the internal table
179-
* `header-cell` | Header cell in the internal table
180-
* `body-cell` | Body cell in the internal table
181-
* `footer-cell` | Footer cell in the internal table
182-
* `details-cell` | Row details cell in the internal table
183-
* `focused-cell` | Focused cell in the internal table
184-
* `resize-handle` | Handle for resizing the columns
185-
* `reorder-ghost` | Ghost element of the header cell being dragged
176+
* Part name | Description
177+
* -----------------------|----------------
178+
* `row` | Row in the internal table
179+
* `expanded-row` | Expanded row
180+
* `selected-row` | Selected row
181+
* `details-opened-row` | Row with details open
182+
* `odd-row` | Odd row
183+
* `first-row` | The first body row
184+
* `last-row` | The last body row
185+
* `dragstart-row` | Set on the row for one frame when drag is starting. The value is a number when multiple rows are dragged
186+
* `dragover-above-row` | Set on the row when the a row is dragged over above
187+
* `dragover-below-row` | Set on the row when the a row is dragged over below
188+
* `dragover-on-top-row` | Set on the row when the a row is dragged over on top
189+
* `drag-disabled-row` | Set to a row that isn't available for dragging
190+
* `drop-disabled-row` | Set to a row that can't be dropped on top of
191+
* `cell` | Cell in the internal table
192+
* `header-cell` | Header cell in the internal table
193+
* `body-cell` | Body cell in the internal table
194+
* `footer-cell` | Footer cell in the internal table
195+
* `details-cell` | Row details cell in the internal table
196+
* `focused-cell` | Focused cell in the internal table
197+
* `resize-handle` | Handle for resizing the columns
198+
* `reorder-ghost` | Ghost element of the header cell being dragged
186199
*
187200
* The following state attributes are available for styling:
188201
*
@@ -888,9 +901,9 @@ class Grid extends ElementMixin(
888901
return;
889902
}
890903

891-
row.toggleAttribute('first', index === 0);
892-
row.toggleAttribute('last', index === this._effectiveSize - 1);
893-
row.toggleAttribute('odd', index % 2);
904+
this._updateRowState(row, 'first', index === 0);
905+
this._updateRowState(row, 'last', index === this._effectiveSize - 1);
906+
this._updateRowState(row, 'odd', index % 2);
894907
this._a11yUpdateRowRowindex(row, index);
895908
this._getItem(index, row);
896909
}
@@ -975,9 +988,9 @@ class Grid extends ElementMixin(
975988
this._a11yUpdateRowLevel(row, model.level);
976989
this._a11yUpdateRowSelected(row, model.selected);
977990

978-
row.toggleAttribute('expanded', model.expanded);
979-
row.toggleAttribute('selected', model.selected);
980-
row.toggleAttribute('details-opened', model.detailsOpened);
991+
this._updateRowState(row, 'expanded', model.expanded);
992+
this._updateRowState(row, 'selected', model.selected);
993+
this._updateRowState(row, 'details-opened', model.detailsOpened);
981994

982995
this._generateCellClassNames(row, model);
983996
this._filterDragAndDrop(row, model);
@@ -994,6 +1007,35 @@ class Grid extends ElementMixin(
9941007
this._a11yUpdateRowExpanded(row, model.expanded);
9951008
}
9961009

1010+
/**
1011+
* @param {!HTMLElement} row
1012+
* @param {string} state
1013+
* @param {boolean | string | null | undefined} value
1014+
* @param {boolean} partWithValue
1015+
* @protected
1016+
*/
1017+
_updateRowState(row, state, value, partWithValue) {
1018+
switch (typeof value) {
1019+
case 'boolean':
1020+
row.toggleAttribute(state, value);
1021+
break;
1022+
case 'string':
1023+
row.setAttribute(state, value);
1024+
break;
1025+
default:
1026+
// Value set to null / undefined
1027+
row.removeAttribute(state);
1028+
break;
1029+
}
1030+
1031+
const part = partWithValue ? `${state}-${value}-row` : `${state}-row`;
1032+
if (value || value === '') {
1033+
addValueToAttribute(row, 'part', part);
1034+
} else {
1035+
removeValueFromAttribute(row, 'part', part);
1036+
}
1037+
}
1038+
9971039
/** @private */
9981040
_resizeHandler() {
9991041
this._updateDetailsCellHeights();

‎packages/grid/test/data-provider.test.js‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -414,11 +414,26 @@ describe('data provider', () => {
414414
});
415415
});
416416

417-
it('should toggle expanded attribute on the row', () => {
418-
expandIndex(grid, 0);
419-
expect(getRows(grid.$.items)[0].hasAttribute('expanded')).to.be.true;
420-
collapseIndex(grid, 0);
421-
expect(getRows(grid.$.items)[0].hasAttribute('expanded')).to.be.false;
417+
describe('row state', () => {
418+
let bodyRows;
419+
420+
beforeEach(() => {
421+
bodyRows = getRows(grid.$.items);
422+
});
423+
424+
it('should toggle expanded attribute on the row', () => {
425+
expandIndex(grid, 0);
426+
expect(bodyRows[0].hasAttribute('expanded')).to.be.true;
427+
collapseIndex(grid, 0);
428+
expect(bodyRows[0].hasAttribute('expanded')).to.be.false;
429+
});
430+
431+
it('should update part attribute when expanding / collapsing', () => {
432+
expandIndex(grid, 0);
433+
expect(bodyRows[0].getAttribute('part')).to.contain('expanded-row');
434+
collapseIndex(grid, 0);
435+
expect(bodyRows[0].getAttribute('part')).to.not.contain('expanded-row');
436+
});
422437
});
423438

424439
it('should request pages from 0', () => {

0 commit comments

Comments
 (0)