Fire enter event on another component

I have a ComboBox and a button next to each other. The ComboBox searches our backend for results when pressing enter, the button just says search.
When I click the button I would like to trigger the ComboBox like if the enter key has been pressed when focused so it performs a search.

Is this possible?

Should be pretty easy yeah: Button’s clickhandler does the backend call and updates the CB’s contents with setItems when done. Then you’ll probably also want to focus() and setOpened(true). If you don’t expect there to be any items in the ComboBox before that, you might want to setAutoOpen(false) initially to avoid showing a tiny, empty dropdown.

Oh and setAllowCustomValue(true) so that you can type stuff into it that doesn’t match an item

We are using a custom CB field with an AbstractBackEndDataProvider for this and would like it to work without the button in some views and with the button in others. So it should be possible to only write text in the CB and press enter to perform the search (this is how we do it today)

right, Enter should do that too. Ok then that’s easy e.g. through the CustomValueSetListener:

cb.setAllowCustomValue(true);
cb.addCustomValueSetListener(e->{
    loadPeople();
});
        
Button btn = new Button(VaadinIcon.SEARCH.create(), e->{
    loadPeople();
});

where loadPeople is

private void loadPeople() {
    cb.setItems(Person.getPeople());
    cb.setValue(null);
    cb.setOpened(true);
    }```

(apparently setAutoOpen(false) prevents that listener from working as desired here, so I’d suggest to skip that part – the only downside is a barely noticable flicker of the empty dropdown)

Thanks :slightly_smiling_face: