Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
- [#3738](https://github.com/plotly/dash/pull/3738) Add missing `stacklevel=2` to `warnings.warn()` calls so warnings report the caller's location instead of internal Dash source lines
- [#3740](https://github.com/plotly/dash/pull/3740) Fix cannot tab into dropdowns in Safari
- [#2462](https://github.com/plotly/dash/issues/2462) Allow `MATCH` in `Input`/`State` when the callback's `Output` has no wildcards (fixed-id Output, no Output, or `ALL`-only wildcard Output). `ALLSMALLER` still requires a corresponding `MATCH` in an Output.
- [#3768](https://github.com/plotly/dash/pull/3768) Improved `Dropdown` search performance for large options lists
- [#3759](https://github.com/plotly/dash/pull/3759) Fix the issue where `Patch` objects cannot be updated via `set_props()` in `websocket` callback. Fix [#3742](https://github.com/plotly/dash/issues/3742)

## [4.2.0rc3] - 2026-05-12
Expand Down
51 changes: 31 additions & 20 deletions components/dash-core-components/src/utils/dropdownSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface SanitizedOptions {
options: DetailedOption[];
indexes: string[];
valueSet: Set<OptionValue>;
search: Search;
}

// Single-pass sanitization via sanitizeOptions, plus detection of
Expand Down Expand Up @@ -55,38 +56,48 @@ export function sanitizeDropdownOptions(
indexes.push('search');
}

return {options: sanitized, indexes, valueSet};
// Build the search index ONCE during sanitization
const search = new Search('value');
search.searchIndex = new UnorderedSearchIndex();
search.indexStrategy = new AllSubstringsIndexStrategy();
search.tokenizer = TOKENIZER;

indexes.forEach(index => {
search.addIndex(index);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it matter if the index include here is the same as the uidFieldName passed into the constructor? I ask because indexes always contains 'value' and that's the same as the uidFieldName used in the constructor.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'value' in the constructor is only used as the document uid field in js-search. It is not automatically indexed for searching, so we still need search.addIndex('value') here.
This is unchanged from previous version (old line 75-77)

});

if (sanitized.length > 0) {
search.addDocuments(sanitized);
}

return {
options: sanitized,
indexes,
valueSet,
search,
};
}

export function filterOptions(
options: SanitizedOptions,
searchValue?: string,
search_order?: 'index' | 'original'
searchOrder?: string
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove the union type here? In fact, you could reference the existing type in components/dash-core-components/src/types.ts.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call! It's better to use the correct prop types.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you'll need to update references to this property throughout the repo.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what you mean here. But if the type is defined like this, does it have to change anywhere else?

searchOrder?: DropdownProps['search_order']

): DetailedOption[] {
if (!searchValue) {
return options.options;
}

const search = new Search('value');
search.searchIndex = new UnorderedSearchIndex();
search.indexStrategy = new AllSubstringsIndexStrategy();
search.tokenizer = TOKENIZER;

options.indexes.forEach(index => {
search.addIndex(index);
});
const results =
(options.search.search(searchValue) as DetailedOption[]) || [];

if (options.options.length > 0) {
search.addDocuments(options.options);
}
// Preserve original option order
if (searchOrder === 'original') {
const resultSet = new Set(results.map(option => option.value));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you use map here? Does it allow for faster lookups when only using value?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The map just makes it more robust. It converts the results into a Set of option values instead of object references.


const searchResults =
(search.search(searchValue) as DetailedOption[]) || [];

if (search_order === 'original') {
const resultSet = new Set(searchResults);
return options.options.filter(option => resultSet.has(option));
return options.options.filter(option =>
resultSet.has(option.value)
);
}

return searchResults;
return results;
}
Loading