Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add min-width on the columns #7557

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/grid/src/vaadin-grid-column-mixin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,15 @@ export declare class GridColumnMixinClass<
*/
width: string | null | undefined;

/**
* Min-width of the cells for this column.
*
* Please note that using the `em` length unit is discouraged as
* it might lead to misalignment issues if the header, body, and footer
* cells have different font sizes. Instead, use `rem` if you need
* a length unit relative to the font size.
*/
minWidth: string | null | undefined;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: space missing after this line

/**
* Flex grow ratio for the cell widths. When set to 0, cell width is fixed.
* @attr {number} flex-grow
Expand Down
24 changes: 24 additions & 0 deletions packages/grid/src/vaadin-grid-column-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ export const ColumnBaseMixin = (superClass) =>
static get observers() {
return [
'_widthChanged(width, _headerCell, _footerCell, _cells)',
'_minWidthChanged(minWidth, _headerCell, _footerCell, _cells)',
'_frozenChanged(frozen, _headerCell, _footerCell, _cells)',
'_frozenToEndChanged(frozenToEnd, _headerCell, _footerCell, _cells)',
'_flexGrowChanged(flexGrow, _headerCell, _footerCell, _cells)',
Expand Down Expand Up @@ -406,6 +407,17 @@ export const ColumnBaseMixin = (superClass) =>
});
}

/** @private */
_minWidthChanged(minWidth) {
if (this.parentElement && this.parentElement._columnPropChanged) {
this.parentElement._columnPropChanged('minWidth');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

untested code

}

this._allCells.forEach((cell) => {
cell.style.minWidth = minWidth;
});
}

/** @private */
_frozenChanged(frozen) {
if (this.parentElement && this.parentElement._columnPropChanged) {
Expand Down Expand Up @@ -863,6 +875,18 @@ export const GridColumnMixin = (superClass) =>
value: '100px',
sync: true,
},
/**
* Min-width of the cells for this column.
*
* Please note that using the `em` length unit is discouraged as
* it might lead to misalignment issues if the header, body, and footer
* cells have different font sizes. Instead, use `rem` if you need
* a length unit relative to the font size.
*/
minWidth: {
type: String,
sync: true,
},

/**
* Flex grow ratio for the cell widths. When set to 0, cell width is fixed.
Expand Down
8 changes: 6 additions & 2 deletions packages/grid/src/vaadin-grid-column-resizing-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,12 @@ export const ColumnResizingMixin = (superClass) =>
} else {
maxWidth = cellWidth + (isRTL ? cellRect.left - eventX : eventX - cellRect.right);
}

column.width = `${Math.max(minWidth, maxWidth)}px`;
const calculatedWidth = Math.max(minWidth, maxWidth);
if (column.minWidth) {
column.width = `max(${column.minWidth}, ${calculatedWidth}px)`;
} else {
column.width = `${calculatedWidth}px`;
}
column.flexGrow = 0;
}
// Fix width and flex-grow for all preceding columns
Expand Down
18 changes: 17 additions & 1 deletion packages/grid/src/vaadin-grid-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,12 @@ export const GridMixin = (superClass) =>
this.__calculateAndCacheIntrinsicWidths(cols);

cols.forEach((col) => {
col.width = `${this.__getDistributedWidth(col)}px`;
const calculatedWidth = this.__getDistributedWidth(col);
if (col.minWidth) {
col.width = `max(${col.minWidth}, ${calculatedWidth}px)`;
} else {
col.width = `${calculatedWidth}px`;
}
});
}

Expand Down Expand Up @@ -507,6 +512,17 @@ export const GridMixin = (superClass) =>
} else {
this._recalculateColumnWidths(cols);
}
// update the columns without autowidth and set the min width
const noAutoWidthCols = this._getColumns().filter((col) => !col.hidden && !col.autoWidth);

noAutoWidthCols.forEach((col) => {
const calculatedWidth = col.width;
if (col.minWidth) {
col.width = `max(${col.minWidth}, ${calculatedWidth})`;
} else {
col.width = `${calculatedWidth}`;
}
});
Comment on lines +515 to +525
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

untested code

}

/** @private */
Expand Down
3 changes: 3 additions & 0 deletions packages/grid/test/column-min-width-lit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import '../theme/lumo/lit-all-imports.js';
import '../src/lit-all-imports.js';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could align with the polymer test and import the styled grid (import '../lit-all-imports.js';)

import './column-min-width.common.js';
2 changes: 2 additions & 0 deletions packages/grid/test/column-min-width-polymer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import '../all-imports.js';
import './column-min-width.common.js';
Loading
Loading