Skip to content

Commit

Permalink
fix(InputMenu/SelectMenu): escape regexp before search
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamincanac committed Oct 10, 2024
1 parent 0f9ac87 commit 7c21dde
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/runtime/components/InputMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ import { useFormField } from '../composables/useFormField'
import UIcon from './Icon.vue'
import UAvatar from './Avatar.vue'
import UChip from './Chip.vue'
import { get } from '../utils'
import { get, escapeRegExp } from '../utils'
defineOptions({ inheritAttrs: false })
Expand Down Expand Up @@ -175,16 +175,17 @@ function filterFunction(items: ArrayOrWrapped<AcceptableValue>, searchTerm: stri
}
const fields = Array.isArray(props.filter) ? props.filter : ['label']
const escapedSearchTerm = escapeRegExp(searchTerm)
return items.filter((item) => {
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) => {
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>
}
Expand Down
7 changes: 4 additions & 3 deletions src/runtime/components/SelectMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ import { useFormField } from '../composables/useFormField'
import UIcon from './Icon.vue'
import UAvatar from './Avatar.vue'
import UChip from './Chip.vue'
import { get } from '../utils'
import { get, escapeRegExp } from '../utils'
const props = withDefaults(defineProps<SelectMenuProps<T>>(), {
search: true,
Expand Down Expand Up @@ -167,16 +167,17 @@ function filterFunction(items: ArrayOrWrapped<AcceptableValue>, searchTerm: stri
}
const fields = Array.isArray(props.filter) ? props.filter : ['label']
const escapedSearchTerm = escapeRegExp(searchTerm)
return items.filter((item) => {
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) => {
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>
}
Expand Down
4 changes: 4 additions & 0 deletions src/runtime/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,7 @@ export function looseToNumber(val: any): any {
const n = Number.parseFloat(val)
return Number.isNaN(n) ? val : n
}

export function escapeRegExp(string: string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
}

0 comments on commit 7c21dde

Please sign in to comment.