Skip to content

Commit 61efd4f

Browse files
committed
Check if input is null when working with it
1 parent 1edcaf8 commit 61efd4f

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

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

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
<script setup lang="ts">
22
import { firstParam, focusIn, useNuxtApp, useRoute, useRouter } from "#imports"
33
4-
import { computed, nextTick, ref, SetupContext, watch } from "vue"
4+
import {
5+
computed,
6+
nextTick,
7+
ref,
8+
type SetupContext,
9+
useTemplateRef,
10+
watch,
11+
} from "vue"
512
import { onClickOutside } from "@vueuse/core"
613
714
import {
@@ -36,16 +43,18 @@ import VSearchBarButton from "~/components/VHeader/VHeaderMobile/VSearchBarButto
3643
* displayed in the bar itself.
3744
*/
3845
39-
const searchInputRef = ref<HTMLInputElement | null>(null)
40-
const headerRef = ref<HTMLElement | null>(null)
41-
const recentSearchesRef = ref<InstanceType<typeof VRecentSearches> | null>(null)
42-
const contentSettingsButtonRef = ref<InstanceType<
43-
typeof VContentSettingsButton
44-
> | null>(null)
46+
const searchInputRef = useTemplateRef("searchInputRef")
47+
const headerRef = useTemplateRef("headerRef")
48+
const recentSearchesRef =
49+
useTemplateRef<InstanceType<typeof VRecentSearches>>("recentSearchesRef")
50+
const contentSettingsButtonRef = useTemplateRef<
51+
InstanceType<typeof VContentSettingsButton>
52+
>("contentSettingsButtonRef")
4553
const contentSettingsButton = computed(
4654
() => (contentSettingsButtonRef.value?.$el as HTMLElement) ?? undefined
4755
)
48-
const clearButtonRef = ref<InstanceType<typeof VSearchBarButton> | null>(null)
56+
const clearButtonRef =
57+
useTemplateRef<InstanceType<typeof VSearchBarButton>>("clearButtonRef")
4958
5059
const mediaStore = useMediaStore()
5160
const searchStore = useSearchStore()
@@ -69,7 +78,10 @@ const { updateSearchState, searchTerm, searchStatus } =
6978
const localSearchTerm = ref(searchTerm.value)
7079
7180
const focusInput = () => {
72-
const input = searchInputRef.value as HTMLInputElement
81+
const input = searchInputRef.value
82+
if (!input) {
83+
return
84+
}
7385
ensureFocus(input)
7486
input.selectionStart = selection.value.start
7587
input.selectionEnd = selection.value.end
@@ -110,7 +122,10 @@ const deactivate = () => {
110122
* the input field is blurred and focused again.
111123
*/
112124
const updateSelection = () => {
113-
const inputElement = searchInputRef.value as HTMLInputElement
125+
const inputElement = searchInputRef.value
126+
if (!inputElement) {
127+
return
128+
}
114129
const lastPos =
115130
inputElement.value.length > 0 ? inputElement.value.length - 1 : 0
116131
selection.value = {
@@ -127,7 +142,11 @@ const updateSelection = () => {
127142
* and activate the search bar.
128143
*/
129144
const updateSearchText = () => {
130-
localSearchTerm.value = (searchInputRef.value as HTMLInputElement).value
145+
const inputElement = searchInputRef.value
146+
if (!inputElement) {
147+
return
148+
}
149+
localSearchTerm.value = inputElement.value
131150
updateSelection()
132151
if (isInputFocused.value && !isSearchBarActive.value) {
133152
activate()

0 commit comments

Comments
 (0)