perf: 修改 use 为 hooks
This commit is contained in:
parent
dd8f37181a
commit
6374ad4805
@ -112,7 +112,7 @@ export default {
|
||||
:::
|
||||
|
||||
| Name | Description | Accepted Values |
|
||||
| ------------- | -------------- | ---------------- |
|
||||
| ------------- | -------------- | ---------------- | ------------ |
|
||||
| name | 原生 name 属性 | -- |
|
||||
| v-model | 是否启用 | `true` `false` |
|
||||
| disabled | 禁用 | `true` `false` |
|
||||
|
@ -3,25 +3,25 @@ import { ref, onMounted, onUnmounted, Ref } from 'vue'
|
||||
// 案例详见 dropdown.vue
|
||||
const useClickOutside = (elementRef: Ref<HTMLElement | null>) => {
|
||||
// 设置一个导出值
|
||||
const isClickOutside = ref(false);
|
||||
const isClickOutside = ref(false)
|
||||
// 给界面绑定上事件
|
||||
const handler = (e: MouseEvent) => {
|
||||
if (elementRef.value) {
|
||||
// e.target 有可能是为 null 所以需要断言
|
||||
if (elementRef.value.contains(e.target as HTMLElement)) {
|
||||
// 判断目标节点是不是当前的节点
|
||||
isClickOutside.value = false;
|
||||
isClickOutside.value = false
|
||||
} else {
|
||||
isClickOutside.value = true
|
||||
}
|
||||
}
|
||||
}
|
||||
onMounted(() => {
|
||||
document.addEventListener('click', handler);
|
||||
});
|
||||
document.addEventListener('click', handler)
|
||||
})
|
||||
onUnmounted(() => {
|
||||
document.removeEventListener('click', handler);
|
||||
});
|
||||
return isClickOutside;
|
||||
document.removeEventListener('click', handler)
|
||||
})
|
||||
return isClickOutside
|
||||
}
|
||||
export default useClickOutside;
|
||||
export default useClickOutside
|
||||
|
@ -1,8 +1,8 @@
|
||||
<template>
|
||||
<div
|
||||
v-if="trigger === 'click'"
|
||||
class="layui-dropdown"
|
||||
ref="dropdownRef"
|
||||
class="layui-dropdown"
|
||||
:class="[openState ? 'layui-dropdown-up' : '']"
|
||||
>
|
||||
<div @click="open">
|
||||
|
@ -21,5 +21,3 @@ const active = inject('active')
|
||||
const slotsChange: Ref<boolean> = inject('slotsChange') as Ref<boolean>
|
||||
slotsChange.value = !slotsChange.value
|
||||
</script>
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user