Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(default-theme): added searchbar on mobile devices #1144

Merged
merged 5 commits into from
Oct 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/default-theme/components/SwHeader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<SfHeader
:title="$t('page.title')"
class="sw-header"
class="sw-header sf-header--has-mobile-search"
:has-mobile-search="false"
:is-sticky="false"
>
Expand Down
42 changes: 28 additions & 14 deletions packages/default-theme/components/SwSearchBar.vue
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
<template>
<div class="sw-search-bar">
<SfSearchBar
v-model="typingQuery"
:placeholder="$t('Search for products')"
:aria-label="$t('Search for products')"
class="sf-header__search desktop-only"
v-model="typingQuery"
class="sf-header__search"
data-cy="search-bar"
@enter="performSearch"
@focus="isSuggestBoxOpen = true"
data-cy="search-bar"
/>

<SwSuggestSearch
v-if="!isMobile"
:products="getProducts"
:total-found="getTotal"
:search-phrase="typingQuery"
Expand All @@ -21,12 +23,16 @@
</template>

<script>
import { ref, reactive, onMounted, watch, computed } from "@vue/composition-api"
import { ref, reactive, onMounted, computed } from "@vue/composition-api"
import { getSearchPageUrl } from "@/helpers"
import { SfSearchBar } from "@storefront-ui/vue"
import { useProductQuickSearch } from "@shopware-pwa/composables"
import {
mapMobileObserver,
unMapMobileObserver,
} from "@storefront-ui/vue/src/utilities/mobile-observer"
import { debounce } from "@shopware-pwa/helpers"
import SwSuggestSearch from "@/components/SwSuggestSearch"
const SwSuggestSearch = () => import("@/components/SwSuggestSearch")

export default {
components: {
Expand All @@ -51,29 +57,37 @@ export default {
}
}, 300)

watch(typingQuery, () => {
performSuggestSearch(typingQuery.value)
})

return {
getProducts,
getTotal,
isSuggestBoxOpen,
typingQuery,
performSuggestSearch,
}
},
computed: {
...mapMobileObserver(),
},
watch: {
$route(to, from) {
this.isSuggestBoxOpen = false
},
typingQuery(value) {
if (!this.isMobile) {
this.performSuggestSearch(value)
}
},
},
beforeDestroy() {
unMapMobileObserver()
},
methods: {
performSearch() {
if (this.typingQuery.length > 0) {
this.$router.push(this.$i18n.path(getSearchPageUrl(this.typingQuery)))
}
},
},
watch: {
$route(to, from) {
this.isSuggestBoxOpen = false
},
},
}
</script>

Expand Down