perf: 修改 use 为 hooks

This commit is contained in:
就眠仪式 2021-10-24 23:42:18 +08:00
parent dd8f37181a
commit 6374ad4805
5 changed files with 11 additions and 13 deletions

View File

@ -112,7 +112,7 @@ export default {
::: :::
| Name | Description | Accepted Values | | Name | Description | Accepted Values |
| ------------- | -------------- | ---------------- | | ------------- | -------------- | ---------------- | ------------ |
| name | 原生 name 属性 | -- | | name | 原生 name 属性 | -- |
| v-model | 是否启用 | `true` `false` | | v-model | 是否启用 | `true` `false` |
| disabled | 禁用 | `true` `false` | | disabled | 禁用 | `true` `false` |

View File

@ -3,25 +3,25 @@ import { ref, onMounted, onUnmounted, Ref } from 'vue'
// 案例详见 dropdown.vue // 案例详见 dropdown.vue
const useClickOutside = (elementRef: Ref<HTMLElement | null>) => { const useClickOutside = (elementRef: Ref<HTMLElement | null>) => {
// 设置一个导出值 // 设置一个导出值
const isClickOutside = ref(false); const isClickOutside = ref(false)
// 给界面绑定上事件 // 给界面绑定上事件
const handler = (e: MouseEvent) => { const handler = (e: MouseEvent) => {
if (elementRef.value) { if (elementRef.value) {
// e.target 有可能是为 null 所以需要断言 // e.target 有可能是为 null 所以需要断言
if (elementRef.value.contains(e.target as HTMLElement)) { if (elementRef.value.contains(e.target as HTMLElement)) {
// 判断目标节点是不是当前的节点 // 判断目标节点是不是当前的节点
isClickOutside.value = false; isClickOutside.value = false
} else { } else {
isClickOutside.value = true isClickOutside.value = true
} }
} }
} }
onMounted(() => { onMounted(() => {
document.addEventListener('click', handler); document.addEventListener('click', handler)
}); })
onUnmounted(() => { onUnmounted(() => {
document.removeEventListener('click', handler); document.removeEventListener('click', handler)
}); })
return isClickOutside; return isClickOutside
} }
export default useClickOutside; export default useClickOutside

View File

@ -1,8 +1,8 @@
<template> <template>
<div <div
v-if="trigger === 'click'" v-if="trigger === 'click'"
class="layui-dropdown"
ref="dropdownRef" ref="dropdownRef"
class="layui-dropdown"
:class="[openState ? 'layui-dropdown-up' : '']" :class="[openState ? 'layui-dropdown-up' : '']"
> >
<div @click="open"> <div @click="open">

View File

@ -50,7 +50,7 @@ provide('selectItem', selectItem)
provide('openState', openState) provide('openState', openState)
// select update , change // select update , change
const emit = defineEmits(['update:modelValue','change']) const emit = defineEmits(['update:modelValue', 'change'])
watch(selectItem, function (item) { watch(selectItem, function (item) {
emit('change', item.value) emit('change', item.value)

View File

@ -21,5 +21,3 @@ const active = inject('active')
const slotsChange: Ref<boolean> = inject('slotsChange') as Ref<boolean> const slotsChange: Ref<boolean> = inject('slotsChange') as Ref<boolean>
slotsChange.value = !slotsChange.value slotsChange.value = !slotsChange.value
</script> </script>