From 100c789e57d2ee822e26dfdc145d63db0d6a91c4 Mon Sep 17 00:00:00 2001 From: Diego Cardoso Date: Tue, 10 Oct 2023 10:51:32 +0300 Subject: [PATCH] fix: make auto-width and text-align usable in custom slot (#6602) (#6621) --- packages/grid/src/vaadin-grid-column.js | 2 +- packages/grid/src/vaadin-grid.js | 5 ++- packages/grid/test/grid-wrapper.test.js | 60 +++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 packages/grid/test/grid-wrapper.test.js diff --git a/packages/grid/src/vaadin-grid-column.js b/packages/grid/src/vaadin-grid-column.js index f2c0045823..d206ae18be 100644 --- a/packages/grid/src/vaadin-grid-column.js +++ b/packages/grid/src/vaadin-grid-column.js @@ -437,7 +437,7 @@ export const ColumnBaseMixin = (superClass) => /** @private */ _textAlignChanged(textAlign) { - if (textAlign === undefined) { + if (textAlign === undefined || this._grid === undefined) { return; } if (['start', 'end', 'center'].indexOf(textAlign) === -1) { diff --git a/packages/grid/src/vaadin-grid.js b/packages/grid/src/vaadin-grid.js index 7d6e3abd18..4db4687398 100644 --- a/packages/grid/src/vaadin-grid.js +++ b/packages/grid/src/vaadin-grid.js @@ -595,7 +595,10 @@ class Grid extends ElementMixin( return 0; } - const columnWidth = Math.max(this.__getIntrinsicWidth(col), this.__getDistributedWidth(col.parentElement, col)); + const columnWidth = Math.max( + this.__getIntrinsicWidth(col), + this.__getDistributedWidth((col.assignedSlot || col).parentElement, col), + ); // We're processing a regular grid-column and not a grid-column-group if (!innerColumn) { diff --git a/packages/grid/test/grid-wrapper.test.js b/packages/grid/test/grid-wrapper.test.js new file mode 100644 index 0000000000..c934199693 --- /dev/null +++ b/packages/grid/test/grid-wrapper.test.js @@ -0,0 +1,60 @@ +import { expect } from '@esm-bundle/chai'; +import { fixtureSync, nextFrame } from '@vaadin/testing-helpers'; +import '../all-imports.js'; +import { flushGrid, getBodyCellContent } from './helpers.js'; + +class GridWrapper extends HTMLElement { + constructor() { + super(); + this.attachShadow({ mode: 'open' }).innerHTML = ` + + + + + + + `; + } +} + +customElements.define('grid-wrapper', GridWrapper); + +async function initGrid(wrapper) { + const grid = wrapper.shadowRoot.querySelector('vaadin-grid'); + grid.items = Array.from({ length: 10 }).map((_, i) => ({ name: `Person ${i}` })); + flushGrid(grid); + await nextFrame(); + return grid; +} + +describe('columns slotted in custom element', () => { + it('should render column correctly with auto-width defined', async () => { + const wrapper = fixtureSync( + '', + ); + const grid = await initGrid(wrapper); + + expect(getBodyCellContent(grid, 0, 0).textContent).to.equal('Person 0'); + }); + + it('should render column correctly with auto-width inside a column-group', async () => { + const wrapper = fixtureSync( + '', + ); + const grid = await initGrid(wrapper); + + expect(getBodyCellContent(grid, 0, 0).textContent).to.equal('Person 0'); + }); + + it('should render column correctly when text-align is defined', async () => { + const wrapper = fixtureSync( + '', + ); + // Delaying the definition of the custom-element so internal is not yet defined + customElements.define('grid-wrapper-delayed', class extends GridWrapper {}); + + const grid = await initGrid(wrapper); + + expect(getBodyCellContent(grid, 0, 0).textContent).to.equal('Person 0'); + }); +});