-
Notifications
You must be signed in to change notification settings - Fork 21
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
refactor: Migrate preselected filters component #1422
Changes from 19 commits
abd1332
8e7bb45
928f86e
ff2d75b
cdc2dd8
91f5246
1facf0f
c882abe
b905727
0cd0688
c5c7997
c239e8d
274277c
896ddf3
619b397
a5ea863
142b086
078f700
d268201
1532f21
42ec203
2e54d17
0c98c95
b1c518f
7ee475b
58b9b84
1b33509
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,69 +1,83 @@ | ||||||||||||||
<script lang="ts"> | ||||||||||||||
import { Component, Inject, Prop, Watch } from 'vue-property-decorator'; | ||||||||||||||
import Vue from 'vue'; | ||||||||||||||
import { defineComponent, PropType, onMounted, watch, computed, ref, ComputedRef } from 'vue'; | ||||||||||||||
import { createRawFilters } from '../../../utils/filters'; | ||||||||||||||
import { isArrayEmpty } from '../../../utils/array'; | ||||||||||||||
import { SnippetConfig } from '../../../x-installer/api/api.types'; | ||||||||||||||
import { useXBus } from '../../../composables/use-x-bus'; | ||||||||||||||
import { useHybridInject, useNoElementRender } from '../../../composables'; | ||||||||||||||
import { baseSnippetConfig } from '../../../views/base-config'; | ||||||||||||||
|
||||||||||||||
/** | ||||||||||||||
* 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 | ||||||||||||||
*/ | ||||||||||||||
@Component | ||||||||||||||
export default class PreselectedFilters extends Vue { | ||||||||||||||
/** | ||||||||||||||
* Injects {@link SnippetConfig} provided by an ancestor as snippetConfig. | ||||||||||||||
* | ||||||||||||||
* @internal | ||||||||||||||
*/ | ||||||||||||||
@Inject('snippetConfig') | ||||||||||||||
public snippetConfig?: SnippetConfig; | ||||||||||||||
|
||||||||||||||
/** | ||||||||||||||
* A list of filters to preselect. | ||||||||||||||
* | ||||||||||||||
* @remarks Emits the {@link FacetsXEvents.PreselectedFiltersProvided} when the | ||||||||||||||
* component is created. | ||||||||||||||
* | ||||||||||||||
* @public | ||||||||||||||
*/ | ||||||||||||||
@Prop({ default: () => [] }) | ||||||||||||||
public filters!: string[]; | ||||||||||||||
|
||||||||||||||
/** | ||||||||||||||
* Gets the provided preselected filters prioritizing the {@link SnippetConfig} over the | ||||||||||||||
* filters prop. | ||||||||||||||
* | ||||||||||||||
* @returns An array of filter's ids. | ||||||||||||||
*/ | ||||||||||||||
protected get preselectedFilters(): string[] { | ||||||||||||||
return this.snippetConfig?.filters ?? this.filters; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
/** | ||||||||||||||
* Emits the {@link FacetsXEvents.PreselectedFiltersProvided} to save | ||||||||||||||
* the provided filters in the state. | ||||||||||||||
*/ | ||||||||||||||
@Watch('preselectedFilters') | ||||||||||||||
protected emitPreselectedFilters(): void { | ||||||||||||||
if (!isArrayEmpty(this.preselectedFilters)) { | ||||||||||||||
this.$x.emit('PreselectedFiltersProvided', createRawFilters(this.preselectedFilters)); | ||||||||||||||
export default defineComponent({ | ||||||||||||||
name: 'PreselectedFilters', | ||||||||||||||
props: { | ||||||||||||||
/** | ||||||||||||||
* A list of filters to preselect. | ||||||||||||||
* | ||||||||||||||
* @remarks Emits the {@link FacetsXEvents.PreselectedFiltersProvided} when the | ||||||||||||||
* component is created. | ||||||||||||||
* | ||||||||||||||
* @public | ||||||||||||||
*/ | ||||||||||||||
filters: { | ||||||||||||||
type: Array as PropType<string[]>, | ||||||||||||||
default: () => [] | ||||||||||||||
} | ||||||||||||||
}, | ||||||||||||||
setup(props, { slots }) { | ||||||||||||||
// eslint-disable-next-line @typescript-eslint/unbound-method | ||||||||||||||
const { emit } = 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 = ref(useHybridInject<SnippetConfig>('snippetConfig', baseSnippetConfig)); | ||||||||||||||
annacv marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
|
||||||||||||||
/** | ||||||||||||||
* Gets the provided preselected filters prioritizing the {@link SnippetConfig} over the | ||||||||||||||
* filters prop. | ||||||||||||||
* | ||||||||||||||
* @returns An array of filter's ids. | ||||||||||||||
*/ | ||||||||||||||
const preselectedFilters: ComputedRef<string[]> = computed(() => { | ||||||||||||||
annacv marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
return snippetConfig.value?.filters ?? props.filters; | ||||||||||||||
}); | ||||||||||||||
|
||||||||||||||
/** | ||||||||||||||
* Emits the {@link FacetsXEvents.PreselectedFiltersProvided} to save | ||||||||||||||
* the provided filters in the state. | ||||||||||||||
*/ | ||||||||||||||
const emitPreselectedFilters = (): void => { | ||||||||||||||
if (!isArrayEmpty(preselectedFilters.value)) { | ||||||||||||||
emit('PreselectedFiltersProvided', createRawFilters(preselectedFilters.value)); | ||||||||||||||
} | ||||||||||||||
}; | ||||||||||||||
|
||||||||||||||
/** | ||||||||||||||
* Emits the {@link FacetsXEvents.PreselectedFiltersProvided} when the | ||||||||||||||
* computed prop changes. | ||||||||||||||
*/ | ||||||||||||||
watch(preselectedFilters, emitPreselectedFilters); | ||||||||||||||
|
||||||||||||||
/** | ||||||||||||||
* Emits the {@link FacetsXEvents.PreselectedFiltersProvided} when the | ||||||||||||||
* component is mounted. | ||||||||||||||
*/ | ||||||||||||||
onMounted(() => { | ||||||||||||||
emitPreselectedFilters(); | ||||||||||||||
}); | ||||||||||||||
annacv marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
|
||||||||||||||
return () => useNoElementRender(slots); | ||||||||||||||
} | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, if I'm not wrong render is only available if we use the Options API, for the Composition API we should use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||||||||||||||
|
||||||||||||||
/** | ||||||||||||||
* Emits the {@link FacetsXEvents.PreselectedFiltersProvided} when the | ||||||||||||||
* component is created. | ||||||||||||||
*/ | ||||||||||||||
created(): void { | ||||||||||||||
this.emitPreselectedFilters(); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||||||||||||||
render(): void {} | ||||||||||||||
} | ||||||||||||||
}); | ||||||||||||||
</script> | ||||||||||||||
|
||||||||||||||
<docs lang="mdx"> | ||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you don't care what the metadata is in the tests, you can use
expect.any(Object)
.Remove this and use that instead.