Skip to content

Commit 9804604

Browse files
authored
feat: add itemHasChildrenPath property (#3468)
1 parent 61076cb commit 9804604

10 files changed

Lines changed: 75 additions & 24 deletions

‎packages/grid/src/vaadin-grid-data-provider-mixin.d.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ export declare class DataProviderMixinClass<TItem> {
101101
*/
102102
readonly loading: boolean | null | undefined;
103103

104+
/**
105+
* Path to an item sub-property that indicates whether the item has child items.
106+
* @attr {string} item-has-children-path
107+
*/
108+
itemHasChildrenPath: string;
109+
104110
/**
105111
* Path to an item sub-property that identifies the item.
106112
* @attr {string} item-id-path

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,15 @@ export const DataProviderMixin = (superClass) =>
186186
value: false
187187
},
188188

189+
/**
190+
* Path to an item sub-property that indicates whether the item has child items.
191+
* @attr {string} item-has-children-path
192+
*/
193+
itemHasChildrenPath: {
194+
type: String,
195+
value: 'children'
196+
},
197+
189198
/**
190199
* Path to an item sub-property that identifies the item.
191200
* @attr {string} item-id-path

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,14 +216,12 @@ export const KeyboardNavigationMixin = (superClass) =>
216216
}
217217
}
218218

219-
// TODO: A tree toggle component should not be the way to determine if the row is expandable
220219
/** @private */
221220
__isRowExpandable(row) {
222-
const treeToggle = [...row.children].reduce(
223-
(value, cell) => value || cell._content.querySelector('vaadin-grid-tree-toggle'),
224-
null
225-
);
226-
return treeToggle && !treeToggle.expanded && !treeToggle.leaf;
221+
if (this.itemHasChildrenPath) {
222+
const item = row._item;
223+
return item && this.get(this.itemHasChildrenPath, item) && !this._isExpanded(item);
224+
}
227225
}
228226

229227
/** @private */

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ declare class GridTreeColumn<TItem = GridDefaultItem> extends GridColumn<TItem>
2828
/**
2929
* JS Path of the property in the item that indicates whether the item has child items.
3030
* @attr {string} item-has-children-path
31+
* @deprecated Use `grid.itemHasChildrenPath` instead.
3132
*/
3233
itemHasChildrenPath: string | null | undefined;
3334
}

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ class GridTreeColumn extends GridColumn {
3434
/**
3535
* JS Path of the property in the item that indicates whether the item has child items.
3636
* @attr {string} item-has-children-path
37+
* @deprecated Use `grid.itemHasChildrenPath` instead.
3738
*/
3839
itemHasChildrenPath: {
3940
type: String,
40-
value: 'children'
41+
observer: '_itemHasChildrenPathChanged'
4142
}
4243
};
4344
}
@@ -68,7 +69,7 @@ class GridTreeColumn extends GridColumn {
6869
toggle.__item = item;
6970
toggle.__rendererExpanded = expanded;
7071
toggle.expanded = expanded;
71-
toggle.leaf = this.__isLeafItem(item, this.itemHasChildrenPath);
72+
toggle.leaf = this.__isLeafItem(item, this._grid.itemHasChildrenPath);
7273
toggle.textContent = this.__getToggleContent(this.path, item);
7374
toggle.level = level;
7475
}
@@ -84,6 +85,19 @@ class GridTreeColumn extends GridColumn {
8485
return this.__defaultRenderer;
8586
}
8687

