Skip to content

Commit

Permalink
feat: add itemHasChildrenPath property (#3468)
Browse files Browse the repository at this point in the history
  • Loading branch information
web-padawan authored Feb 21, 2022
1 parent 61076cb commit 9804604
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 24 deletions.
6 changes: 6 additions & 0 deletions packages/grid/src/vaadin-grid-data-provider-mixin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ export declare class DataProviderMixinClass<TItem> {
*/
readonly loading: boolean | null | undefined;

/**
* Path to an item sub-property that indicates whether the item has child items.
* @attr {string} item-has-children-path
*/
itemHasChildrenPath: string;

/**
* Path to an item sub-property that identifies the item.
* @attr {string} item-id-path
Expand Down
9 changes: 9 additions & 0 deletions packages/grid/src/vaadin-grid-data-provider-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,15 @@ export const DataProviderMixin = (superClass) =>
value: false
},

/**
* Path to an item sub-property that indicates whether the item has child items.
* @attr {string} item-has-children-path
*/
itemHasChildrenPath: {
type: String,
value: 'children'
},

/**
* Path to an item sub-property that identifies the item.
* @attr {string} item-id-path
Expand Down
10 changes: 4 additions & 6 deletions packages/grid/src/vaadin-grid-keyboard-navigation-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,12 @@ export const KeyboardNavigationMixin = (superClass) =>
}
}

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

/** @private */
Expand Down
1 change: 1 addition & 0 deletions packages/grid/src/vaadin-grid-tree-column.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ declare class GridTreeColumn<TItem = GridDefaultItem> extends GridColumn<TItem>
/**
* JS Path of the property in the item that indicates whether the item has child items.
* @attr {string} item-has-children-path
* @deprecated Use `grid.itemHasChildrenPath` instead.
*/
itemHasChildrenPath: string | null | undefined;
}
Expand Down
18 changes: 16 additions & 2 deletions packages/grid/src/vaadin-grid-tree-column.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ class GridTreeColumn extends GridColumn {
/**
* JS Path of the property in the item that indicates whether the item has child items.
* @attr {string} item-has-children-path
* @deprecated Use `grid.itemHasChildrenPath` instead.
*/
itemHasChildrenPath: {
type: String,
value: 'children'
observer: '_itemHasChildrenPathChanged'
}
};
}
Expand Down Expand Up @@ -68,7 +69,7 @@ class GridTreeColumn extends GridColumn {
toggle.__item = item;
toggle.__rendererExpanded = expanded;
toggle.expanded = expanded;
toggle.leaf = this.__isLeafItem(item, this.itemHasChildrenPath);
toggle.leaf = this.__isLeafItem(item, this._grid.itemHasChildrenPath);
toggle.textContent = this.__getToggleContent(this.path, item);
toggle.level = level;
}
Expand All @@ -84,6 +85,19 @@ class GridTreeColumn extends GridColumn {
return this.__defaultRenderer;
}

/** @private */
_itemHasChildrenPathChanged(itemHasChildrenPath) {
if (itemHasChildrenPath) {
console.warn(
`WARNING: Since Vaadin 23, itemHasChildrenPath on <vaadin-grid-tree-column> is deprecated. Please set this property on the <vaadin-grid> instead.`
);

if (this._grid) {
this._grid.itemHasChildrenPath = itemHasChildrenPath;
}
}
}

/**
* Expands or collapses the row once the tree toggle is switched.
* The listener handles only user-fired events.
Expand Down
2 changes: 0 additions & 2 deletions packages/grid/test/accessibility.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,9 @@ describe('accessibility', () => {
beforeEach(() => {
grid = fixtureSync(`
<vaadin-grid item-id-path="name">
<vaadin-grid-tree-column path="name" header="" width="100px" flex-shrink="0"></vaadin-grid-tree-column>
<vaadin-grid-column path="name" width="200px" flex-shrink="0"></vaadin-grid-column>
</vaadin-grid>
`);

grid.dataProvider = hierarchicalDataProvider;
flushGrid(grid);
});
Expand Down
4 changes: 4 additions & 0 deletions packages/grid/test/data-provider.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ describe('data provider', () => {
});

describe('first level', () => {
it('should set itemHasChildren path by default', () => {
expect(grid.itemHasChildrenPath).to.equal('children');
});

it('should have collapsed items by default', () => {
for (let i = 0; i < grid._effectiveSize; i++) {
expect(isIndexExpanded(grid, i)).to.be.false;
Expand Down
4 changes: 2 additions & 2 deletions packages/grid/test/scroll-to-index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ const fixtures = {
</vaadin-grid>
`,
treeGrid: `
<vaadin-grid style="width: 200px; height: 500px;">
<vaadin-grid-tree-column path="name" header="foo" item-has-children-path="hasChildren"></vaadin-grid-tree-column>
<vaadin-grid style="width: 200px; height: 500px;" item-has-children-path="hasChildren">
<vaadin-grid-tree-column path="name" header="foo"></vaadin-grid-tree-column>
</vaadin-grid>
`
};
Expand Down
44 changes: 32 additions & 12 deletions packages/grid/test/tree-toggle.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,24 +144,44 @@ describe('tree toggle', () => {
expect(toggle.level).to.equal(0);
});

it('should have a higher level', () => {
column.itemHasChildrenPath = 'hasChildren';
toggle.expanded = true;
const childToggle = getBodyCellContent(grid, 1, 0).firstElementChild;
expect(childToggle.level).to.equal(1);
});

it('should not be a leaf', () => {
column.itemHasChildrenPath = 'hasChildren';
expect(toggle.leaf).to.be.false;
});

it('should ignore a custom renderer', () => {
column.renderer = (root) => {
root.innerHTML = 'cell';
};

expect(getBodyCellContent(grid, 0, 0).firstElementChild).to.equal(toggle);
});

describe('itemHasChildrenPath', () => {
beforeEach(() => {
sinon.stub(console, 'warn');
});

afterEach(() => {
console.warn.restore();
});

it('should have a higher level', () => {
column.itemHasChildrenPath = 'hasChildren';
toggle.expanded = true;
const childToggle = getBodyCellContent(grid, 1, 0).firstElementChild;
expect(childToggle.level).to.equal(1);
});

it('should not be a leaf', () => {
column.itemHasChildrenPath = 'hasChildren';
expect(toggle.leaf).to.be.false;
});

it('should warn when setting column property', () => {
column.itemHasChildrenPath = 'hasChildren';
expect(console.warn.called).to.be.true;
});

it('should forward column property to the grid', () => {
column.itemHasChildrenPath = 'hasChildren';
expect(grid.itemHasChildrenPath).to.equal('hasChildren');
});
});
});
});
1 change: 1 addition & 0 deletions packages/grid/test/typings/grid.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ assertType<number>(narrowedGrid.pageSize);
assertType<number>(narrowedGrid.size);
assertType<boolean | null | undefined>(narrowedGrid.loading);
assertType<string | null | undefined>(narrowedGrid.itemIdPath);
assertType<string>(narrowedGrid.itemHasChildrenPath);

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

0 comments on commit 9804604

Please sign in to comment.