Skip to content

Automatically enable "clear_button" selectize plugin when choices include "" #1384

@gadenbuie

Description

@gadenbuie

In the app below, choices includes a "" value. This makes it possible for the user to select "nothing". The problem is that, from a usability perspective, it's hard to know how to enter the "nothing" state.

You can try this on shinylive:

  1. Select a choice from the input.
  2. Without using the clear button, attempt to clear the current selection.
  3. The correct actions are:
    1. Move focus to the selectize input
    2. Press Backspace to delete the current input
    3. Do not press Enter, instead Tab or move focus away from the input.

We could instead detect the presence of "" in choices and turn on the "clear_button" plugin by adding it to the options["plugins"] array.

from shiny.express import input, render, ui


ui.input_selectize(
    "filter",
    "Filter by",
    choices={
        "": "Select filter",
        "first": "First Choice",
        "second": "Second Choice",
    },
    options={
        "plugins": ['clear_button']
    }
)


@render.text
def txt():
    if input.filter():
        return f"Data is filtered by your {input.filter()} choice."
    else:
        return "The data is not filtered."

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions