Skip to content

Commit 01f76e7

Browse files
feat: add tooltip support to vaadin-grid (#4416)
Co-authored-by: Tomi Virkki <virkki@vaadin.com>
1 parent fe56f5f commit 01f76e7

4 files changed

Lines changed: 224 additions & 1 deletion

File tree

‎dev/grid.html‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<body>
1111
<script type="module">
1212
import '@vaadin/grid/all-imports';
13+
import '@vaadin/tooltip';
1314

1415
const grid = document.querySelector('vaadin-grid');
1516

@@ -28,6 +29,11 @@
2829

2930
cb(pageItems, levelSize);
3031
};
32+
33+
const tooltip = document.querySelector('[slot="tooltip"]');
34+
tooltip.textGenerator = ({column, item}) => {
35+
return column && column.path && item ? `Tooltip ${column.path} ${item.name}` : '';
36+
};
3137
</script>
3238

3339
<vaadin-grid item-id-path="name">
@@ -36,6 +42,8 @@
3642
<vaadin-grid-column path="name" width="200px" flex-shrink="0"></vaadin-grid-column>
3743
<vaadin-grid-column path="name" width="200px" flex-shrink="0"></vaadin-grid-column>
3844
<vaadin-grid-column path="name" width="200px" flex-shrink="0"></vaadin-grid-column>
45+
46+
<vaadin-tooltip slot="tooltip"></vaadin-tooltip>
3947
</vaadin-grid>
4048
</body>
4149
</html>
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
import { expect } from '@esm-bundle/chai';
2+
import { escKeyDown, fixtureSync, focusin, focusout, mousedown, nextFrame, tabKeyDown } from '@vaadin/testing-helpers';
3+
import sinon from 'sinon';
4+
import '@vaadin/grid/vaadin-grid.js';
5+
import '@vaadin/tooltip/vaadin-tooltip.js';
6+
import { flushGrid, getCell } from '@vaadin/grid/test/helpers.js';
7+
import { mouseenter, mouseleave } from '@vaadin/tooltip/test/helpers.js';
8+
9+
function getHeaderCell(grid, index = 0) {
10+
return grid.$.header.querySelectorAll('[part~="cell"]')[index];
11+
}
12+
13+
describe('tooltip', () => {
14+
let grid, tooltip;
15+
16+
beforeEach(() => {
17+
grid = fixtureSync(`
18+
<vaadin-grid>
19+
<vaadin-grid-column path="firstName"></vaadin-grid-column>
20+
<vaadin-grid-column path="lastName"></vaadin-grid-column>
21+
<vaadin-tooltip slot="tooltip"></vaadin-tooltip>
22+
</vaadin-grid>
23+
`);
24+
grid.items = [
25+
{ firstName: 'John', lastName: 'Doe' },
26+
{ firstName: 'Jane', lastName: 'Doe' },
27+
];
28+
flushGrid(grid);
29+
30+
tooltip = grid.querySelector('vaadin-tooltip');
31+
tooltip.textGenerator = ({ column, item }) => {
32+
return column && column.path && item ? `${column.path}: ${item[column.path]}` : '';
33+
};
34+
});
35+
36+
it('should set manual on the tooltip to true', () => {
37+
expect(tooltip.manual).to.be.true;
38+
});
39+
40+
it('should show tooltip on cell mouseenter', () => {
41+
const cell = getCell(grid, 0);
42+
mouseenter(cell);
43+
expect(tooltip.opened).to.be.true;
44+
});
45+
46+
it('should hide tooltip on cell mouseleave', () => {
47+
const cell = getCell(grid, 0);
48+
mouseenter(cell);
49+
mouseleave(cell);
50+
expect(tooltip.opened).to.be.false;
51+
});
52+
53+
it('should set tooltip target on cell mouseenter', () => {
54+
const cell = getCell(grid, 0);
55+
mouseenter(cell);
56+
expect(tooltip.target).to.be.equal(cell);
57+
});
58+
59+
it('should set tooltip context on cell mouseenter', () => {
60+
const cell = getCell(grid, 0);
61+
mouseenter(cell);
62+
expect(tooltip.context).to.be.instanceOf(Object);
63+
expect(tooltip.context.item.firstName).to.equal('John');
64+
expect(tooltip.context.item.lastName).to.equal('Doe');
65+
});
66+
67+
it('should change tooltip context on header cell mouseenter', () => {
68+
const cell = getCell(grid, 0);
69+
mouseenter(cell);
70+
71+
mouseleave(cell);
72+
mouseenter(getHeaderCell(grid, 0));
73+
74+
expect(tooltip.context.item).to.be.not.ok;
75+
});
76+
77+
it('should hide tooltip on cell mousedown', () => {
78+
const cell = getCell(grid, 0);
79+
mouseenter(cell);
80+
mousedown(cell);
81+
expect(tooltip.opened).to.be.false;
82+
});
83+
84+
it('should show tooltip on cell keyboard focus', () => {
85+
const cell = getCell(grid, 0);
86+
tabKeyDown(document.body);
87+
focusin(cell);
88+
expect(tooltip.opened).to.be.true;
89+
});
90+
91+
it('should set tooltip target on cell keyboard focus', () => {
92+
const cell = getCell(grid, 0);
93+
tabKeyDown(document.body);
94+
focusin(cell);
95+
expect(tooltip.target).to.be.equal(cell);
96+
});
97+
98+
it('should set tooltip context on cell keyboard focus', () => {
99+
const cell = getCell(grid, 0);
100+
tabKeyDown(document.body);
101+
focusin(cell);
102+
expect(tooltip.context).to.be.instanceOf(Object);
103+
expect(tooltip.context.item.firstName).to.equal('John');
104+
expect(tooltip.context.item.lastName).to.equal('Doe');
105+
});
106+
107+
it('should hide tooltip on grid focusout', () => {
108+
const cell = getCell(grid, 0);
109+
tabKeyDown(document.body);
110+
focusin(cell);
111+
focusout(grid);
112+
expect(tooltip.opened).to.be.false;
113+
});
114+
115+
it('should hide tooltip on grid cell content Esc', () => {
116+
const cell = getCell(grid, 0);
117+
tabKeyDown(document.body);
118+
focusin(cell._content);
119+
escKeyDown(cell._content);
120+
expect(tooltip.opened).to.be.false;
121+
});
122+
123+
it('should set tooltip opened to false when the grid is removed', () => {
124+
const cell = getCell(grid, 0);
125+
mouseenter(cell);
126+
127+
grid.remove();
128+
129+
expect(tooltip.opened).to.be.false;
130+
});
131+
132+
it('should not set tooltip target if there is no tooltip', async () => {
133+
const spy = sinon.spy(grid._tooltipController, 'setTarget');
134+
135+
tooltip.remove();
136+
await nextFrame();
137+
138+
const cell = getCell(grid, 0);
139+
mouseenter(cell);
140+
141+
expect(spy.calledOnce).to.be.false;
142+
});
143+
144+
it('should not set tooltip context if there is no tooltip', async () => {
145+
const spy = sinon.spy(grid._tooltipController, 'setContext');
146+
147+
tooltip.remove();
148+
await nextFrame();
149+
150+
const cell = getCell(grid, 0);
151+
mouseenter(cell);
152+
153+
expect(spy.calledOnce).to.be.false;
154+
});
155+
156+
it('should not set tooltip opened if there is no tooltip', async () => {
157+
const spy = sinon.spy(grid._tooltipController, 'setOpened');
158+
159+
tooltip.remove();
160+
await nextFrame();
161+
162+
const cell = getCell(grid, 0);
163+
mouseenter(cell);
164+
165+
expect(spy.calledOnce).to.be.false;
166+
});
167+
});

