2021-09-10 10:00:44 +08:00
|
|
|
import { getCurrentInstance, computed } from 'vue'
|
2021-09-10 15:00:39 +08:00
|
|
|
import { debugWarn } from '@element-plus/utils/error'
|
2021-09-10 10:00:44 +08:00
|
|
|
|
|
|
|
import type { ComputedRef } from 'vue'
|
2020-09-19 20:44:07 +08:00
|
|
|
|
2020-12-07 10:57:48 +08:00
|
|
|
interface Params {
|
|
|
|
excludeListeners?: boolean
|
|
|
|
excludeKeys?: string[]
|
|
|
|
}
|
|
|
|
|
2020-09-19 20:44:07 +08:00
|
|
|
const DEFAULT_EXCLUDE_KEYS = ['class', 'style']
|
|
|
|
const LISTENER_PREFIX = /^on[A-Z]/
|
|
|
|
|
2021-09-10 10:00:44 +08:00
|
|
|
export default (params: Params = {}): ComputedRef<Record<string, unknown>> => {
|
2020-12-07 10:57:48 +08:00
|
|
|
const { excludeListeners = false, excludeKeys = [] } = params
|
2020-09-19 20:44:07 +08:00
|
|
|
const allExcludeKeys = excludeKeys.concat(DEFAULT_EXCLUDE_KEYS)
|
|
|
|
|
2021-09-10 10:00:44 +08:00
|
|
|
const instance = getCurrentInstance()
|
|
|
|
if (!instance) {
|
2021-09-10 15:00:39 +08:00
|
|
|
debugWarn(
|
2021-09-10 10:00:44 +08:00
|
|
|
'use-attrs',
|
|
|
|
'getCurrentInstance() returned null. useAttrs() must be called at the top of a setup function'
|
|
|
|
)
|
|
|
|
return computed(() => ({}))
|
|
|
|
}
|
|
|
|
|
|
|
|
return computed(() =>
|
|
|
|
Object.fromEntries(
|
|
|
|
Object.entries(instance.proxy?.$attrs).filter(
|
|
|
|
([key]) =>
|
|
|
|
!allExcludeKeys.includes(key) &&
|
|
|
|
!(excludeListeners && LISTENER_PREFIX.test(key))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2020-09-19 20:44:07 +08:00
|
|
|
}
|