Skip to content

Commit a47e6ed

Browse files
authored
feat: add hasChildren property to grid row model (#9956)
1 parent d169f3e commit a47e6ed

15 files changed

+32
-12
lines changed

packages/grid/src/vaadin-grid-column-mixin.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ export declare class GridColumnMixinClass<
147147
* - `model.level` Level of the tree represented with a horizontal offset of the toggle button.
148148
* - `model.selected` Selected state.
149149
* - `model.detailsOpened` Details opened state.
150+
* - `model.hasChildren` Whether the item has children.
150151
*/
151152
renderer: GridBodyRenderer<TItem, Column> | null | undefined;
152153

packages/grid/src/vaadin-grid-column-mixin.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,7 @@ export const GridColumnMixin = (superClass) =>
885885
* - `model.level` Level of the tree represented with a horizontal offset of the toggle button.
886886
* - `model.selected` Selected state.
887887
* - `model.detailsOpened` Details opened state.
888+
* - `model.hasChildren` Whether the item has children.
888889
*
889890
* @type {GridBodyRenderer | null | undefined}
890891
*/

packages/grid/src/vaadin-grid-data-provider-mixin.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,15 @@ export const DataProviderMixin = (superClass) =>
222222
return this.__expandedKeys && this.__expandedKeys.has(this.getItemId(item));
223223
}
224224

225+
/**
226+
* @param {!GridItem} item
227+
* @return {boolean}
228+
* @protected
229+
*/
230+
_hasChildren(item) {
231+
return this.itemHasChildrenPath && item && !!get(this.itemHasChildrenPath, item);
232+
}
233+
225234
/** @private */
226235
_expandedItemsChanged() {
227236
this._dataProviderController.recalculateFlatSize();

packages/grid/src/vaadin-grid-event-context-mixin.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export interface GridEventContext<TItem> {
1313
index?: number;
1414
selected?: boolean;
1515
detailsOpened?: boolean;
16+
hasChildren?: boolean;
1617
expanded?: boolean;
1718
level?: number;
1819
}
@@ -34,6 +35,7 @@ export declare class EventContextMixinClass<TItem> {
3435
* - `detailsOpened`: whether the row details are open for the item
3536
* - `expanded`: the expanded state of the tree toggle
3637
* - `level`: the tree hierarchy level
38+
* - `hasChildren`: whether the item has children
3739
*
3840
* The returned object is populated only when a grid cell, header, footer or row details is found in `event.composedPath()`.
3941
* This means mostly mouse and keyboard events. If such a grid part is not found in the path, an empty object is returned.

packages/grid/src/vaadin-grid-event-context-mixin.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export const EventContextMixin = (superClass) =>
2121
* - `detailsOpened`: whether the row details are open for the item
2222
* - `expanded`: the expanded state of the tree toggle
2323
* - `level`: the tree hierarchy level
24+
* - `hasChildren`: whether the item has children
2425
*
2526
* The returned object is populated only when a grid cell, header, footer or row details is found in `event.composedPath()`.
2627
* This means mostly mouse and keyboard events. If such a grid part is not found in the path, an empty object is returned.

packages/grid/src/vaadin-grid-keyboard-navigation-mixin.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { isKeyboardActive } from '@vaadin/a11y-base/src/focus-utils.js';
77
import { animationFrame } from '@vaadin/component-base/src/async.js';
88
import { Debouncer } from '@vaadin/component-base/src/debounce.js';
99
import { addValueToAttribute, removeValueFromAttribute } from '@vaadin/component-base/src/dom-utils.js';
10-
import { get } from '@vaadin/component-base/src/path-utils.js';
1110

1211
function isRow(element) {
1312
return element instanceof HTMLTableRowElement;
@@ -286,10 +285,7 @@ export const KeyboardNavigationMixin = (superClass) =>
286285

287286
/** @private */
288287
__isRowExpandable(row) {
289-
if (this.itemHasChildrenPath) {
290-
const item = row._item;
291-
return !!(item && get(this.itemHasChildrenPath, item) && !this._isExpanded(item));
292-
}
288+
return this._hasChildren(row._item) && !this._isExpanded(row._item);
293289
}
294290

295291
/** @private */

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export interface GridItemModel<TItem> {
6262
expanded?: boolean;
6363
level?: number;
6464
detailsOpened?: boolean;
65+
hasChildren?: boolean;
6566
}
6667

6768
/**

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,7 @@ export const GridMixin = (superClass) =>
843843
level: this.__getRowLevel(row),
844844
expanded: this._isExpanded(row._item),
845845
selected: this._isSelected(row._item),
846+
hasChildren: this._hasChildren(row._item),
846847
detailsOpened: !!this.rowDetailsRenderer && this._isDetailsOpened(row._item),
847848
};
848849
}

packages/grid/src/vaadin-grid-tree-column-mixin.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export const GridTreeColumnMixin = (superClass) =>
3737
*
3838
* @private
3939
*/
40-
__defaultRenderer(root, _column, { item, expanded, level }) {
40+
__defaultRenderer(root, _column, { item, expanded, level, hasChildren }) {
4141
let toggle = root.firstElementChild;
4242
if (!toggle) {
4343
toggle = document.createElement('vaadin-grid-tree-toggle');
@@ -48,7 +48,7 @@ export const GridTreeColumnMixin = (superClass) =>
4848
toggle.__item = item;
4949
toggle.__rendererExpanded = expanded;
5050
toggle.expanded = expanded;
51-
toggle.leaf = this.__isLeafItem(item, this._grid.itemHasChildrenPath);
51+
toggle.leaf = !hasChildren;
5252
toggle.textContent = this.__getToggleContent(this.path, item);
5353
toggle.level = level;
5454
}
@@ -83,11 +83,6 @@ export const GridTreeColumnMixin = (superClass) =>
8383
}
8484
}
8585

86-
/** @private */
87-
__isLeafItem(item, itemHasChildrenPath) {
88-
return !item || !item[itemHasChildrenPath];
89-
}
90-
9186
/** @private */
9287
__getToggleContent(path, item) {
9388
return path && get(path, item);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ export type GridDefaultItem = any;
9393
* `expanded` | Boolean | True if the item's tree sublevel is expanded.
9494
* `selected` | Boolean | True if the item is selected.
9595
* `detailsOpened` | Boolean | True if the item's row details are open.
96+
* `hasChildren` | Boolean | True if the item has children
9697
*
9798
* The following helper elements can be used for further customization:
9899
* - [`<vaadin-grid-column-group>`](#/elements/vaadin-grid-column-group)

0 commit comments

Comments
 (0)