Skip to content

Commit

Permalink
fix(#281): Made Dropdowns always close
Browse files Browse the repository at this point in the history
  • Loading branch information
lxgr-linux committed Mar 18, 2024
1 parent 3cf7dee commit 93ffa87
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/components/elements/Dropdown/Dropdown.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,18 @@
</template>

<script setup lang="ts" generic="T">
import { ref } from "vue";
import { ref, watch } from "vue";
import {
ButtonType,
getButtonColor,
getTextColor,
} from "@/components/elements/CCButton/ButtonType";
import { useDropdown } from "@/def-composables/useDropdown";
const model = defineModel<T>();
const isOpen = ref(false);
const thisCounter = ref(0);
const { openCounter, incCounter } = useDropdown();
const props = withDefaults(
defineProps<{
Expand All @@ -56,7 +59,7 @@ const props = withDefaults(
initial: "?",
displayFn: (v: T): string => "" + v,
type: ButtonType.PUSSYRED,
}
},
);
const displayButton = () => {
Expand All @@ -67,8 +70,18 @@ const displayButton = () => {
const toggleDropdown = () => {
isOpen.value = !isOpen.value;
if (isOpen.value) {
incCounter();
thisCounter.value = openCounter.value;
}
};
watch(openCounter, (cur) => {
if (cur != thisCounter.value) {
isOpen.value = false;
}
});
const selectOption = (option: T) => {
model.value = option;
};
Expand Down
13 changes: 13 additions & 0 deletions src/def-composables/useDropdown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {ref, type Ref} from "vue";

const openCounter: Ref<number> = ref(0)

const incCounter = () => {
openCounter.value++
}

export const useDropdown = () => {
return {
openCounter, incCounter
}
}

0 comments on commit 93ffa87

Please sign in to comment.