Skip to content

Commit

Permalink
perf(SelectUnitModal): use debounced search
Browse files Browse the repository at this point in the history
  • Loading branch information
rudnovd committed Mar 31, 2024
1 parent 0edaaac commit c7f17b4
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions src/components/SelectUnitModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@
<template #header>
<input
ref="searchInput"
:value="search"
v-model="search"
type="text"
class="search-input"
name="search-creatures"
:placeholder="t('components.selectUnitModal.searchCreature')"
@keyup.enter="selectFirstFounded"
@input="searchCreature"
/>
</template>

<template #content>
<div class="units-modal-content">
<div v-if="!search" class="units">
<div v-if="!debouncedSearch" class="units">
<div v-for="town in towns" :key="town.name" class="town">
<ObjectPortrait
v-for="creature in creaturesByTowns[town.id]"
Expand Down Expand Up @@ -64,6 +63,7 @@
import BaseDialog from '@/components/base/BaseDialog.vue'
import type { Creature } from '@/models/Creature'
import { useStore } from '@/store'
import { refDebounced } from '@vueuse/core'
import { computed, defineAsyncComponent, defineComponent, nextTick, ref, watch } from 'vue'
import { useI18n } from 'vue-i18n'
Expand All @@ -88,6 +88,7 @@ export default defineComponent({
const towns = computed(() => store.towns)
const search = ref('')
const debouncedSearch = refDebounced(search, 200)
const searchInput = ref()
watch(
Expand All @@ -98,10 +99,9 @@ export default defineComponent({
)
const searchCreatures = computed(() => {
return creatures.value.filter((creature: Creature) => {
const searchText = search.value.toLowerCase()
const creatureName = creature.name.toLowerCase()
return creatureName.indexOf(searchText) > -1
const searchText = debouncedSearch.value.toLowerCase()
return creatures.value.filter(({ name }: Creature) => {
return name.toLowerCase().includes(searchText)
})
})
Expand Down Expand Up @@ -141,24 +141,19 @@ export default defineComponent({
context.emit('close')
}
const searchCreature = (event: Event) => {
const target = event.currentTarget as HTMLInputElement
search.value = target.value
}
return {
t,
towns,
search,
debouncedSearch,
creaturesByTowns,
searchCreatures,
searchInput,
selectUnit,
selectFirstFounded,
onClose,
searchCreature,
}
},
})
Expand Down

0 comments on commit c7f17b4

Please sign in to comment.