Skip to content

Commit c717898

Browse files
committed
Improve selectize input selection logic
Refactors the selection logic to only remove items not in the desired set and only add missing items, improving efficiency and correctness when updating selected values in InputSelectize.
1 parent a3f32d2 commit c717898

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

shiny/playwright/controller/_input_controls.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,16 +1198,32 @@ def set(
11981198
selected = [selected]
11991199
self._loc_events.click()
12001200

1201-
while True:
1202-
remove_button = self._loc_events.locator("> .item > .remove").first
1203-
if remove_button.count() == 0:
1204-
break
1205-
remove_button.click()
1201+
currently_selected: list[str] = []
1202+
selected_items = self._loc_events.locator("> .item")
1203+
for i in range(selected_items.count()):
1204+
item = selected_items.nth(i)
1205+
data_value = item.get_attribute("data-value")
1206+
if data_value:
1207+
currently_selected.append(data_value)
1208+
1209+
current_set = set(currently_selected)
1210+
desired_set = set(selected)
1211+
1212+
for current_value in currently_selected:
1213+
if current_value not in desired_set:
1214+
item_to_remove = self._loc_events.locator(
1215+
f"> .item[data-value='{current_value}']"
1216+
)
1217+
if item_to_remove.count() > 0:
1218+
item_to_remove.click()
1219+
self.page.keyboard.press("Delete")
12061220

12071221
for value in selected:
1208-
self._loc_selectize.locator(f"[data-value='{value}']").click(
1209-
timeout=timeout
1210-
)
1222+
if value not in current_set:
1223+
self._loc_selectize.locator(f"[data-value='{value}']").click(
1224+
timeout=timeout
1225+
)
1226+
12111227
self._loc_events.press("Escape")
12121228

12131229
def expect_choices(

0 commit comments

Comments
 (0)