|
38 | 38 | import com.vaadin.flow.component.contextmenu.SubMenu; |
39 | 39 | import com.vaadin.flow.component.dependency.JsModule; |
40 | 40 | import com.vaadin.flow.component.dependency.NpmPackage; |
| 41 | +import com.vaadin.flow.dom.Element; |
41 | 42 | import com.vaadin.flow.function.SerializableConsumer; |
42 | 43 | import com.vaadin.flow.internal.JsonSerializer; |
43 | 44 |
|
|
55 | 56 | @JsModule("@vaadin/polymer-legacy-adapter/style-modules.js") |
56 | 57 | @JsModule("./menubarConnector.js") |
57 | 58 | @JsModule("@vaadin/menu-bar/src/vaadin-menu-bar.js") |
| 59 | +@JsModule("@vaadin/tooltip/src/vaadin-tooltip.js") |
58 | 60 | @NpmPackage(value = "@vaadin/menu-bar", version = "23.3.0-alpha1") |
59 | 61 | @NpmPackage(value = "@vaadin/vaadin-menu-bar", version = "23.3.0-alpha1") |
| 62 | +@NpmPackage(value = "@vaadin/tooltip", version = "23.3.0-alpha1") |
60 | 63 | public class MenuBar extends Component |
61 | 64 | implements HasMenuItems, HasSize, HasStyle, HasTheme, HasEnabled { |
62 | 65 |
|
@@ -176,6 +179,113 @@ public MenuItem addItem(Component component, |
176 | 179 | return menuManager.addItem(component, clickListener); |
177 | 180 | } |
178 | 181 |
|
| 182 | + /** |
| 183 | + * Creates a new {@link MenuItem} component with the provided text content |
| 184 | + * and the tooltip text and adds it to the root level of this menu bar. |
| 185 | + * <p> |
| 186 | + * The added {@link MenuItem} component is placed inside a button in the |
| 187 | + * menu bar. If this button overflows the menu bar horizontally, the |
| 188 | + * {@link MenuItem} is moved out of the button, into a context menu openable |
| 189 | + * via an overflow button at the end of the button row. |
| 190 | + * <p> |
| 191 | + * To add content to the sub menu opened by clicking the root level item, |
| 192 | + * use {@link MenuItem#getSubMenu()}. |
| 193 | + * |
| 194 | + * @param text |
| 195 | + * the text content for the new item |
| 196 | + * @param tooltipText |
| 197 | + * the tooltip text for the new item |
| 198 | + * @return the added {@link MenuItem} component |
| 199 | + */ |
| 200 | + public MenuItem addItem(String text, String tooltipText) { |
| 201 | + var item = addItem(text); |
| 202 | + setTooltip(item, tooltipText); |
| 203 | + return item; |
| 204 | + } |
| 205 | + |
| 206 | + /** |
| 207 | + * Creates a new {@link MenuItem} component with the provided tooltip text |
| 208 | + * and adds it to the root level of this menu bar. The provided component is |
| 209 | + * added into the created {@link MenuItem}. |
| 210 | + * <p> |
| 211 | + * The added {@link MenuItem} component is placed inside a button in the |
| 212 | + * menu bar. If this button overflows the menu bar horizontally, the |
| 213 | + * {@link MenuItem} is moved out of the button, into a context menu openable |
| 214 | + * via an overflow button at the end of the button row. |
| 215 | + * <p> |
| 216 | + * To add content to the sub menu opened by clicking the root level item, |
| 217 | + * use {@link MenuItem#getSubMenu()}. |
| 218 | + * |
| 219 | + * @param component |
| 220 | + * the component to add inside new item |
| 221 | + * @param tooltipText |
| 222 | + * the tooltip text for the new item |
| 223 | + * @return the added {@link MenuItem} component |
| 224 | + */ |
| 225 | + public MenuItem addItem(Component component, String tooltipText) { |
| 226 | + var item = addItem(component); |
| 227 | + setTooltip(item, tooltipText); |
| 228 | + return item; |
| 229 | + } |
| 230 | + |
| 231 | + /** |
| 232 | + * Creates a new {@link MenuItem} component with the provided text content |
| 233 | + * and the tooltip text and click listener and adds it to the root level of |
| 234 | + * this menu bar. |
| 235 | + * <p> |
| 236 | + * The added {@link MenuItem} component is placed inside a button in the |
| 237 | + * menu bar. If this button overflows the menu bar horizontally, the |
| 238 | + * {@link MenuItem} is moved out of the button, into a context menu openable |
| 239 | + * via an overflow button at the end of the button row. |
| 240 | + * <p> |
| 241 | + * To add content to the sub menu opened by clicking the root level item, |
| 242 | + * use {@link MenuItem#getSubMenu()}. |
| 243 | + * |
| 244 | + * @param text |
| 245 | + * the text content for the new item |
| 246 | + * @param tooltipText |
| 247 | + * the tooltip text for the new item |
| 248 | + * @param clickListener |
| 249 | + * the handler for clicking the new item, can be {@code null} to |
| 250 | + * not add listener |
| 251 | + * @return the added {@link MenuItem} component |
| 252 | + */ |
| 253 | + public MenuItem addItem(String text, String tooltipText, |
| 254 | + ComponentEventListener<ClickEvent<MenuItem>> clickListener) { |
| 255 | + var item = addItem(text, clickListener); |
| 256 | + setTooltip(item, tooltipText); |
| 257 | + return item; |
| 258 | + } |
| 259 | + |
| 260 | + /** |
| 261 | + * Creates a new {@link MenuItem} component with the provided click listener |
| 262 | + * and the tooltip text and adds it to the root level of this menu bar. The |
| 263 | + * provided component is added into the created {@link MenuItem}. |
| 264 | + * <p> |
| 265 | + * The added {@link MenuItem} component is placed inside a button in the |
| 266 | + * menu bar. If this button overflows the menu bar horizontally, the |
| 267 | + * {@link MenuItem} is moved out of the button, into a context menu openable |
| 268 | + * via an overflow button at the end of the button row. |
| 269 | + * <p> |
| 270 | + * To add content to the sub menu opened by clicking the root level item, |
| 271 | + * use {@link MenuItem#getSubMenu()}. |
| 272 | + * |
| 273 | + * @param component |
| 274 | + * the component to add inside the added menu item |
| 275 | + * @param tooltipText |
| 276 | + * the tooltip text for the new item |
| 277 | + * @param clickListener |
| 278 | + * the handler for clicking the new item, can be {@code null} to |
| 279 | + * not add listener |
| 280 | + * @return the added {@link MenuItem} component |
| 281 | + */ |
| 282 | + public MenuItem addItem(Component component, String tooltipText, |
| 283 | + ComponentEventListener<ClickEvent<MenuItem>> clickListener) { |
| 284 | + var item = addItem(component, clickListener); |
| 285 | + setTooltip(item, tooltipText); |
| 286 | + return item; |
| 287 | + } |
| 288 | + |
179 | 289 | /** |
180 | 290 | * Gets the {@link MenuItem} components added to the root level of the menu |
181 | 291 | * bar. |
@@ -393,4 +503,28 @@ public MenuBarI18n setMoreOptions(String moreOptions) { |
393 | 503 | return this; |
394 | 504 | } |
395 | 505 | } |
| 506 | + |
| 507 | + /** |
| 508 | + * Sets the tooltip property for the given menu item. A vaadin-tooltip |
| 509 | + * element with a custom generator is created and added inside the menu bar |
| 510 | + * in case it doesn't exist. |
| 511 | + */ |
| 512 | + private void setTooltip(MenuItem item, String tooltipText) { |
| 513 | + if (!getElement().getChildren().anyMatch( |
| 514 | + child -> "tooltip".equals(child.getAttribute("slot")))) { |
| 515 | + // No <vaadin-tooltip> yet added, add one |
| 516 | + var tooltipElement = new Element("vaadin-tooltip"); |
| 517 | + tooltipElement.setAttribute("slot", "tooltip"); |
| 518 | + |
| 519 | + tooltipElement.addAttachListener(e -> { |
| 520 | + // Assigns a generator that reads the tooltip property of the |
| 521 | + // item component |
| 522 | + tooltipElement.executeJs( |
| 523 | + "this.textGenerator = ({item}) => { return (item && item.component) ? item.component.tooltip : ''; }"); |
| 524 | + }); |
| 525 | + getElement().appendChild(tooltipElement); |
| 526 | + } |
| 527 | + |
| 528 | + item.getElement().setProperty("tooltip", tooltipText); |
| 529 | + } |
396 | 530 | } |
0 commit comments