@empathyco/x-components
Version:
Empathy X Components
71 lines (68 loc) • 2.36 kB
JavaScript
import { defineComponent, inject, computed, watch } from 'vue';
import { useXBus } from '../../../composables/use-x-bus.js';
import { isArrayEmpty } from '../../../utils/array.js';
import { createRawFilters } from '../../../utils/filters.js';
import { facetsXModule } from '../x-module.js';
/**
* This component emits {@link FacetsXEvents.PreselectedFiltersProvided} when a preselected filter
* is set in the snippet config or by using the prop of the component.
*
* @public
*/
var _sfc_main = defineComponent({
name: 'PreselectedFilters',
xModule: facetsXModule.name,
props: {
/**
* A list of filters to preselect.
*
* @remarks Emits the {@link FacetsXEvents.PreselectedFiltersProvided} when the
* component is created.
*
* @public
*/
filters: {
type: Array,
default: () => [],
},
},
setup(props, { slots }) {
const xBus = useXBus();
/**
* Injects {@link SnippetConfig} provided by an ancestor as snippetConfig
* and sets is as a ref to get synced when it changes.
*
* @internal
*/
const snippetConfig = inject('snippetConfig');
/**
* Gets the provided preselected filters prioritizing the {@link SnippetConfig} over the
* filters prop.
*
* @returns An array of filter's ids.
* @internal
*/
const preselectedFilters = computed(() => {
return snippetConfig?.filters ?? props.filters;
});
/**
* Emits the {@link FacetsXEvents.PreselectedFiltersProvided} to save
* the provided filters in the state.
*
* @internal
*/
const emitPreselectedFilters = () => {
if (!isArrayEmpty(preselectedFilters.value)) {
xBus.emit('PreselectedFiltersProvided', createRawFilters(preselectedFilters.value));
}
};
/**
* Emits the {@link FacetsXEvents.PreselectedFiltersProvided} when the
* computed prop changes.
*/
watch(preselectedFilters, emitPreselectedFilters, { immediate: true });
return () => slots.default?.()[0] ?? '';
},
});
export { _sfc_main as default };
//# sourceMappingURL=preselected-filters.vue.js.map