flow-components icon indicating copy to clipboard operation
flow-components copied to clipboard

ComboBox is missing an addOpenedChangeListener

Open sujoykd opened this issue 2 years ago • 2 comments
trafficstars

Describe your motivation

The ComboBox component is missing an addOpenedChangeListener.

Describe the solution you'd like

An addOpenedChangeListener in ComboBox similar to how it is present in DatePicker

Describe alternatives you've considered

comboBox.getElement().addPropertyChangeListener("opened", "opened-changed", event -> {
  // do something
});

Additional context

No response

sujoykd avatar Sep 04 '23 14:09 sujoykd

This is a low hanging fruit as the component fires the low level event already, you can thus use Element API to observe it

    combobox.getElement().addEventListener("opened-changed", e -> {
        Notification.show(e.getEventData().toJson());
    }).addEventData("event.detail");

TatuLund avatar Feb 08 '24 10:02 TatuLund

Excellent work @TatuLund , thank you. I've built on your example code a bit:

public class MyComboBox<T> extends ComboBox<T> {
	/**
	 * @param listener invoked when the dropdown overlay is opened or closed. Receives true if the dropdown is opened, false if it is closed.
	 * @return the registration.
	 */
	@NonNull
	public Registration addOpenedChangeListener(@NonNull SerializableConsumer<Boolean> listener) {
		return getElement().addEventListener("opened-changed", e -> {
			final boolean opened = e.getEventData().getObject("event.detail").getBoolean("value");
			listener.accept(opened);
		}).addEventData("event.detail");
	}
}

mvysny avatar Feb 08 '24 14:02 mvysny