Skip to content

fix(InputSelectize): Fix InputSelectize set method to clear selections #2024

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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

* Fix missing session when trying to display an error duing bookmarking. (#1984)

* Fixed `set()` method of `InputSelectize` controller so it clears existing selections before applying new values. (#2024)


## [1.4.0] - 2025-04-08

Expand Down
29 changes: 26 additions & 3 deletions shiny/playwright/controller/_input_controls.py
Original file line number Diff line number Diff line change
Expand Up @@ -1197,10 +1197,33 @@ def set(
if isinstance(selected, str):
selected = [selected]
self._loc_events.click()

currently_selected: list[str] = []
selected_items = self._loc_events.locator("> .item")
for i in range(selected_items.count()):
item = selected_items.nth(i)
data_value = item.get_attribute("data-value")
if data_value:
currently_selected.append(data_value)

current_set = set(currently_selected)
desired_set = set(selected)

for current_value in currently_selected:
if current_value not in desired_set:
item_to_remove = self._loc_events.locator(
f"> .item[data-value='{current_value}']"
)
if item_to_remove.count() > 0:
item_to_remove.click()
self.page.keyboard.press("Delete")

for value in selected:
self._loc_selectize.locator(f"[data-value='{value}']").click(
timeout=timeout
)
if value not in current_set:
self._loc_selectize.locator(f"[data-value='{value}']").click(
timeout=timeout
)

self._loc_events.press("Escape")

def expect_choices(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from shiny import App, render, ui
from shiny.session import Inputs, Outputs, Session

app_ui = ui.page_fluid(
ui.input_selectize(
"test_selectize",
"Select",
["Choice 1", "Choice 2", "Choice 3", "Choice 4"],
multiple=True,
),
ui.output_text("test_selectize_output"),
)


def server(input: Inputs, output: Outputs, session: Session) -> None:
@render.text
def test_selectize_output():
return f"Selected: {', '.join(input.test_selectize())}"


app = App(app_ui, server)
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from playwright.sync_api import Page

from shiny.playwright import controller
from shiny.pytest import create_app_fixture
from shiny.run import ShinyAppProc

app = create_app_fixture("app_selectize.py")


def test_inputselectize(page: Page, app: ShinyAppProc):
page.goto(app.url)

input_selectize = controller.InputSelectize(page, "test_selectize")
output_text = controller.OutputText(page, "test_selectize_output")

input_selectize.set(["Choice 1", "Choice 2", "Choice 3"])
output_text.expect_value("Selected: Choice 1, Choice 2, Choice 3")
input_selectize.set(["Choice 2", "Choice 3"])
output_text.expect_value("Selected: Choice 2, Choice 3")
input_selectize.set(["Choice 3"])
output_text.expect_value("Selected: Choice 3")
input_selectize.set([])
output_text.expect_value("Selected: ")
Loading