mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-25 09:20:36 +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
|
lazy: props.searchLazy
|
||||||
})
|
})
|
||||||
|
|
||||||
|
function escapeRegExp (string: string) {
|
||||||
|
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
||||||
|
}
|
||||||
|
|
||||||
const filteredOptions = computed(() => {
|
const filteredOptions = computed(() => {
|
||||||
if (!query.value || debouncedSearch) {
|
if (!query.value || debouncedSearch) {
|
||||||
return options.value
|
return options.value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const escapedQuery = escapeRegExp(query.value)
|
||||||
|
|
||||||
return options.value.filter((option: any) => {
|
return options.value.filter((option: any) => {
|
||||||
return (props.searchAttributes?.length ? props.searchAttributes : [props.optionAttribute]).some((searchAttribute: any) => {
|
return (props.searchAttributes?.length ? props.searchAttributes : [props.optionAttribute]).some((searchAttribute: any) => {
|
||||||
if (['string', 'number'].includes(typeof option)) {
|
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)
|
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
|
lazy: props.searchableLazy
|
||||||
})
|
})
|
||||||
|
|
||||||
|
function escapeRegExp (string: string) {
|
||||||
|
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
||||||
|
}
|
||||||
|
|
||||||
const filteredOptions = computed(() => {
|
const filteredOptions = computed(() => {
|
||||||
if (!query.value || debouncedSearch) {
|
if (!query.value || debouncedSearch) {
|
||||||
return options.value
|
return options.value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const escapedQuery = escapeRegExp(query.value)
|
||||||
|
|
||||||
return options.value.filter((option: any) => {
|
return options.value.filter((option: any) => {
|
||||||
return (props.searchAttributes?.length ? props.searchAttributes : [props.optionAttribute]).some((searchAttribute: any) => {
|
return (props.searchAttributes?.length ? props.searchAttributes : [props.optionAttribute]).some((searchAttribute: any) => {
|
||||||
if (['string', 'number'].includes(typeof option)) {
|
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)
|
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