-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
optimize dropdown search #3768
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
base: dev
Are you sure you want to change the base?
optimize dropdown search #3768
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ export interface SanitizedOptions { | |
| options: DetailedOption[]; | ||
| indexes: string[]; | ||
| valueSet: Set<OptionValue>; | ||
| search: Search; | ||
| } | ||
|
|
||
| // Single-pass sanitization via sanitizeOptions, plus detection of | ||
|
|
@@ -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); | ||
| }); | ||
|
|
||
| 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call! It's better to use the correct prop types.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you use
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
|
||
| 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; | ||
| } | ||
There was a problem hiding this comment.
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
uidFieldNamepassed into the constructor? I ask becauseindexesalways contains'value'and that's the same as theuidFieldNameused in the constructor.There was a problem hiding this comment.
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)