Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
<artifactId>vaadin-flow-components-base</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-flow-components-test-util</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-renderer-flow</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.io.Serializable;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

Expand All @@ -26,8 +27,11 @@
import com.vaadin.flow.component.HasComponents;
import com.vaadin.flow.component.HasEnabled;
import com.vaadin.flow.component.HasText;
import com.vaadin.flow.component.SignalPropertySupport;
import com.vaadin.flow.component.Tag;
import com.vaadin.flow.component.shared.internal.DisableOnClickController;
import com.vaadin.flow.function.SerializableConsumer;
import com.vaadin.flow.signals.Signal;

/**
* Base class for item component used inside {@link ContextMenu}s.
Expand All @@ -54,6 +58,9 @@ public abstract class MenuItemBase<C extends ContextMenuBase<C, I, S>, I extends

private boolean checkable = false;

private SignalPropertySupport<Boolean> checkedSupport;
private SerializableConsumer<Boolean> boundCheckedWriteCallback;

private Set<String> themeNames = new LinkedHashSet<>();

private final DisableOnClickController<MenuItemBase<C, I, S>> disableOnClickController = new DisableOnClickController<>(
Expand All @@ -69,7 +76,11 @@ public MenuItemBase(C contextMenu) {
this.contextMenu = contextMenu;
getElement().addEventListener("click", e -> {
if (checkable) {
setChecked(!isChecked());
if (boundCheckedWriteCallback != null) {
boundCheckedWriteCallback.accept(!isChecked());
} else {
setChecked(!isChecked());
}
}
});

Expand Down Expand Up @@ -166,11 +177,42 @@ public void setChecked(boolean checked) {
+ "Use setCheckable() to make the item checkable first.");
}

getElement().setProperty("_checked", checked);
getCheckedSupport().set(checked);
}

executeJsWhenAttached(
"window.Vaadin.Flow.contextMenuConnector.setChecked($0, $1)",
getElement(), checked);
/**
* Binds the checked state to the given signal. The binding is two-way:
* signal changes push to the DOM property, and client-side click events
* invoke the write callback.
* <p>
* While a signal is bound, any attempt to set the checked state manually
* throws {@link com.vaadin.flow.signals.BindingActiveException}.
*
* @param signal
* the signal to bind, not {@code null}
* @param writeCallback
* the callback to propagate value changes back, or {@code null}
* for one-way binding
* @since 25.1
*/
public void bindChecked(Signal<Boolean> signal,
SerializableConsumer<Boolean> writeCallback) {
Objects.requireNonNull(signal, "Signal cannot be null");
boundCheckedWriteCallback = writeCallback;
getCheckedSupport()
.bind(signal.map(v -> v == null ? Boolean.FALSE : v));
}

private SignalPropertySupport<Boolean> getCheckedSupport() {
if (checkedSupport == null) {
checkedSupport = SignalPropertySupport.create(this, checked -> {
getElement().setProperty("_checked", checked);
executeJsWhenAttached(
"window.Vaadin.Flow.contextMenuConnector.setChecked($0, $1)",
getElement(), checked);
});
}
return checkedSupport;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* Copyright 2000-2026 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.flow.component.contextmenu;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

import com.vaadin.flow.component.UI;
import com.vaadin.flow.function.DeploymentConfiguration;
import com.vaadin.flow.server.VaadinSession;
import com.vaadin.flow.signals.BindingActiveException;
import com.vaadin.flow.signals.local.ValueSignal;
import com.vaadin.tests.AbstractSignalsUnitTest;

public class MenuItemSignalTest extends AbstractSignalsUnitTest {

private ContextMenu contextMenu;
private MenuItem item;
private ValueSignal<Boolean> signal;

@Before
public void setup() {
// ContextMenu's MenuItemsArrayGenerator triggers chunk loading on
// attach, which requires a DeploymentConfiguration on the session.
VaadinSession session = VaadinSession.getCurrent();
if (session != null && session.getConfiguration() == null) {
DeploymentConfiguration config = Mockito
.mock(DeploymentConfiguration.class);
Mockito.when(session.getService().getDeploymentConfiguration())
.thenReturn(config);
}
contextMenu = new ContextMenu();
item = contextMenu.addItem("");
item.setCheckable(true);
signal = new ValueSignal<>(false);
}

@After
public void tearDown() {
if (contextMenu != null && contextMenu.isAttached()) {
contextMenu.removeFromParent();
}
}

@Test
public void bindChecked_signalBound_propertySync() {
item.bindChecked(signal, signal::set);
UI.getCurrent().add(contextMenu);
flushBeforeClientResponse();

Assert.assertFalse(item.isChecked());

signal.set(true);
Assert.assertTrue(item.isChecked());

signal.set(false);
Assert.assertFalse(item.isChecked());
}

@Test
public void bindChecked_notAttached_noEffect() {
item.bindChecked(signal, signal::set);

boolean initial = item.isChecked();
signal.set(true);
Assert.assertEquals(initial, item.isChecked());
}

@Test
public void bindChecked_detachAndReattach() {
item.bindChecked(signal, signal::set);
UI.getCurrent().add(contextMenu);
flushBeforeClientResponse();

signal.set(true);
Assert.assertTrue(item.isChecked());

contextMenu.removeFromParent();
signal.set(false);
Assert.assertTrue(item.isChecked());

UI.getCurrent().add(contextMenu);
flushBeforeClientResponse();
Assert.assertFalse(item.isChecked());
}

@Test(expected = BindingActiveException.class)
public void bindChecked_setWhileBound_throws() {
item.bindChecked(signal, signal::set);
UI.getCurrent().add(contextMenu);

item.setChecked(true);
}

@Test(expected = BindingActiveException.class)
public void bindChecked_doubleBind_throws() {
item.bindChecked(signal, signal::set);
var other = new ValueSignal<>(true);
item.bindChecked(other, other::set);
}

@Test(expected = NullPointerException.class)
public void bindChecked_nullSignal_throwsNPE() {
item.bindChecked(null, null);
}

private void flushBeforeClientResponse() {
UI.getCurrent().getInternals().getStateTree()
.runExecutionsBeforeClientResponse();
}
}