Skip to content

Commit

Permalink
feat: add snippet provider & use it
Browse files Browse the repository at this point in the history
  • Loading branch information
annacv committed Feb 27, 2024
1 parent c5c7997 commit bec789f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
37 changes: 37 additions & 0 deletions packages/x-components/src/components/snippet-config-provider.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<script lang="ts">
import { defineComponent, provide, ref } from 'vue';
import { useNoElementRender } from '../composables';
import { baseSnippetConfig } from '../views/base-config';
import { SnippetConfig } from '../x-installer';
/**.
* SnippetConfig Provider component.
* This component provides a SnippetConfig object and a function that is responsible for mutating the SnippetConfig when we need to update its data from an injector component.
*
* @public
*/
export default defineComponent({
name: 'SnippetConfigProvider',
setup(_, { slots }) {
const snippetConfig = ref(baseSnippetConfig);
/**
* Sets or updates the snippet config.
*
* @param config - A part or all the snippet config.
*
* @public
*/
function setSnippetConfig(config: Partial<SnippetConfig>): void {
Object.assign(snippetConfig.value, config);
}
provide('snippetConfig', {
snippetConfig,
setSnippetConfig
});
return () => useNoElementRender(slots);
}
});
</script>
6 changes: 5 additions & 1 deletion packages/x-components/src/views/home/Home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
<div class="x x-text-neutral-90">
<Tagging :consent="false" />
<SnippetConfigExtraParams :values="initialExtraParams" />
<PreselectedFilters />
<SnippetConfigProvider>
<PreselectedFilters />
</SnippetConfigProvider>
<UrlHandler query="q" store="store" />
<SnippetCallbacks />
<ExperienceControls />
Expand Down Expand Up @@ -520,6 +522,7 @@
import { QueryPreviewInfo } from '../../x-modules/queries-preview/store/types';
import QueryPreviewButton from '../../x-modules/queries-preview/components/query-preview-button.vue';
import DisplayEmitter from '../../components/display-emitter.vue';
import SnippetConfigProvider from '../../components/snippet-config-provider.vue';
import Aside from './aside.vue';
import PredictiveLayer from './predictive-layer.vue';
import Result from './result.vue';
Expand All @@ -531,6 +534,7 @@
infiniteScroll
},
components: {
SnippetConfigProvider,
DisplayEmitter,
QueryPreviewButton,
DisplayResultProvider,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<script lang="ts">
import { defineComponent, PropType, onMounted, watch, computed, ComputedRef } from 'vue';
import { defineComponent, PropType, onMounted, watch, computed, ComputedRef, inject } from 'vue';
import { createRawFilters } from '../../../utils/filters';
import { isArrayEmpty } from '../../../utils/array';
import { SnippetConfig } from '../../../x-installer/api/api.types';
import { use$x, useHybridInject, useNoElementRender } from '../../../composables/index';
import { use$x, useNoElementRender } from '../../../composables/index';
/**
* This component emits {@link FacetsXEvents.PreselectedFiltersProvided} when a preselected filter
Expand Down Expand Up @@ -35,7 +35,8 @@
*
* @internal
*/
const snippetConfig = useHybridInject<SnippetConfig>('snippetConfig');
// TODO: Solve this typing issues without using <any>
const { snippetConfig } = inject<Partial<SnippetConfig> | any>('snippetConfig');
/**
* Gets the provided preselected filters prioritizing the {@link SnippetConfig} over the
Expand Down

0 comments on commit bec789f

Please sign in to comment.