Skip to content

Dropdown component label search #2110

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

Merged
merged 10 commits into from
Jul 13, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ Dropdown.propTypes = {
* see https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/title
*/
title: PropTypes.string,

/**
* Optional search value for the option, to use if the label
* is a component or provide a custom search value different
* from the label. If no search value and the label is a
* component, the `value` will be used for search.
*/
search: PropTypes.string,
})
),
]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,39 @@ const Dropdown = props => {
} = props;
const [optionsCheck, setOptionsCheck] = useState(null);
const [sanitizedOptions, filterOptions] = useMemo(() => {
const sanitized = sanitizeOptions(options);
let sanitized = sanitizeOptions(options);

const indexes = ['strValue'];
let hasElement = false,
hasSearch = false;
sanitized = Array.isArray(sanitized)
? sanitized.map(option => {
if (option.search) {
hasSearch = true;
}
if (React.isValidElement(option.label)) {
hasElement = true;
}
return {
...option,
strValue: String(option.value),
};
})
: sanitized;

if (!hasElement) {
indexes.push('label');
}
if (hasSearch) {
indexes.push('search');
}

return [
sanitized,
createFilterOptions({
options: sanitized,
tokenizer: TOKENIZER,
indexes,
}),
];
}, [options]);
Expand Down
3 changes: 2 additions & 1 deletion components/dash-core-components/src/utils/optionTypes.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React from 'react';
import {type} from 'ramda';

export const sanitizeOptions = options => {
if (type(options) === 'Object') {
return Object.entries(options).map(([value, label]) => ({
label: String(label),
label: React.isValidElement(label) ? label : String(label),
value,
}));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ def test_mdcap001_dcc_components_as_props(dash_dcc):
],
id="dropdown",
),
dcc.Dropdown(
[
{"label": "one", "value": 1, "search": "uno"},
{"label": "two", "value": 2, "search": "dos"},
],
id="indexed-search",
),
]
)

Expand All @@ -41,3 +48,26 @@ def test_mdcap001_dcc_components_as_props(dash_dcc):
dash_dcc.find_element("#dropdown").click()
dash_dcc.wait_for_text_to_equal("#dropdown h4", "h4")
dash_dcc.wait_for_text_to_equal("#dropdown h6", "h6")

search_input = dash_dcc.find_element("#dropdown input")
search_input.send_keys("4")
options = dash_dcc.find_elements("#dropdown .VirtualizedSelectOption")

assert len(options) == 1
assert options[0].text == "h4"

def search_indexed(value, length, texts):
search = dash_dcc.find_element("#indexed-search input")
dash_dcc.clear_input(search)
search.send_keys(value)
opts = dash_dcc.find_elements("#indexed-search .VirtualizedSelectOption")

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

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