-
Notifications
You must be signed in to change notification settings - Fork 291
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
86 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Ref, onMounted, onBeforeUnmount } from 'vue'; | ||
|
||
export default function ( | ||
refs: Ref[], | ||
isOpen: Ref<boolean>, | ||
toggleChange: (isOpen: boolean) => void | ||
): void { | ||
function onGlobalMouseDown(e: MouseEvent) { | ||
let target = e.target as HTMLElement; | ||
|
||
/** | ||
* TODO: 需要去了解下shadow DOM | ||
*/ | ||
if (target.shadowRoot && e.composed) { | ||
target = (e.composedPath()[0] || target) as HTMLElement; | ||
} | ||
|
||
const element = [refs[0]?.value, refs[1]?.value]; | ||
if ( | ||
isOpen.value && | ||
element.every((el) => el && !el.contains(target) && el !== target) | ||
) { | ||
toggleChange(false); | ||
} | ||
} | ||
|
||
onMounted(() => { | ||
document.body.addEventListener('mousedown', onGlobalMouseDown, false); | ||
}); | ||
|
||
onBeforeUnmount(() => { | ||
document.body.addEventListener('mousedown', onGlobalMouseDown, false); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<template> | ||
<d-select v-model="value" :options="options" :multiple="true" /> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import { defineComponent, reactive, ref } from 'vue' | ||
export default defineComponent({ | ||
setup () { | ||
const value = ref([]) | ||
const options = reactive([1,2,3]) | ||
return { | ||
value, | ||
options | ||
} | ||
}, | ||
}) | ||
</script> |