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

[combo-box] Selected value label is not updated when refreshing data provider

Open sissbruecker opened this issue 4 years ago • 4 comments

Description

When using a combo box with an item label generator, changing the item's data and refreshing the data provider does not update the displayed value in the combo box input.

This also affects the multi-select combo box, here the chip's labels are not updated.

It seems we want to run ComboBoxBase.refreshValue when the data provider is refreshed.

Expected outcome

For a combo box, updating item data, and refreshing the data provider should update the label of the selected value displayed in the combo box input.

For a multi-select combo box, updating item data, and refreshing the data provider should update the labels of the selected items displayed in the visible chips.

Minimal reproducible example

        ComboBox<TestBean> comboBox = new ComboBox<>();
        List<TestBean> items = IntStream.range(0, 100)
                .mapToObj(i -> new TestBean("Item " + (i + 1))).collect(Collectors.toList());
        comboBox.setItems(items);
        comboBox.setItemLabelGenerator(TestBean::getName);

        NativeButton changeItemData = new NativeButton("Change item data", e -> {
            items.forEach(item -> item.setName("Updated " + item.getName()));
            comboBox.getDataProvider().refreshAll();
        });
    private static class TestBean {
        private String name;

        public TestBean(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

Steps to reproduce

  1. Add the snippet above to a view and run it
  2. Select Item 1
  3. ComboBox value input displays Item 1
  4. Click button to change item data
  5. ComboBox value input still displays Item 1

Environment

Vaadin version(s): 23

Browsers

Issue is not browser related

sissbruecker avatar May 30 '22 09:05 sissbruecker

There is a somewhat related issue: #1049, although the way to update items there is different.

web-padawan avatar May 31 '22 10:05 web-padawan

Another issue for getDataProvider().refreshAll(): #1616

web-padawan avatar May 31 '22 10:05 web-padawan

Same here, using Vaadin 23.3.0. Context: Trying to trigger re-localization in method localeChange. I have Grid, ComboBox and MultiSelectComboBox.

Calling X.getDataProvider().refreshAll() on each - Grid values are updating as expected with new Locale. As for ComboBox and MultiSelectComboBox - the items are updating in drop-down, but chips of selected items stays untouched.

danielkolesnik avatar Dec 28 '22 20:12 danielkolesnik

[!WARNING] Automated reproduction — produced by the Claude Code repro skill. Needs human verification. The steps, verdict, and root-cause pointer below were generated automatically and must be confirmed by a human before being treated as authoritative.

Verified while triaging #1616 (now closed as a duplicate of this issue). Confirmed on current main (25.3-SNAPSHOT) — with one addition to the original report: the targeted refreshItem(item) leaves the caption equally stale, not only refreshAll().

  • Branch: repro/1616 — runnable reproduction (route /repro-1616)
  • Theme / Browser: Lumo / Chromium

input caption "a" while the overlay item and server value read "aab"

Observed behavior

ComboBox with one mutable item (identity by id via equals/hashCode), setItemLabelGenerator(it -> it.name), value pre-selected. Buttons mutate the item's name and fire a refresh:

Step Input caption Overlay item Server-side value label
initial a — a
mutate → refreshAll() a (stale) aa aa
mutate → refreshItem(item) a (stale) aab aab

The overlay always shows the fresh label; the input caption never updates. No console errors, no server exceptions.

Root cause (confirms the description)

The input caption comes from the selectedItem property, which is only rewritten by ComboBoxBase#refreshValue() — exactly as this issue's description suspected. refreshValue() runs on setValue(...) and setItemLabelGenerator(...), but on data-provider events the listener routes to dataCommunicator.refresh(item) / reset(), and the reset-triggered refreshValue() is explicitly limited to MultiSelectComboBox (and per the reports here, the chips still stale-out too):

https://github.com/vaadin/flow-components/blob/29d9da5f76b2f8cd8b773e7e42b2601f26ccb1d5/vaadin-combo-box-flow-parent/vaadin-combo-box-flow/src/main/java/com/vaadin/flow/component/combobox/ComboBoxDataController.java#L510-L526

https://github.com/vaadin/flow-components/blob/29d9da5f76b2f8cd8b773e7e42b2601f26ccb1d5/vaadin-combo-box-flow-parent/vaadin-combo-box-flow/src/main/java/com/vaadin/flow/component/combobox/ComboBoxDataController.java#L634-L647

A fix shape: after a DataRefreshEvent matching the current value (or after any reset()), call comboBox.refreshValue() for single-select ComboBox too.

Notes

  • Workaround: re-set the value (setValue(null) + setValue(item)) or re-apply the label generator — both paths call refreshValue().
  • The reproduction was built for #1616, hence the branch name; it covers the single-select case only.

web-padawan avatar Jul 11 '26 07:07 web-padawan