Skip to content

Commit

Permalink
Check if input is null when working with it
Browse files Browse the repository at this point in the history
  • Loading branch information
obulat committed Nov 28, 2024
1 parent 9913c49 commit d403a51
Showing 1 changed file with 30 additions and 11 deletions.
41 changes: 30 additions & 11 deletions frontend/src/components/VHeader/VHeaderMobile/VHeaderMobile.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
<script setup lang="ts">
import { firstParam, focusIn, useNuxtApp, useRoute, useRouter } from "#imports"
import { computed, nextTick, ref, SetupContext, watch } from "vue"
import {
computed,
nextTick,
ref,
type SetupContext,
useTemplateRef,
watch,
} from "vue"
import { onClickOutside } from "@vueuse/core"
import {
Expand Down Expand Up @@ -36,16 +43,18 @@ import VSearchBarButton from "~/components/VHeader/VHeaderMobile/VSearchBarButto
* displayed in the bar itself.
*/
const searchInputRef = ref<HTMLInputElement | null>(null)
const headerRef = ref<HTMLElement | null>(null)
const recentSearchesRef = ref<InstanceType<typeof VRecentSearches> | null>(null)
const contentSettingsButtonRef = ref<InstanceType<
typeof VContentSettingsButton
> | null>(null)
const searchInputRef = useTemplateRef("searchInputRef")
const headerRef = useTemplateRef("headerRef")
const recentSearchesRef =
useTemplateRef<InstanceType<typeof VRecentSearches>>("recentSearchesRef")
const contentSettingsButtonRef = useTemplateRef<
InstanceType<typeof VContentSettingsButton>
>("contentSettingsButtonRef")
const contentSettingsButton = computed(
() => (contentSettingsButtonRef.value?.$el as HTMLElement) ?? undefined
)
const clearButtonRef = ref<InstanceType<typeof VSearchBarButton> | null>(null)
const clearButtonRef =
useTemplateRef<InstanceType<typeof VSearchBarButton>>("clearButtonRef")
const mediaStore = useMediaStore()
const searchStore = useSearchStore()
Expand All @@ -69,7 +78,10 @@ const { updateSearchState, searchTerm, searchStatus } =
const localSearchTerm = ref(searchTerm.value)
const focusInput = () => {
const input = searchInputRef.value as HTMLInputElement
const input = searchInputRef.value
if (!input) {
return
}
ensureFocus(input)
input.selectionStart = selection.value.start
input.selectionEnd = selection.value.end
Expand Down Expand Up @@ -110,7 +122,10 @@ const deactivate = () => {
* the input field is blurred and focused again.
*/
const updateSelection = () => {
const inputElement = searchInputRef.value as HTMLInputElement
const inputElement = searchInputRef.value
if (!inputElement) {
return
}
const lastPos =
inputElement.value.length > 0 ? inputElement.value.length - 1 : 0
selection.value = {
Expand All @@ -127,7 +142,11 @@ const updateSelection = () => {
* and activate the search bar.
*/
const updateSearchText = () => {
localSearchTerm.value = (searchInputRef.value as HTMLInputElement).value
const inputElement = searchInputRef.value
if (!inputElement) {
return
}
localSearchTerm.value = inputElement.value
updateSelection()
if (isInputFocused.value && !isSearchBarActive.value) {
activate()
Expand Down

0 comments on commit d403a51

Please sign in to comment.