Skip to content
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

Search improves #386

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
41 changes: 38 additions & 3 deletions src/mixins/treeselectMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,14 @@ export default {
default: constant([ 'label' ]),
},

/**
* The maximum number of matched leaf/branch nodes which should be expanded during local search
*/
maxExpandMatchesOnSearch: {
type: Number,
default: Infinity,
},

/**
* Sets `maxHeight` style value of the menu.
*/
Expand Down Expand Up @@ -408,6 +416,14 @@ export default {
default: 'No sub-options.',
},

/**
* On search the options should not expand to show matched nodes at all
*/
noExpandOnSearch: {
type: Boolean,
default: false,
},

/**
* Text displayed when there are no available options.
*/
Expand Down Expand Up @@ -518,6 +534,14 @@ export default {
default: true,
},

/**
* Minimum number of character after the search should be preformed
*/
searchMinInputLength: {
type: Number,
default: 1,
},

/**
* Search in ancestor nodes too.
*/
Expand Down Expand Up @@ -1196,7 +1220,7 @@ export default {
const { searchQuery } = this.trigger
const done = () => this.resetHighlightedOptionWhenNecessary(true)

if (!searchQuery) {
if (!searchQuery || searchQuery.length < this.searchMinInputLength) {
// Exit sync search mode.
this.localSearch.active = false
return done()
Expand Down Expand Up @@ -1224,6 +1248,7 @@ export default {

const lowerCasedSearchQuery = searchQuery.trim().toLocaleLowerCase()
const splitSearchQuery = lowerCasedSearchQuery.replace(/\s+/g, ' ').split(' ')
let maxExpandMatchesOnSearch = this.maxExpandMatchesOnSearch
this.traverseAllNodesDFS(node => {
if (this.searchNested && splitSearchQuery.length > 1) {
node.isMatched = splitSearchQuery.every(filterValue =>
Expand All @@ -1247,10 +1272,20 @@ export default {
}

if (
(node.isMatched || (node.isBranch && node.isExpandedOnSearch)) &&
(node.isMatched || (node.isBranch && node.hasMatchedDescendants)) &&
node.parentNode !== NO_PARENT_NODE
) {
node.parentNode.isExpandedOnSearch = true
if (!this.noExpandOnSearch) {
if (node.isMatched && maxExpandMatchesOnSearch) {
node.parentNode.isExpandedOnSearch = true
if (maxExpandMatchesOnSearch !== Infinity) {
--maxExpandMatchesOnSearch
}
} else if (node.isBranch && node.isExpandedOnSearch) {
node.parentNode.isExpandedOnSearch = true
}
}

node.parentNode.hasMatchedDescendants = true
}
})
Expand Down