forked from element-plus/element-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
43 lines (35 loc) · 1.01 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import {
getCurrentInstance,
reactive,
shallowRef,
watchEffect,
} from 'vue'
import { entries } from '@element-plus/utils/util'
interface Params {
excludeListeners?: boolean
excludeKeys?: string[]
}
const DEFAULT_EXCLUDE_KEYS = ['class', 'style']
const LISTENER_PREFIX = /^on[A-Z]/
export default (params: Params = {}) => {
const { excludeListeners = false, excludeKeys = [] } = params
const instance = getCurrentInstance()
const attrs = shallowRef({})
const allExcludeKeys = excludeKeys.concat(DEFAULT_EXCLUDE_KEYS)
// Since attrs are not reactive, make it reactive instead of doing in `onUpdated` hook for better performance
instance.attrs = reactive(instance.attrs)
watchEffect(() => {
const res = entries(instance.attrs)
.reduce((acm, [key, val]) => {
if (
!allExcludeKeys.includes(key) &&
!(excludeListeners && LISTENER_PREFIX.test(key))
) {
acm[key] = val
}
return acm
}, {})
attrs.value = res
})
return attrs
}