Skip to content

Commit c7337ad

Browse files
committed
Fix searching by dropdown value
1 parent fbd13f7 commit c7337ad

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

components/dash-core-components/src/fragments/Dropdown.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function DropdownLabel(
4242
);
4343
}
4444
const displayLabel = `${props.label ?? props.value}`;
45-
return <span>{displayLabel}</span>;
45+
return <span title={props.title}>{displayLabel}</span>;
4646
}
4747

4848
const DropdownOption: React.FC<DropdownOptionProps> = ({
@@ -320,10 +320,19 @@ const Dropdown = (props: DropdownProps) => {
320320
return;
321321
}
322322

323+
// Don't interfere with the event if the user is using Home/End keys on the search input
324+
if (
325+
['Home', 'End'].includes(e.key) &&
326+
document.activeElement instanceof HTMLInputElement
327+
) {
328+
return;
329+
}
330+
323331
const focusableElements = e.currentTarget.querySelectorAll(
324332
'input[type="search"], input[type="checkbox"]:not([disabled])'
325333
) as NodeListOf<HTMLElement>;
326334

335+
// Don't interfere with the event if there aren't any options that the user can interact with
327336
if (focusableElements.length === 0) {
328337
return;
329338
}

components/dash-core-components/src/utils/dropdownSearch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export function createFilteredOptions(
3636
// Sanitize and prepare options
3737
let sanitized = sanitizeOptions(options);
3838

39-
const indexes = [];
39+
const indexes = ['value'];
4040
let hasElement = false,
4141
hasSearch = false;
4242

components/dash-core-components/tests/integration/misc/test_dcc_components_as_props.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
from selenium.webdriver.common.keys import Keys
12
from dash import Dash, dcc, html
3+
from dash.testing import wait
24

35

46
def test_mdcap001_dcc_components_as_props(dash_dcc):
@@ -49,25 +51,26 @@ def test_mdcap001_dcc_components_as_props(dash_dcc):
4951
dash_dcc.wait_for_text_to_equal("#dropdown h4", "h4")
5052
dash_dcc.wait_for_text_to_equal("#dropdown h6", "h6")
5153

52-
search_input = dash_dcc.find_element("#dropdown input")
54+
search_input = dash_dcc.find_element("#dropdown .dash-dropdown-search")
5355
search_input.send_keys("4")
54-
options = dash_dcc.find_elements("#dropdown .VirtualizedSelectOption")
56+
options = dash_dcc.find_elements("#dropdown .dash-dropdown-option")
5557

5658
assert len(options) == 1
57-
assert options[0].text == "h4"
59+
wait.until(lambda: options[0].text == "h4", 1)
60+
61+
search_input.send_keys(Keys.ESCAPE)
62+
dash_dcc.find_element("#indexed-search").click()
5863

5964
def search_indexed(value, length, texts):
60-
search = dash_dcc.find_element("#indexed-search input")
65+
search = dash_dcc.find_element("#indexed-search .dash-dropdown-search")
6166
dash_dcc.clear_input(search)
6267
search.send_keys(value)
63-
opts = dash_dcc.find_elements("#indexed-search .VirtualizedSelectOption")
68+
opts = dash_dcc.find_elements("#indexed-search .dash-dropdown-option")
6469

6570
assert len(opts) == length
6671
assert [o.text for o in opts] == texts
6772

6873
search_indexed("o", 2, ["one", "two"])
6974
search_indexed("1", 1, ["one"])
7075
search_indexed("uno", 1, ["one"])
71-
# FIXME clear_input doesnt work well when the input is focused. (miss the o)
72-
dash_dcc.clear_input("#indexed-search input")
7376
search_indexed("dos", 1, ["two"])

components/dash-core-components/tests/integration/test_title_props.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,15 @@ def add_title_to_option(title):
5252
dash_dcc.start_server(app)
5353

5454
elements = [
55-
dash_dcc.wait_for_element("#dropdown_1 .Select-value"),
56-
dash_dcc.wait_for_element("#dropdown_2 .Select-value"),
55+
dash_dcc.wait_for_element("#dropdown_1 .dash-dropdown-value span"),
56+
dash_dcc.wait_for_element("#dropdown_2 .dash-dropdown-value span"),
5757
dash_dcc.wait_for_element("#checklist_1 .Select-value-label"),
5858
dash_dcc.wait_for_element("#radioitems_1 .Select-value-label"),
5959
]
6060

6161
component_title_input = dash_dcc.wait_for_element("#title_input")
6262

6363
# Empty string title ('') (default for no title)
64-
6564
for element in elements:
6665
wait.until(lambda: element.get_attribute("title") == "", 3)
6766

0 commit comments

Comments
 (0)