Skip to content

Commit 31a1c9b

Browse files
authored
Check if input is null when working with it (#5193)
1 parent e017b6b commit 31a1c9b

File tree

1 file changed

+32
-12
lines changed

1 file changed

+32
-12
lines changed

frontend/src/components/VHeader/VHeaderMobile/VHeaderMobile.vue

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
<script setup lang="ts">
2-
import { focusIn, useNuxtApp, useRoute, useRouter } from "#imports"
3-
import { computed, nextTick, ref, SetupContext, watch } from "vue"
2+
import { useNuxtApp, useRoute, useRouter } from "#imports"
3+
import {
4+
computed,
5+
nextTick,
6+
ref,
7+
type SetupContext,
8+
useTemplateRef,
9+
watch,
10+
} from "vue"
411
512
import { onClickOutside } from "@vueuse/core"
613
@@ -15,6 +22,7 @@ import {
1522
getAllTabbableIn,
1623
getFirstTabbableIn,
1724
} from "~/utils/reakit-utils/focus"
25+
import { focusIn } from "~/utils/focus-management"
1826
import { useMediaStore } from "~/stores/media"
1927
import { useSearchStore } from "~/stores/search"
2028
import { useDialogControl } from "~/composables/use-dialog-control"
@@ -34,16 +42,18 @@ import VSearchBarButton from "~/components/VHeader/VHeaderMobile/VSearchBarButto
3442
* displayed in the bar itself.
3543
*/
3644
37-
const searchInputRef = ref<HTMLInputElement | null>(null)
38-
const headerRef = ref<HTMLElement | null>(null)
39-
const recentSearchesRef = ref<InstanceType<typeof VRecentSearches> | null>(null)
40-
const contentSettingsButtonRef = ref<InstanceType<
41-
typeof VContentSettingsButton
42-
> | null>(null)
45+
const searchInputRef = useTemplateRef("searchInputRef")
46+
const headerRef = useTemplateRef("headerRef")
47+
const recentSearchesRef =
48+
useTemplateRef<InstanceType<typeof VRecentSearches>>("recentSearchesRef")
49+
const contentSettingsButtonRef = useTemplateRef<
50+
InstanceType<typeof VContentSettingsButton>
51+
>("contentSettingsButtonRef")
4352
const contentSettingsButton = computed(
4453
() => (contentSettingsButtonRef.value?.$el as HTMLElement) ?? undefined
4554
)
46-
const clearButtonRef = ref<InstanceType<typeof VSearchBarButton> | null>(null)
55+
const clearButtonRef =
56+
useTemplateRef<InstanceType<typeof VSearchBarButton>>("clearButtonRef")
4757
4858
const mediaStore = useMediaStore()
4959
const searchStore = useSearchStore()
@@ -67,7 +77,10 @@ const { updateSearchState, searchTerm, searchStatus } =
6777
const localSearchTerm = ref(searchTerm.value)
6878
6979
const focusInput = () => {
70-
const input = searchInputRef.value as HTMLInputElement
80+
const input = searchInputRef.value
81+
if (!input) {
82+
return
83+
}
7184
ensureFocus(input)
7285
input.selectionStart = selection.value.start
7386
input.selectionEnd = selection.value.end
@@ -108,7 +121,10 @@ const deactivate = () => {
108121
* the input field is blurred and focused again.
109122
*/
110123
const updateSelection = () => {
111-
const inputElement = searchInputRef.value as HTMLInputElement
124+
const inputElement = searchInputRef.value
125+
if (!inputElement) {
126+
return
127+
}
112128
const lastPos =
113129
inputElement.value.length > 0 ? inputElement.value.length - 1 : 0
114130
selection.value = {
@@ -125,7 +141,11 @@ const updateSelection = () => {
125141
* and activate the search bar.
126142
*/
127143
const updateSearchText = () => {
128-
localSearchTerm.value = (searchInputRef.value as HTMLInputElement).value
144+
const inputElement = searchInputRef.value
145+
if (!inputElement) {
146+
return
147+
}
148+
localSearchTerm.value = inputElement.value
129149
updateSelection()
130150
if (isInputFocused.value && !isSearchBarActive.value) {
131151
activate()

0 commit comments

Comments
 (0)