<script setup lang="ts">
import { computed, inject, provide, watchEffect, type PropType } from 'vue'
import { ElConfigProvider, configProviderProps, type ConfigProviderContext, type ConfigProviderPropsPublic } from 'element-plus'
import type { Language } from 'element-plus/es/locale'
import { useLocale, componentConfigKey, mergeComponentConfig, type ComponentConfig } from '@94ai-plus/common-locale'
import { nfComponentConfigProps } from './component-config'
import { elementPlusLocales } from './element-locales'
import type { ThemeName } from '@94ai-plus/common-constants'
import { mergeNfButtonConfig, nfButtonConfigKey, type ButtonProps } from '@94ai-plus/nf-button'
defineOptions({ name: 'NfConfigProvider' })
// Reuse Element Plus's public prop definitions, validators and defaults rather
// than maintaining a second list that can drift when Element Plus is upgraded.
const props = defineProps({
...configProviderProps,
locale: { type: [String, Object] as PropType<string | Language> },
elementPlusLocale: configProviderProps.locale,
theme: { type: String as PropType<ThemeName>, default: 'chalk' },
nfButton: { type: Object as PropType<ButtonProps> },
...nfComponentConfigProps,
})
const emit = defineEmits<{
localeChange: [locale: string]
themeChange: [theme: ThemeName]
}>()
defineSlots<{ default(props: { config: ConfigProviderContext | undefined }): unknown }>()
const parentConfig = inject(componentConfigKey, computed<ComponentConfig>(() => ({})))
provide(componentConfigKey, computed(() => mergeComponentConfig(parentConfig.value, {
nfAction: props.nfAction,
nfAlert: props.nfAlert,
nfAudio: props.nfAudio,
nfConditions: props.nfConditions,
nfConfirm: props.nfConfirm,
nfDatePicker: props.nfDatePicker,
nfDrawer: props.nfDrawer,
nfEmpty: props.nfEmpty,
nfForm: props.nfForm,
nfFormItem: props.nfFormItem,
nfImportTemplate: props.nfImportTemplate,
nfIndicatorPlane: props.nfIndicatorPlane,
nfInfiniteMenu: props.nfInfiniteMenu,
nfKeywords: props.nfKeywords,
nfLazySelect: props.nfLazySelect,
nfMessage: props.nfMessage,
nfNotification: props.nfNotification,
nfPopupLazySelect: props.nfPopupLazySelect,
nfRadioGroup: props.nfRadioGroup,
nfResizablePanels: props.nfResizablePanels,
nfRules: props.nfRules,
nfSearchForm: props.nfSearchForm,
nfSoftphone: props.nfSoftphone,
nfTable: props.nfTable,
nfTag: props.nfTag,
nfTextOverflow: props.nfTextOverflow,
nfTransferSelect: props.nfTransferSelect,
nfVariableSelect: props.nfVariableSelect,
nfVirtualSelect: props.nfVirtualSelect,
nfHalfYearPicker: props.nfHalfYearPicker,
nfHalfYearRangePicker: props.nfHalfYearRangePicker,
nfWeekRangePicker: props.nfWeekRangePicker,
})))
const localeContext = useLocale()
const parentButtonConfig = inject(nfButtonConfigKey, computed<ButtonProps>(() => ({})))
provide(nfButtonConfigKey, computed(() => mergeNfButtonConfig(parentButtonConfig.value, props.nfButton)))
watchEffect(() => {
if (typeof props.locale === 'string' && props.locale && props.locale !== localeContext.locale.value) {
localeContext.setLocale(props.locale)
emit('localeChange', props.locale)
}
})
watchEffect(() => {
if (typeof document !== 'undefined') {
document.documentElement.dataset.nfTheme = props.theme
emit('themeChange', props.theme)
}
})
const elementConfig = computed(() => {
const config: Record<string, unknown> = {}
for (const key of Object.keys(configProviderProps) as (keyof typeof configProviderProps)[]) {
config[key] = key === 'locale'
? props.elementPlusLocale ?? (typeof props.locale === 'object' ? props.locale : props.locale ? elementPlusLocales[props.locale] : undefined)
: props[key]
}
return config as ConfigProviderPropsPublic
})
</script>
<template>
<ElConfigProvider v-slot="slotProps" v-bind="elementConfig">
<slot v-bind="slotProps" />
</ElConfigProvider>
</template>