mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-28 10:50:40 +01:00
fix(InputMenu/SelectMenu): escape regexp before search
This commit is contained in:
@@ -127,7 +127,7 @@ import { useFormField } from '../composables/useFormField'
|
|||||||
import UIcon from './Icon.vue'
|
import UIcon from './Icon.vue'
|
||||||
import UAvatar from './Avatar.vue'
|
import UAvatar from './Avatar.vue'
|
||||||
import UChip from './Chip.vue'
|
import UChip from './Chip.vue'
|
||||||
import { get } from '../utils'
|
import { get, escapeRegExp } from '../utils'
|
||||||
|
|
||||||
defineOptions({ inheritAttrs: false })
|
defineOptions({ inheritAttrs: false })
|
||||||
|
|
||||||
@@ -175,16 +175,17 @@ function filterFunction(items: ArrayOrWrapped<AcceptableValue>, searchTerm: stri
|
|||||||
}
|
}
|
||||||
|
|
||||||
const fields = Array.isArray(props.filter) ? props.filter : ['label']
|
const fields = Array.isArray(props.filter) ? props.filter : ['label']
|
||||||
|
const escapedSearchTerm = escapeRegExp(searchTerm)
|
||||||
|
|
||||||
return items.filter((item) => {
|
return items.filter((item) => {
|
||||||
if (typeof item !== 'object') {
|
if (typeof item !== 'object') {
|
||||||
return String(item).search(new RegExp(searchTerm, 'i')) !== -1
|
return String(item).search(new RegExp(escapedSearchTerm, 'i')) !== -1
|
||||||
}
|
}
|
||||||
|
|
||||||
return fields.some((field) => {
|
return fields.some((field) => {
|
||||||
const child = get(item, field)
|
const child = get(item, field)
|
||||||
|
|
||||||
return child !== null && child !== undefined && String(child).search(new RegExp(searchTerm, 'i')) !== -1
|
return child !== null && child !== undefined && String(child).search(new RegExp(escapedSearchTerm, 'i')) !== -1
|
||||||
})
|
})
|
||||||
}) as ArrayOrWrapped<T>
|
}) as ArrayOrWrapped<T>
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -117,7 +117,7 @@ import { useFormField } from '../composables/useFormField'
|
|||||||
import UIcon from './Icon.vue'
|
import UIcon from './Icon.vue'
|
||||||
import UAvatar from './Avatar.vue'
|
import UAvatar from './Avatar.vue'
|
||||||
import UChip from './Chip.vue'
|
import UChip from './Chip.vue'
|
||||||
import { get } from '../utils'
|
import { get, escapeRegExp } from '../utils'
|
||||||
|
|
||||||
const props = withDefaults(defineProps<SelectMenuProps<T>>(), {
|
const props = withDefaults(defineProps<SelectMenuProps<T>>(), {
|
||||||
search: true,
|
search: true,
|
||||||
@@ -167,16 +167,17 @@ function filterFunction(items: ArrayOrWrapped<AcceptableValue>, searchTerm: stri
|
|||||||
}
|
}
|
||||||
|
|
||||||
const fields = Array.isArray(props.filter) ? props.filter : ['label']
|
const fields = Array.isArray(props.filter) ? props.filter : ['label']
|
||||||
|
const escapedSearchTerm = escapeRegExp(searchTerm)
|
||||||
|
|
||||||
return items.filter((item) => {
|
return items.filter((item) => {
|
||||||
if (typeof item !== 'object') {
|
if (typeof item !== 'object') {
|
||||||
return String(item).search(new RegExp(searchTerm, 'i')) !== -1
|
return String(item).search(new RegExp(escapedSearchTerm, 'i')) !== -1
|
||||||
}
|
}
|
||||||
|
|
||||||
return fields.some((field) => {
|
return fields.some((field) => {
|
||||||
const child = get(item, field)
|
const child = get(item, field)
|
||||||
|
|
||||||
return child !== null && child !== undefined && String(child).search(new RegExp(searchTerm, 'i')) !== -1
|
return child !== null && child !== undefined && String(child).search(new RegExp(escapedSearchTerm, 'i')) !== -1
|
||||||
})
|
})
|
||||||
}) as ArrayOrWrapped<T>
|
}) as ArrayOrWrapped<T>
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,3 +59,7 @@ export function looseToNumber(val: any): any {
|
|||||||
const n = Number.parseFloat(val)
|
const n = Number.parseFloat(val)
|
||||||
return Number.isNaN(n) ? val : n
|
return Number.isNaN(n) ? val : n
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function escapeRegExp(string: string) {
|
||||||
|
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user