|
1 |
| -import type { VuesticComponent, VuesticComponentName, Props } from '../types' |
| 1 | +import { VuesticComponentName, Props, VuesticComponent } from '../types' |
2 | 2 | import { useLocalConfig } from '../../../composables/useLocalConfig'
|
3 | 3 | import { useGlobalConfig } from '../../global-config/global-config'
|
4 | 4 | import { computed } from 'vue'
|
5 |
| -import { injectChildPresetPropFromParent } from '../../../composables/useChildComponents' |
| 5 | +import { injectChildPropsFromParent } from '../../../composables/useChildComponents' |
| 6 | +import { ComponentPresetProp, PresetPropValue } from '../../../composables' |
| 7 | +import { notNil } from '../../../utils/isNilValue' |
| 8 | +import { head } from 'lodash' |
| 9 | + |
| 10 | +const withPresetProp = <P extends Props>(props: P): props is P & ComponentPresetProp => 'preset' in props |
| 11 | +const getPresetProp = <P extends Props>(props: P) => withPresetProp(props) ? props.preset : undefined |
6 | 12 |
|
7 | 13 | export const useComponentConfigProps = <T extends VuesticComponent>(component: T, originalProps: Props) => {
|
8 | 14 | const localConfig = useLocalConfig()
|
9 | 15 | const { globalConfig } = useGlobalConfig()
|
10 | 16 |
|
11 |
| - const instancePreset = computed(() => originalProps.preset) |
12 |
| - const getPresetProps = (presetName: string) => globalConfig.value.components?.presets?.[component.name as VuesticComponentName]?.[presetName] |
13 |
| - const parentPropPreset = injectChildPresetPropFromParent() |
| 17 | + const componentName = component.name as VuesticComponentName |
| 18 | + |
| 19 | + const getPresetProps = (presetPropValue: PresetPropValue): Props => { |
| 20 | + return (presetPropValue instanceof Array ? presetPropValue : [presetPropValue]).reduce<Props>((acc, presetName) => { |
| 21 | + const presetProps = globalConfig.value.components?.presets?.[componentName]?.[presetName] |
| 22 | + |
| 23 | + if (!presetProps) { |
| 24 | + return acc |
| 25 | + } |
| 26 | + |
| 27 | + const extendedPresets = getPresetProp(presetProps) |
| 28 | + |
| 29 | + return { |
| 30 | + ...acc, |
| 31 | + ...(extendedPresets ? getPresetProps(extendedPresets) : undefined), |
| 32 | + ...presetProps, |
| 33 | + } |
| 34 | + }, {}) |
| 35 | + } |
| 36 | + const parentInjectedProps = injectChildPropsFromParent() |
14 | 37 |
|
15 | 38 | return computed(() => {
|
16 | 39 | const globalConfigProps: Props = {
|
17 | 40 | ...globalConfig.value.components?.all,
|
18 |
| - ...globalConfig.value.components?.[component.name as VuesticComponentName], |
| 41 | + ...globalConfig.value.components?.[componentName], |
19 | 42 | }
|
20 | 43 |
|
21 |
| - const localConfigProps: Props = localConfig.value |
22 |
| - .reduce((finalConfig, config) => config[component.name as VuesticComponentName] |
23 |
| - ? { ...finalConfig, ...config[component.name as VuesticComponentName] } |
24 |
| - : finalConfig |
25 |
| - , {}) |
| 44 | + const localConfigProps = localConfig.value |
| 45 | + .reduce<Props>((finalConfig, config) => { |
| 46 | + const componentConfigProps = config[componentName] |
| 47 | + |
| 48 | + return componentConfigProps |
| 49 | + ? { ...finalConfig, ...componentConfigProps } |
| 50 | + : finalConfig |
| 51 | + }, {}) |
26 | 52 |
|
27 |
| - const presetName = parentPropPreset?.value || instancePreset.value || localConfigProps.preset || globalConfigProps.preset |
28 |
| - const presetProps = presetName && getPresetProps(presetName) |
| 53 | + const presetProp = head([ |
| 54 | + originalProps, |
| 55 | + parentInjectedProps?.value, |
| 56 | + localConfigProps, |
| 57 | + globalConfigProps, |
| 58 | + ].filter(notNil).map(getPresetProp).filter(notNil)) |
| 59 | + const presetProps = presetProp ? getPresetProps(presetProp) : undefined |
29 | 60 |
|
30 | 61 | return { ...globalConfigProps, ...localConfigProps, ...presetProps }
|
31 | 62 | })
|
|
0 commit comments