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

fix: update keymapper when refreshing checkbox item (#6935) (CP: 24.6) #6945

Merged
merged 2 commits into from
Dec 13, 2024
Merged
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 @@ -389,10 +389,10 @@ public void onDiscard(DataChangeEvent<T> dataChangeEvent) {
}

private void handleDataChange(DataChangeEvent<T> dataChangeEvent) {
if (dataChangeEvent instanceof DataChangeEvent.DataRefreshEvent) {
T otherItem = ((DataChangeEvent.DataRefreshEvent<T>) dataChangeEvent)
.getItem();
if (dataChangeEvent instanceof DataChangeEvent.DataRefreshEvent<T> dataRefreshEvent) {
T otherItem = dataRefreshEvent.getItem();
Object otherItemId = getItemId(otherItem);
keyMapper.refresh(otherItem);
getCheckboxItems().filter(
item -> Objects.equals(getItemId(item.item), otherItemId))
.findFirst().ifPresent(this::updateCheckbox);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,33 @@ public void preserveAllSelectionOnDataChange_noExtraChangeEventsFired() {
Assert.assertTrue(events.isEmpty());
}

@Test
public void refreshItem_selectFromClient_valueContainsUpdatedItem() {
CheckboxGroup<CustomItem> group = new CheckboxGroup<>();
CheckboxGroupListDataView<CustomItem> dataView = group.setItems(
new CustomItem(1L, "foo"), new CustomItem(2L, "bar"),
new CustomItem(3L, "baz"));
dataView.setIdentifierProvider(CustomItem::getId);

CustomItem updatedItem = new CustomItem(2L, "updated");
dataView.refreshItem(updatedItem);

AtomicReference<Set<CustomItem>> selectedItems = new AtomicReference<>();
group.addValueChangeListener(e -> selectedItems.set(e.getValue()));

// Simulate selecting an item from the client side via key
String itemKey = group.getChildren().skip(1).findFirst().orElseThrow()
.getElement().getProperty("value");
JsonArray selection = Json.createArray();
selection.set(0, itemKey);
group.getElement().setPropertyJson("value", selection);

Assert.assertEquals("updated",
selectedItems.get().stream().findFirst().orElseThrow().name);
Assert.assertEquals("updated",
group.getValue().stream().findFirst().orElseThrow().name);
}

/**
* Used in the tests {@link #singleDataRefreshEvent()} and
* {@link #allDataRefreshEvent()}
Expand Down