Skip to content

Commit 96a490b

Browse files
committed
feat(presets): Allow selecting props using other props in presets
1 parent 8b76cdb commit 96a490b

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

Diff for: packages/ui/src/services/component-config/types.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { VuesticComponentsMap } from '../vue-plugin'
22
import type { VNodeProps, AllowedComponentProps, HTMLAttributes, VNode, DefineComponent } from 'vue'
33
import { ComponentSlots } from '../../utils/component-options'
4+
import { ObjectOrGetter } from '../../utils/types/object-or-getter'
45

56
export type VuesticComponentName = keyof VuesticComponentsMap
67
export type VueDefaultPropNames = keyof (VNodeProps & AllowedComponentProps) | `on${string}`
@@ -34,11 +35,11 @@ type VuesticComponentSlotPropsMap = {
3435
}
3536
}
3637

37-
type VuesticComponentPreset<T extends VuesticComponentName> = VuesticComponentPropsMap[T] & VuesticComponentSlotPropsMap[T]
38+
export type VuesticComponentPresetProps<T extends VuesticComponentName> = VuesticComponentPropsMap[T] & VuesticComponentSlotPropsMap[T]
3839

3940
export type Presets = {
4041
[componentName in VuesticComponentName]?: {
41-
[presetName: string]: VuesticComponentPreset<componentName>
42+
[presetName: string]: ObjectOrGetter<VuesticComponentPresetProps<componentName>, VuesticComponentPropsMap[componentName]>
4243
}
4344
}
4445

Diff for: packages/ui/src/services/component-config/utils/use-component-config-props.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { injectChildPropsFromParent } from '../../../composables/useChildCompone
66
import { ComponentPresetProp, PresetPropValue } from '../../../composables'
77
import { notNil } from '../../../utils/isNilValue'
88
import { head } from 'lodash'
9+
import { getObject } from '../../../utils/object-or-getter'
910

1011
const withPresetProp = <P extends Props>(props: P): props is P & ComponentPresetProp => 'preset' in props
1112
const getPresetProp = <P extends Props>(props: P) => withPresetProp(props) ? props.preset : undefined
@@ -18,12 +19,14 @@ export const useComponentConfigProps = <T extends VuesticComponent>(component: T
1819

1920
const getPresetProps = (presetPropValue: PresetPropValue): Props => {
2021
return (presetPropValue instanceof Array ? presetPropValue : [presetPropValue]).reduce<Props>((acc, presetName) => {
21-
const presetProps = globalConfig.value.components?.presets?.[componentName]?.[presetName]
22+
const preset = globalConfig.value.components?.presets?.[componentName]?.[presetName]
2223

23-
if (!presetProps) {
24+
if (!preset) {
2425
return acc
2526
}
2627

28+
const presetProps = getObject(preset, originalProps)
29+
2730
const extendedPresets = getPresetProp(presetProps)
2831

2932
return {
@@ -51,8 +54,8 @@ export const useComponentConfigProps = <T extends VuesticComponent>(component: T
5154
}, {})
5255

5356
const presetProp = head([
54-
originalProps,
5557
parentInjectedProps?.value,
58+
originalProps,
5659
localConfigProps,
5760
globalConfigProps,
5861
].filter(notNil).map(getPresetProp).filter(notNil))

Diff for: packages/ui/src/utils/object-or-getter.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { ObjectOrGetter } from './types/object-or-getter'
2+
3+
export const getObject = <T extends object, P>(objectOrGetter: ObjectOrGetter<T, P>, baseProps: P) => {
4+
if (typeof objectOrGetter === 'function') {
5+
return objectOrGetter(baseProps)
6+
}
7+
8+
return objectOrGetter
9+
}

Diff for: packages/ui/src/utils/types/object-or-getter.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type ObjectOrGetter<T extends object, P> = T | ((props: P) => T)

0 commit comments

Comments
 (0)