88+
/** @private */
89+
_itemHasChildrenPathChanged(itemHasChildrenPath) {
90+
if (itemHasChildrenPath) {
91+
console.warn(
92+
`WARNING: Since Vaadin 23, itemHasChildrenPath on <vaadin-grid-tree-column> is deprecated. Please set this property on the <vaadin-grid> instead.`
93+
);
94+
95+
if (this._grid) {
96+
this._grid.itemHasChildrenPath = itemHasChildrenPath;
97+
}
98+
}
99+
}
100+
87101
/**
88102
* Expands or collapses the row once the tree toggle is switched.
89103
* The listener handles only user-fired events.

‎packages/grid/test/accessibility.test.js‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,9 @@ describe('accessibility', () => {
198198
beforeEach(() => {
199199
grid = fixtureSync(`
200200
<vaadin-grid item-id-path="name">
201-
<vaadin-grid-tree-column path="name" header="" width="100px" flex-shrink="0"></vaadin-grid-tree-column>
202201
<vaadin-grid-column path="name" width="200px" flex-shrink="0"></vaadin-grid-column>
203202
</vaadin-grid>
204203
`);
205-
206204
grid.dataProvider = hierarchicalDataProvider;
207205
flushGrid(grid);
208206
});

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,10 @@ describe('data provider', () => {
250250
});
251251

252252
describe('first level', () => {
253+
it('should set itemHasChildren path by default', () => {
254+
expect(grid.itemHasChildrenPath).to.equal('children');
255+
});
256+
253257
it('should have collapsed items by default', () => {
254258
for (let i = 0; i < grid._effectiveSize; i++) {
255259
expect(isIndexExpanded(grid, i)).to.be.false;

‎packages/grid/test/scroll-to-index.test.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ const fixtures = {
3030
</vaadin-grid>
3131
`,
3232
treeGrid: `
33-
<vaadin-grid style="width: 200px; height: 500px;">
34-
<vaadin-grid-tree-column path="name" header="foo" item-has-children-path="hasChildren"></vaadin-grid-tree-column>
33+
<vaadin-grid style="width: 200px; height: 500px;" item-has-children-path="hasChildren">
34+
<vaadin-grid-tree-column path="name" header="foo"></vaadin-grid-tree-column>
3535
</vaadin-grid>
3636
`
3737
};

‎packages/grid/test/tree-toggle.test.js‎

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -144,24 +144,44 @@ describe('tree toggle', () => {
144144
expect(toggle.level).to.equal(0);
145145
});
146146

147-
it('should have a higher level', () => {
148-
column.itemHasChildrenPath = 'hasChildren';
149-
toggle.expanded = true;
150-
const childToggle = getBodyCellContent(grid, 1, 0).firstElementChild;
151-
expect(childToggle.level).to.equal(1);
152-
});
153-
154-
it('should not be a leaf', () => {
155-
column.itemHasChildrenPath = 'hasChildren';
156-
expect(toggle.leaf).to.be.false;
157-
});
158-
159147
it('should ignore a custom renderer', () => {
160148
column.renderer = (root) => {
161149
root.innerHTML = 'cell';
162150
};
163151

164152
expect(getBodyCellContent(grid, 0, 0).firstElementChild).to.equal(toggle);
165153
});
154+
155+
describe('itemHasChildrenPath', () => {
156+
beforeEach(() => {
157+
sinon.stub(console, 'warn');
158+
});
159+
160+
afterEach(() => {
161+
console.warn.restore();
162+
});
163+
164+
it('should have a higher level', () => {
165+
column.itemHasChildrenPath = 'hasChildren';
166+
toggle.expanded = true;
167+
const childToggle = getBodyCellContent(grid, 1, 0).firstElementChild;
168+
expect(childToggle.level).to.equal(1);
169+
});
170+
171+
it('should not be a leaf', () => {
172+
column.itemHasChildrenPath = 'hasChildren';
173+
expect(toggle.leaf).to.be.false;
174+
});
175+
176+
it('should warn when setting column property', () => {
177+
column.itemHasChildrenPath = 'hasChildren';
178+
expect(console.warn.called).to.be.true;
179+
});
180+
181+
it('should forward column property to the grid', () => {
182+
column.itemHasChildrenPath = 'hasChildren';
183+
expect(grid.itemHasChildrenPath).to.equal('hasChildren');
184+
});
185+
});
166186
});
167187
});

‎packages/grid/test/typings/grid.types.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ assertType<number>(narrowedGrid.pageSize);
141141
assertType<number>(narrowedGrid.size);
142142
assertType<boolean | null | undefined>(narrowedGrid.loading);
143143
assertType<string | null | undefined>(narrowedGrid.itemIdPath);
144+
assertType<string>(narrowedGrid.itemHasChildrenPath);
144145

145146
assertType<TestGridItem[] | undefined | null>(narrowedGrid.items);
146147
assertType<TestGridItem | null>(narrowedGrid.activeItem);

0 commit comments

Comments
 (0)