‎packages/grid/src/vaadin-grid-keyboard-navigation-mixin.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright (c) 2016 - 2022 Vaadin Ltd.
44
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
55
*/
6+
import { isKeyboardActive } from '@vaadin/component-base/src/focus-utils.js';
67

78
/**
89
* @polymerMixin
@@ -584,6 +585,10 @@ export const KeyboardNavigationMixin = (superClass) =>
584585
this.toggleAttribute('navigating', true);
585586
}
586587
}
588+
589+
if (key === 'Escape') {
590+
this._hideTooltip();
591+
}
587592
}
588593

589594
/** @private */
@@ -721,6 +726,7 @@ export const KeyboardNavigationMixin = (superClass) =>
721726
_onFocusOut(e) {
722727
this.toggleAttribute('navigating', false);
723728
this._detectInteracting(e);
729+
this._hideTooltip();
724730
}
725731

726732
/** @private */
@@ -742,6 +748,10 @@ export const KeyboardNavigationMixin = (superClass) =>
742748
// Fire a public event for cell.
743749
const context = this.getEventContext(e);
744750
cell.dispatchEvent(new CustomEvent('cell-focus', { bubbles: true, composed: true, detail: { context } }));
751+
752+
if (isKeyboardActive()) {
753+
this._showTooltip(e);
754+
}
745755
}
746756
}
747757

