mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-14 20:19:34 +01:00
fix(InputMenu/SelectMenu): escape regexp before search
Resolves nuxt/ui#2308
This commit is contained in:
@@ -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
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -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
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user