Skip to content

Commit

Permalink
feat: grid column header and footer part names (#6629)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomivirkki authored Oct 11, 2023
1 parent 35d02a5 commit 553cb9e
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 0 deletions.
14 changes: 14 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,20 @@ 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.
*
* @attr {string} header-part-name
*/
headerPartName: string | null | undefined;

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

export interface GridColumnMixin<TItem, Column extends GridColumnMixinClass<TItem, Column>>
Expand Down
35 changes: 35 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,24 @@ export const ColumnBaseMixin = (superClass) =>
type: String,
},

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

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

/**
* @type {boolean}
* @protected
Expand Down Expand Up @@ -238,6 +256,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 +650,22 @@ export const ColumnBaseMixin = (superClass) =>
this._renderHeaderCellContent(headerRenderer, headerCell);
}

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

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

/**
* 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');
});

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

0 comments on commit 553cb9e

Please sign in to comment.