-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
24.06.35: added filters module filter-search.vue [WTEL-4530]
- Loading branch information
Showing
3 changed files
with
90 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<template> | ||
<wt-search-bar | ||
:search-mode="multisearch && filterName" | ||
:search-mode-options="multisearch && searchModeOpts" | ||
:value="localValue" | ||
class="filter-search" | ||
@input="localValue = $event" | ||
@search="setValue({ name: filterName, value: localValue })" | ||
@update:search-mode="changeMode" | ||
/> | ||
</template> | ||
|
||
<script setup> | ||
import { computed, ref, watch } from 'vue'; | ||
import { useStore } from 'vuex'; | ||
import FilterEvent from '../enums/FilterEvent.enum.js'; | ||
const props = defineProps({ | ||
namespace: { | ||
type: String, | ||
required: true, | ||
}, | ||
multisearch: { | ||
type: Boolean, | ||
default: false, | ||
}, | ||
name: { | ||
type: String, | ||
default: 'q', | ||
}, | ||
searchModeOpts: { | ||
type: Array, | ||
default: () => [], | ||
}, | ||
}); | ||
const store = useStore(); | ||
function getValue(filter) { | ||
return store.getters[`${props.namespace}/GET_FILTER`](filter); | ||
} | ||
function setValue(payload) { | ||
return store.dispatch(`${props.namespace}/SET_FILTER`, payload); | ||
} | ||
const filterName = ref(props.multisearch ? props.searchModeOpts[0].value : props.name); | ||
const filterValue = computed(() => getValue(filterName.value)); | ||
const localValue = ref(filterValue.value); | ||
async function changeMode({ value }, { clearValue = true } = {}) { | ||
if (clearValue) await setValue({ name: filterName.value, value: '' }); | ||
filterName.value = value; | ||
} | ||
function restoreSearchMode() { | ||
const mode = props.searchModeOpts.find(({ value }) => !!getValue(value)); | ||
if (mode) changeMode({ value: mode.value }, { clearValue: false }); | ||
} | ||
function subscribe(payload) { | ||
store.dispatch(`${props.namespace}/SUBSCRIBE`, payload); | ||
} | ||
subscribe({ | ||
event: FilterEvent.RESTORED, | ||
callback: restoreSearchMode, | ||
}); | ||
watch(() => filterValue.value, () => { | ||
localValue.value = filterValue.value; | ||
}); | ||
</script> | ||
|
||
<style lang="scss" scoped> | ||
</style> |