Skip to content

Commit

Permalink
feat: dashboard widget rowspan (#7781)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomivirkki authored Sep 10, 2024
1 parent a590b1b commit a51684e
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 3 deletions.
2 changes: 1 addition & 1 deletion dev/dashboard-layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
</vaadin-dashboard-widget>

<vaadin-dashboard-section section-title="Section">
<vaadin-dashboard-widget widget-title="Sales closed this month">
<vaadin-dashboard-widget style="--vaadin-dashboard-item-rowspan: 2" widget-title="Sales closed this month">
<div class="kpi-number">54 000€</div>
</vaadin-dashboard-widget>

Expand Down
1 change: 1 addition & 0 deletions dev/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
items: [
{
title: 'Sales closed this month',
rowspan: 2,
content: '54 000€',
type: 'kpi',
},
Expand Down
5 changes: 5 additions & 0 deletions packages/dashboard/src/vaadin-dashboard-layout-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,18 @@ export const DashboardLayoutMixin = (superClass) =>
}
::slotted(*) {
/* The grid-column value applied to children */
--_vaadin-dashboard-item-column: span
min(
var(--vaadin-dashboard-item-colspan, 1),
var(--_vaadin-dashboard-effective-col-count, var(--_vaadin-dashboard-col-count))
);
grid-column: var(--_vaadin-dashboard-item-column);
/* The grid-row value applied to children */
--_vaadin-dashboard-item-row: span var(--vaadin-dashboard-item-rowspan, 1);
grid-row: var(--_vaadin-dashboard-item-row);
}
`;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/dashboard/src/vaadin-dashboard-section.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class DashboardSection extends ControllerMixin(ElementMixin(PolylitMixin(LitElem
);
grid-column: var(--_vaadin-dashboard-item-column);
--_vaadin-dashboard-item-row: span var(--vaadin-dashboard-item-rowspan, 1);
grid-row: var(--_vaadin-dashboard-item-row);
}
header {
Expand Down
1 change: 1 addition & 0 deletions packages/dashboard/src/vaadin-dashboard-widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class DashboardWidget extends ControllerMixin(ElementMixin(PolylitMixin(LitEleme
display: flex;
flex-direction: column;
grid-column: var(--_vaadin-dashboard-item-column);
grid-row: var(--_vaadin-dashboard-item-row);
position: relative;
}
Expand Down
5 changes: 5 additions & 0 deletions packages/dashboard/src/vaadin-dashboard.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ export interface DashboardItem {
* The column span of the item
*/
colspan?: number;

/**
* The row span of the item
*/
rowspan?: number;
}

export interface DashboardSectionItem<TItem extends DashboardItem> {
Expand Down
1 change: 1 addition & 0 deletions packages/dashboard/src/vaadin-dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ class Dashboard extends ControllerMixin(DashboardLayoutMixin(ElementMixin(Themab
const itemDragged = this.__widgetReorderController.draggedItem === item;
const style = `
${item.colspan ? `--vaadin-dashboard-item-colspan: ${item.colspan};` : ''}
${item.rowspan ? `--vaadin-dashboard-item-rowspan: ${item.rowspan};` : ''}
${itemDragged ? '--_vaadin-dashboard-item-placeholder-display: block;' : ''}
`.trim();

Expand Down
45 changes: 45 additions & 0 deletions packages/dashboard/test/dashboard-layout.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
setMaximumColumnWidth,
setMinimumColumnWidth,
setMinimumRowHeight,
setRowspan,
} from './helpers.js';

describe('dashboard layout', () => {
Expand Down Expand Up @@ -261,6 +262,36 @@ describe('dashboard layout', () => {
});
});

describe('row span', () => {
it('should span multiple rows', async () => {
setMinimumRowHeight(dashboard, 100);
setRowspan(childElements[0], 2);
await nextFrame();

/* prettier-ignore */
expectLayout(dashboard, [
[0, 1],
[0],
]);
});

it('should span multiple rows on a single column', async () => {
setMinimumRowHeight(dashboard, 100);
setRowspan(childElements[0], 2);
await nextFrame();

dashboard.style.width = `${columnWidth}px`;
await nextFrame();

/* prettier-ignore */
expectLayout(dashboard, [
[0],
[0],
[1],
]);
});
});

describe('gap', () => {
it('should have a default gap', () => {
// Clear the gap used in the tests
Expand Down Expand Up @@ -428,6 +459,20 @@ describe('dashboard layout', () => {
]);
});

it('should span multiple rows inside a section', async () => {
// Using a minimum row height here causes Firefox to crash, disabling for now
// setMinimumRowHeight(dashboard, 100);
setRowspan(childElements[2], 2);
await nextFrame();

/* prettier-ignore */
expectLayout(dashboard, [
[0, 1],
[2, 3],
[2]
]);
});

it('should use minimum row height for all section rows', async () => {
dashboard.style.width = `${columnWidth}px`;
setMinimumRowHeight(dashboard, 300);
Expand Down
18 changes: 18 additions & 0 deletions packages/dashboard/test/dashboard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,24 @@ describe('dashboard', () => {
});
});

describe('row span', () => {
it('should span one row by default', () => {
dashboard.style.width = `${columnWidth}px`;
const widgets = [getElementFromCell(dashboard, 0, 0), getElementFromCell(dashboard, 1, 0)];
expect(widgets[0]).to.not.equal(widgets[1]);
});

it('should span multiple rows', async () => {
dashboard.style.width = `${columnWidth}px`;
dashboard.items = [{ rowspan: 2, id: 'Item 0' }];
await nextFrame();

const widget = getElementFromCell(dashboard, 0, 0);
expect(widget).to.have.property('widgetTitle', 'Item 0 title');
expect(getElementFromCell(dashboard, 1, 0)).to.equal(widget);
});
});

describe('section', () => {
beforeEach(async () => {
dashboard.items = [
Expand Down
7 changes: 7 additions & 0 deletions packages/dashboard/test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ export function setColspan(element: HTMLElement, colspan?: number): void {
element.style.setProperty('--vaadin-dashboard-item-colspan', colspan !== undefined ? `${colspan}` : null);
}

/**
* Sets the row span of the element
*/
export function setRowspan(element: HTMLElement, rowspan?: number): void {
element.style.setProperty('--vaadin-dashboard-item-rowspan', rowspan !== undefined ? `${rowspan}` : null);
}

/**
* Sets the gap between the cells of the dashboard.
*/
Expand Down
9 changes: 7 additions & 2 deletions packages/dashboard/test/typings/dashboard.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ interface TestDashboardItem extends DashboardItem {
const genericDashboard = document.createElement('vaadin-dashboard');
assertType<Dashboard>(genericDashboard);

assertType<{ colspan?: number; rowspan?: number }>(genericDashboard.items[0] as DashboardItem);
assertType<{ items: DashboardItem[]; title?: string | null }>(
genericDashboard.items[0] as DashboardSectionItem<DashboardItem>,
);

assertType<ElementMixinClass>(genericDashboard);
assertType<DashboardLayoutMixinClass>(genericDashboard);
assertType<Array<DashboardItem | DashboardSectionItem<DashboardItem>> | null | undefined>(genericDashboard.items);
Expand All @@ -34,8 +39,8 @@ assertType<Dashboard<TestDashboardItem>>(narrowedDashboard);
assertType<Array<TestDashboardItem | DashboardSectionItem<TestDashboardItem>>>(narrowedDashboard.items);
assertType<DashboardRenderer<TestDashboardItem> | null | undefined>(narrowedDashboard.renderer);
assertType<
| { colspan?: number; testProperty: string }
| { title?: string | null; items: Array<{ colspan?: number; testProperty: string }> }
| { colspan?: number; rowspan?: number; testProperty: string }
| { title?: string | null; items: Array<{ colspan?: number; rowspan?: number; testProperty: string }> }
>(narrowedDashboard.items[0]);

narrowedDashboard.addEventListener('dashboard-item-reorder-start', (event) => {
Expand Down

0 comments on commit a51684e

Please sign in to comment.