Class SideNavItem

java.lang.Object
com.vaadin.flow.component.Component
com.vaadin.flow.component.sidenav.SideNavItem
All Implemented Interfaces:
AttachNotifier, DetachNotifier, HasElement, HasStyle, HasPrefix, HasSuffix, Serializable

@Tag("vaadin-side-nav-item") @NpmPackage(value="@vaadin/side-nav", version="24.1.6") @JsModule("@vaadin/side-nav/src/vaadin-side-nav-item.js") public class SideNavItem extends Component implements HasPrefix, HasSuffix
A menu item for the SideNav component.

Besides the target path it can contain a label, prefix and suffix component, like icons or badges. You can create a hierarchical navigation structure by adding other SideNavItem instances to this SideNavItem instance via addItem(SideNavItem...).

Author:
Vaadin Ltd
See Also:
  • Constructor Details

    • SideNavItem

      public SideNavItem(String label)
      Creates a menu item which does not link to any view but only shows the given label.
      Parameters:
      label - the label for the item
    • SideNavItem

      public SideNavItem(String label, String path)
      Creates a new menu item using the given label that links to the given path.
      Parameters:
      label - the label for the item
      path - the path to link to
    • SideNavItem

      public SideNavItem(String label, Class<? extends Component> view)
      Creates a new menu item using the given label that links to the given view.
      Parameters:
      label - the label for the item
      view - the view to link to
    • SideNavItem

      public SideNavItem(String label, String path, Component prefixComponent)
      Creates a new menu item using the given label and prefix component (like an icon) that links to the given path.
      Parameters:
      label - the label for the item
      path - the path to link to
      prefixComponent - the prefix component for the item (usually an icon)
    • SideNavItem

      public SideNavItem(String label, Class<? extends Component> view, Component prefixComponent)
      Creates a new menu item using the given label and prefix component (like an icon) that links to the given view.
      Parameters:
      label - the label for the item
      view - the view to link to
      prefixComponent - the prefixComponent for the item (usually an icon)
  • Method Details

    • setupSideNavItem

      protected void setupSideNavItem(SideNavItem item)
      Implement this method to set up/modify the SideNavItem right before it's added to the list of the navigation items.
      Parameters:
      item - Item to be set up
    • getLabel

      public String getLabel()
      Gets the label of this menu item.
      Returns:
      the label or null if no label has been set
    • setLabel

      public void setLabel(String label)
      Set a textual label for the item.

      The label is also available for screen reader users.

      Parameters:
      label - the label text to set; or null to remove the label
    • setPath

      public void setPath(String path)
      Sets the path in a form or a URL string this navigation item links to. Note that there is also an alternative way of how to set the link path via setPath(Class).
      Parameters:
      path - The path to link to. Set to null to disable navigation for this item.
      See Also:
    • setPath

      public void setPath(Class<? extends Component> view)
      Sets the view this item links to.

      Note: Vaadin Router will be used to determine the URL path of the view and this URL will be then set to this navigation item using setPath(String)

      Parameters:
      view - The view to link to. The view should be annotated with the Route annotation. Set to null to disable navigation for this item.
      See Also:
    • getPath

      public String getPath()
      Gets the path this navigation item links to.
      Returns:
      path this navigation item links to
    • setExpanded

      public void setExpanded(boolean expanded)
      Sets the expanded status of the item.
      Parameters:
      expanded - true to expand the item, false to collapse it
    • isExpanded

      @Synchronize(property="expanded", value="expanded-changed") public boolean isExpanded()
      Returns whether the side navigation menu item is expanded or collapsed.
      Returns:
      true if the item is expanded, false if collapsed
    • onAttach

      protected void onAttach(AttachEvent attachEvent)
      Description copied from class: Component
      Called when the component is attached to a UI.

      The default implementation does nothing.

      This method is invoked before the AttachEvent is fired for the component.

      Overrides:
      onAttach in class Component
      Parameters:
      attachEvent - the attach event
    • getFeatureFlags

      protected FeatureFlags getFeatureFlags()
      Gets the feature flags for the current UI.

      Extracted with protected visibility to support mocking

      Returns:
      the current set of feature flags
    • addItem

      public void addItem(SideNavItem... items)
      Adds navigation menu item(s) to the menu.
      Parameters:
      items - the navigation menu item(s) to add
    • addItemAsFirst

      public void addItemAsFirst(SideNavItem item)
      Adds the given navigation item as the first child of this navigation item.
      Parameters:
      item - the item to add, value must not be null
    • addItemAtIndex

      public void addItemAtIndex(int index, SideNavItem item)
      Adds the given item as child of this navigation item at the specific index.
      Parameters:
      index - the index, where the item will be added. The index must be non-negative and may not exceed the children count
      item - the item to add, value must not be null
    • getItems

      public List<SideNavItem> getItems()
      Gets the items added to this navigation item (the children of this component that are instances of SideNavItem). This doesn't include the items added to the children of this navigation item.
      Returns:
      the child SideNavItem instances in this navigation menu
      See Also:
    • remove

      public void remove(SideNavItem... items)
      Removes the menu item(s) from the menu.

      If the given menu item is not a child of this menu, does nothing.

      Parameters:
      items - the menu item(s) to remove
    • removeAll

      public void removeAll()
      Removes all navigation menu items from this item.