fix(hooks): cannot access 'useEmptyValuesProps' before initialization (#17448)

This commit is contained in:
qiang 2024-07-09 19:02:05 +08:00 committed by GitHub
parent fc15cad898
commit 10ae88b73c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 8 deletions

View File

@ -4,6 +4,7 @@ import {
SIZE_INJECTION_KEY,
defaultInitialZIndex,
defaultNamespace,
emptyValuesContextKey,
localeContextKey,
namespaceContextKey,
useLocale,
@ -111,6 +112,14 @@ export const provideGlobalConfig = (
size: computed(() => context.value.size || ''),
})
provideFn(
emptyValuesContextKey,
computed(() => ({
emptyValues: context.value.emptyValues,
valueOnClear: context.value.valueOnClear,
}))
)
if (global || !globalConfig.value) {
globalConfig.value = context.value
}

View File

@ -1,9 +1,12 @@
import { computed, ref } from 'vue'
import { useGlobalConfig } from '@element-plus/components/config-provider'
import { computed, getCurrentInstance, inject, ref } from 'vue'
import { buildProps, debugWarn, isFunction } from '@element-plus/utils'
import type { ExtractPropTypes } from 'vue'
import type { ExtractPropTypes, InjectionKey, Ref } from 'vue'
type EmptyValuesContext = ExtractPropTypes<typeof useEmptyValuesProps>
export const emptyValuesContextKey: InjectionKey<Ref<EmptyValuesContext>> =
Symbol('emptyValuesContextKey')
export const SCOPE = 'use-empty-values'
export const DEFAULT_EMPTY_VALUES = ['', undefined, null]
export const DEFAULT_VALUE_ON_CLEAR = undefined
@ -24,13 +27,12 @@ export const useEmptyValuesProps = buildProps({
} as const)
export const useEmptyValues = (
props: ExtractPropTypes<typeof useEmptyValuesProps>,
props: EmptyValuesContext,
defaultValue?: null | undefined
) => {
let config = useGlobalConfig()
if (!config.value) {
config = ref({})
}
const config = getCurrentInstance()
? inject(emptyValuesContextKey, ref<EmptyValuesContext>({}))
: ref<EmptyValuesContext>({})
const emptyValues = computed(
() => props.emptyValues || config.value.emptyValues || DEFAULT_EMPTY_VALUES