clicking outside of a card closes it

This commit is contained in:
undergroundwires
2020-05-24 17:56:14 +01:00
parent c668a97950
commit aab8f21a8d

View File

@@ -4,6 +4,7 @@
<CardListItem <CardListItem
class="card" class="card"
v-for="categoryId of categoryIds" v-for="categoryId of categoryIds"
:data-category="categoryId"
v-bind:key="categoryId" v-bind:key="categoryId"
:categoryId="categoryId" :categoryId="categoryId"
:activeCategoryId="activeCategoryId" :activeCategoryId="activeCategoryId"
@@ -32,6 +33,9 @@ export default class CardList extends StatefulVue {
public async mounted() { public async mounted() {
const state = await this.getCurrentStateAsync(); const state = await this.getCurrentStateAsync();
this.setCategories(state.app.categories); this.setCategories(state.app.categories);
this.onOutsideOfActiveCardClicked(() => {
this.activeCategoryId = null;
});
} }
public onSelected(categoryId: number, isExpanded: boolean) { public onSelected(categoryId: number, isExpanded: boolean) {
@@ -41,7 +45,21 @@ export default class CardList extends StatefulVue {
private setCategories(categories: ReadonlyArray<ICategory>): void { private setCategories(categories: ReadonlyArray<ICategory>): void {
this.categoryIds = categories.map((category) => category.id); this.categoryIds = categories.map((category) => category.id);
} }
private onOutsideOfActiveCardClicked(callback) {
const outsideClickListener = (event) => {
if (!this.activeCategoryId) {
return;
}
const element = document.querySelector(`[data-category="${this.activeCategoryId}"]`);
if (!element.contains(event.target)) {
callback();
}
};
document.addEventListener('click', outsideClickListener);
}
} }
</script> </script>
<style scoped lang="scss"> <style scoped lang="scss">