Skip to content

Commit

Permalink
fix(InputMenu/SelectMenu): escape regexp before search
Browse files Browse the repository at this point in the history
Resolves #2308
  • Loading branch information
benjamincanac committed Oct 10, 2024
1 parent dd0d055 commit c68ba76
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/runtime/components/forms/InputMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -401,20 +401,26 @@ export default defineComponent({
lazy: props.searchLazy
})
function escapeRegExp (string: string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
}
const filteredOptions = computed(() => {
if (!query.value || debouncedSearch) {
return options.value
}
const escapedQuery = escapeRegExp(query.value)
return options.value.filter((option: any) => {
return (props.searchAttributes?.length ? props.searchAttributes : [props.optionAttribute]).some((searchAttribute: any) => {
if (['string', 'number'].includes(typeof option)) {
return String(option).search(new RegExp(query.value, 'i')) !== -1
return String(option).search(new RegExp(escapedQuery, 'i')) !== -1
}
const child = get(option, searchAttribute)
return child !== null && child !== undefined && String(child).search(new RegExp(query.value, 'i')) !== -1
return child !== null && child !== undefined && String(child).search(new RegExp(escapedQuery, 'i')) !== -1
})
})
})
Expand Down
10 changes: 8 additions & 2 deletions src/runtime/components/forms/SelectMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -485,20 +485,26 @@ export default defineComponent({
lazy: props.searchableLazy
})
function escapeRegExp (string: string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
}
const filteredOptions = computed(() => {
if (!query.value || debouncedSearch) {
return options.value
}
const escapedQuery = escapeRegExp(query.value)
return options.value.filter((option: any) => {
return (props.searchAttributes?.length ? props.searchAttributes : [props.optionAttribute]).some((searchAttribute: any) => {
if (['string', 'number'].includes(typeof option)) {
return String(option).search(new RegExp(query.value, 'i')) !== -1
return String(option).search(new RegExp(escapedQuery, 'i')) !== -1
}
const child = get(option, searchAttribute)
return child !== null && child !== undefined && String(child).search(new RegExp(query.value, 'i')) !== -1
return child !== null && child !== undefined && String(child).search(new RegExp(escapedQuery, 'i')) !== -1
})
})
})
Expand Down

0 comments on commit c68ba76

Please sign in to comment.