Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MenuItem DisableOnClick #1472

Closed
zanonmark opened this issue May 11, 2020 · 3 comments · Fixed by #6815
Closed

MenuItem DisableOnClick #1472

zanonmark opened this issue May 11, 2020 · 3 comments · Fixed by #6815
Assignees
Labels

Comments

@zanonmark
Copy link

Description of the bug

It could be useful to have .setDisableOnClick(boolean) on MenuItems too, as the code associated with them could be heavy to run.

@pleku pleku transferred this issue from vaadin/flow May 12, 2020
@pleku
Copy link
Contributor

pleku commented May 12, 2020

Thanks for the feature request, I've transferred it to the context menu Java integration repository.

@vaadin-bot vaadin-bot transferred this issue from vaadin/vaadin-context-menu-flow Oct 6, 2020
@vaadin-bot vaadin-bot transferred this issue from vaadin/vaadin-context-menu May 19, 2021
@vaadin-bot vaadin-bot transferred this issue from vaadin/web-components May 21, 2021
@vaadin-bot vaadin-bot added enhancement New feature or request needs design Design is needed vaadin-context-menu labels May 21, 2021
@jcgueriaud1
Copy link
Contributor

As a workaround to disable the menu item on click, you can do this:

MenuBar menuBar = new MenuBar();
Text selected = new Text("");
ComponentEventListener<ClickEvent<MenuItem>> listener = e -> {
    UI ui = getUI().get();
    selected
            .setText("Starting a task");
    new Thread(() -> {
        try {
            // simulate a long action
            Thread.sleep(2_000);
        } catch (InterruptedException ex) {
            throw new RuntimeException(ex);
        }
        ui.access(() -> {
            selected.setText("Finished a task");
            getElement().executeJs(
                    "$0.disabled = false;", e.getSource());
            ui.push();
        });

    }).start();
};
Div message = new Div(new Text("Clicked item: "), selected);

MenuItem menuItem = menuBar.addItem("View", listener);

getElement().executeJs(
        "$0.addEventListener('click', b => { $0.disabled = true;});", menuItem);

menuBar.addItem("Edit", listener);

getContent().add(menuBar,
        message);

@sissbruecker
Copy link
Contributor

Some notes from investigating this:

  • The logic can be adapted from Button
  • As with Button, the component should register a JS click handler to disable the element immediately on the client in case the roundtrip takes longer. While the menu usually closes on its own when selecting an item, there is an option to keep it open, in which case getting immediate feedback on the item being disabled is useful.
  • Disabling the menu item through JS resulted in the menu not closing anymore after clicking the item. Adding a timeout for disabling the item worked.
  • There might be an opportunity to extract the disable on click behavior into a controller that can be applied to Button, MenuItem and maybe other components in the future.

@ugur-vaadin ugur-vaadin self-assigned this Nov 11, 2024
@rolfsmeds rolfsmeds moved this to December 2024 (24.6) in Roadmap Nov 13, 2024
@rolfsmeds rolfsmeds removed the needs design Design is needed label Nov 13, 2024
@rolfsmeds rolfsmeds changed the title Please provide setDisableOnClick on MenuItems too MenuItem DisableOnClick Dec 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: December 2024 (24.6) - Released
Development

Successfully merging a pull request may close this issue.

7 participants