‎packages/grid/src/vaadin-grid.js‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ import './vaadin-grid-styles.js';
88
import { beforeNextRender } from '@polymer/polymer/lib/utils/render-status.js';
99
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
1010
import { isAndroid, isChrome, isFirefox, isIOS, isSafari, isTouch } from '@vaadin/component-base/src/browser-utils.js';
11+
import { ControllerMixin } from '@vaadin/component-base/src/controller-mixin.js';
1112
import { ElementMixin } from '@vaadin/component-base/src/element-mixin.js';
1213
import { TabindexMixin } from '@vaadin/component-base/src/tabindex-mixin.js';
1314
import { processTemplates } from '@vaadin/component-base/src/templates.js';
15+
import { TooltipController } from '@vaadin/component-base/src/tooltip-controller.js';
1416
import { Virtualizer } from '@vaadin/component-base/src/virtualizer.js';
1517
import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
1618
import { A11yMixin } from './vaadin-grid-a11y-mixin.js';
@@ -258,7 +260,9 @@ class Grid extends ElementMixin(
258260
FilterMixin(
259261
ColumnReorderingMixin(
260262
ColumnResizingMixin(
261-
EventContextMixin(DragAndDropMixin(StylingMixin(TabindexMixin(PolymerElement)))),
263+
ControllerMixin(
264+
EventContextMixin(DragAndDropMixin(StylingMixin(TabindexMixin(PolymerElement)))),
265+
),
262266
),
263267
),
264268
),
@@ -293,6 +297,8 @@ class Grid extends ElementMixin(
293297
<div part="reorder-ghost"></div>
294298
</div>
295299
300+
<slot name="tooltip"></slot>
301+
296302
<div id="focusexit" tabindex="0"></div>
297303
`;
298304
}
@@ -394,6 +400,7 @@ class Grid extends ElementMixin(
394400
disconnectedCallback() {
395401
super.disconnectedCallback();
396402
this.isAttached = false;
403+
this._hideTooltip();
397404
}
398405

399406
/** @private */
@@ -453,6 +460,10 @@ class Grid extends ElementMixin(
453460
new ResizeObserver(() => setTimeout(() => this.__updateFooterPositioning())).observe(this.$.footer);
454461

455462
processTemplates(this);
463+
464+
this._tooltipController = new TooltipController(this);
465+
this.addController(this._tooltipController);
466+
this._tooltipController.setManual(true);
456467
}
457468

458469
/**
@@ -655,6 +666,15 @@ class Grid extends ElementMixin(
655666
cell.id = slotName.replace('-content-', '-');
656667
cell.setAttribute('tabindex', '-1');
657668
cell.setAttribute('role', tagName === 'td' ? 'gridcell' : 'columnheader');
669+
cell.addEventListener('mouseenter', (event) => {
670+
this._showTooltip(event);
671+
});
672+
cell.addEventListener('mouseleave', () => {
673+
this._hideTooltip();
674+
});
675+
cell.addEventListener('mousedown', () => {
676+
this._hideTooltip();
677+
});
658678

659679
const slot = document.createElement('slot');
660680
slot.setAttribute('name', slotName);
@@ -979,6 +999,24 @@ class Grid extends ElementMixin(
979999
};
9801000
}
9811001

1002+
/**
1003+
* @param {Event} event
1004+
* @protected
1005+
*/
1006+
_showTooltip(event) {
1007+
// Check if there is a slotted vaadin-tooltip element.
1008+
if (this._tooltipController.node && this._tooltipController.node.isConnected) {
1009+
this._tooltipController.setTarget(event.target);
1010+
this._tooltipController.setContext(this.getEventContext(event));
1011+
this._tooltipController.setOpened(true);
1012+
}
1013+
}
1014+
1015+
/** @protected */
1016+
_hideTooltip() {
1017+
this._tooltipController.setOpened(false);
1018+
}
1019+
9821020
/**
9831021
* Requests an update for the content of cells.
9841022
*

0 commit comments

Comments
 (0)