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

feat: grid column header and footer part names #6629

Merged
merged 6 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 10 additions & 0 deletions packages/grid/src/vaadin-grid-column-mixin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ export declare class ColumnBaseMixinClass<TItem, Column extends ColumnBaseMixinC
* - `column` The `<vaadin-grid-column>` element.
*/
footerRenderer: GridHeaderFooterRenderer<TItem, Column> | null | undefined;

/**
* Custom part name for the header cell.
web-padawan marked this conversation as resolved.
Show resolved Hide resolved
*/
headerPartName: string | null | undefined;

/**
* Custom part name for the footer cell.
*/
footerPartName: string | null | undefined;
}

export interface GridColumnMixin<TItem, Column extends GridColumnMixinClass<TItem, Column>>
Expand Down
31 changes: 31 additions & 0 deletions packages/grid/src/vaadin-grid-column-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,20 @@ export const ColumnBaseMixin = (superClass) =>
type: String,
},

/**
* Custom part name for the header cell.
*/
headerPartName: {
type: String,
},

/**
* Custom part name for the footer cell.
*/
footerPartName: {
type: String,
},

/**
* @type {boolean}
* @protected
Expand Down Expand Up @@ -238,6 +252,7 @@ export const ColumnBaseMixin = (superClass) =>
'_reorderStatusChanged(_reorderStatus, _headerCell, _footerCell, _cells.*)',
'_hiddenChanged(hidden, _headerCell, _footerCell, _cells.*)',
'_rowHeaderChanged(rowHeader, _cells.*)',
'__headerFooterPartNameChanged(_headerCell, _footerCell, headerPartName, footerPartName)',
];
}

Expand Down Expand Up @@ -631,6 +646,22 @@ export const ColumnBaseMixin = (superClass) =>
this._renderHeaderCellContent(headerRenderer, headerCell);
}

/** @private */
__headerFooterPartNameChanged(headerCell, footerCell, headerPartName, footerPartName) {
[
{ cell: headerCell, partName: headerPartName, currentName: '__currentHeaderPartName' },
{ cell: footerCell, partName: footerPartName, currentName: '__currentFooterPartName' },
].forEach(({ cell, partName, currentName }) => {
if (cell) {
const currentPartNames = cell[currentName] || [];
cell.part.remove(...currentPartNames);

cell[currentName] = partName ? partName.trim().split(' ') : [];
cell.part.add(...cell[currentName]);
}
});
}

/**
* Renders the content of body cells using a renderer.
*
Expand Down
87 changes: 87 additions & 0 deletions packages/grid/test/styling.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,91 @@ describe('styling', () => {

runStylingTest('parts', 'cellPartNameGenerator', 'generateCellPartNames', assertPartNames);
});

describe('header and footer part name', () => {
let column;
let headerCell;
let footerCell;

beforeEach(() => {
column = grid.querySelector('vaadin-grid-column');
column.footerRenderer = (root) => {
root.textContent = 'footer';
};
headerCell = getContainerCell(grid.$.header, 0, 0);
footerCell = getContainerCell(grid.$.footer, 0, 0);
});

it('should add a header and footer part name', () => {
column.headerPartName = 'foobar';
column.footerPartName = 'bazqux';

expect(headerCell.getAttribute('part')).to.contain('foobar');
expect(footerCell.getAttribute('part')).to.contain('bazqux');
DiegoCardoso marked this conversation as resolved.
Show resolved Hide resolved
});

it('should clear the header and footer part name', () => {
column.headerPartName = 'foobar';
column.footerPartName = 'bazqux';

column.headerPartName = '';
column.footerPartName = '';

expect(headerCell.getAttribute('part')).to.not.contain('foobar');
expect(footerCell.getAttribute('part')).to.not.contain('bazqux');
});

it('should add multiple header and footer part names', () => {
column.headerPartName = 'foobar bazqux';
column.footerPartName = 'bazqux foobar';

expect(headerCell.getAttribute('part')).to.contain('foobar');
expect(headerCell.getAttribute('part')).to.contain('bazqux');
expect(footerCell.getAttribute('part')).to.contain('foobar');
expect(footerCell.getAttribute('part')).to.contain('bazqux');
});

it('should remove one header and footer part name', () => {
column.headerPartName = 'foobar bazqux';
column.footerPartName = 'bazqux foobar';

column.headerPartName = 'foobar';
column.footerPartName = 'bazqux';

expect(headerCell.getAttribute('part')).to.contain('foobar');
expect(headerCell.getAttribute('part')).to.not.contain('bazqux');
expect(footerCell.getAttribute('part')).to.contain('bazqux');
expect(footerCell.getAttribute('part')).to.not.contain('foobar');
});

it('should add a header and footer part name with trailing whitespace', () => {
column.headerPartName = 'foobar ';
column.footerPartName = ' bazqux';

expect(headerCell.getAttribute('part')).to.contain('foobar');
expect(footerCell.getAttribute('part')).to.contain('bazqux');
});

it('should clear the header and footer part name with null', () => {
column.headerPartName = 'foobar';
column.footerPartName = 'bazqux';

column.headerPartName = null;
column.footerPartName = null;

expect(headerCell.getAttribute('part')).to.not.contain('foobar');
expect(footerCell.getAttribute('part')).to.not.contain('bazqux');
});

it('should clear the header and footer part name with undefined', () => {
column.headerPartName = 'foobar';
column.footerPartName = 'bazqux';

column.headerPartName = undefined;
column.footerPartName = undefined;

expect(headerCell.getAttribute('part')).to.not.contain('foobar');
expect(footerCell.getAttribute('part')).to.not.contain('bazqux');
});
});
});
4 changes: 4 additions & 0 deletions packages/grid/test/typings/grid.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ assertType<string | null | undefined>(narrowedColumn.header);
assertType<GridColumnTextAlign | null | undefined>(narrowedColumn.textAlign);
assertType<string | null | undefined>(narrowedColumn.path);
assertType<boolean>(narrowedColumn.autoWidth);
assertType<string | null | undefined>(narrowedColumn.headerPartName);
assertType<string | null | undefined>(narrowedColumn.footerPartName);

/* GridColumnGroup */
const genericColumnGroup = document.createElement('vaadin-grid-column-group');
Expand All @@ -258,6 +260,8 @@ assertType<GridColumnGroup>(genericColumnGroup);
const narrowedColumnGroup = genericColumnGroup as GridColumnGroup<TestGridItem>;
assertType<HTMLElement>(narrowedColumnGroup);
assertType<ColumnBaseMixinClass<TestGridItem, GridColumnGroup>>(narrowedColumnGroup);
assertType<string | null | undefined>(narrowedColumnGroup.headerPartName);
assertType<string | null | undefined>(narrowedColumnGroup.footerPartName);

narrowedColumnGroup.headerRenderer = (root, column) => {
assertType<HTMLElement>(root);
Expand Down