-
Notifications
You must be signed in to change notification settings - Fork 341
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ability to apply multiple presets (#4303)
* feat(presets): Added ability to apply multiple presets * chore: remove lodash fn * chore: remove RemoveIndex type from globalls --------- Co-authored-by: Maksim Nedoshev <[email protected]>
- Loading branch information
Showing
14 changed files
with
161 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
packages/docs/page-config/services/components-config/code/components-presets-multiple.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
createVuestic({ | ||
components: { | ||
presets: { | ||
VaButton: { | ||
addToCart: { round: true, color: 'success', icon: 'shopping_cart', 'slot:default': 'Add to card' }, | ||
promotion: { gradient: true, color: 'primary' } | ||
}, | ||
}, | ||
}, | ||
}) |
5 changes: 5 additions & 0 deletions
5
packages/docs/page-config/services/components-config/examples/presets-multiple.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<template> | ||
<VaButton | ||
:preset="['addToCart', 'promotion']" | ||
/> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,8 @@ | |
"types": [ | ||
"vite/client", | ||
] | ||
} | ||
}, | ||
"exclude": [ | ||
"page-config/**/**/code/*.ts", | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,12 @@ | ||
import { PropType, ExtractPropTypes } from 'vue' | ||
|
||
export type PresetPropValue = string | string[]; | ||
|
||
export const useComponentPresetProp = { | ||
preset: { | ||
type: String, | ||
type: [String, Array] as PropType<PresetPropValue>, | ||
default: undefined, | ||
}, | ||
} | ||
|
||
export type ComponentPresetProp = ExtractPropTypes<typeof useComponentPresetProp> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,79 @@ | ||
import type { VuesticComponentsMap } from '../vue-plugin' | ||
import type { VNodeProps, AllowedComponentProps, HTMLAttributes } from 'vue' | ||
import type { VNodeProps, AllowedComponentProps, HTMLAttributes, VNode, DefineComponent } from 'vue' | ||
import { ComponentSlots } from '../../utils/component-options' | ||
|
||
export type VuesticComponentName = keyof VuesticComponentsMap | ||
export type VueDefaultPropNames = keyof (VNodeProps & AllowedComponentProps) | `on${string}` | ||
|
||
export type Props = { [propName: string]: any } | ||
export type Presets = { [componentName in VuesticComponentName]?: { [presetName: string]: Props } } | ||
export type PropTypes<C> = C extends { new(): { $props: infer Props } } ? Omit<Props, VueDefaultPropNames> : never | ||
|
||
export type ComponentConfig = Partial<{ | ||
export type VuesticComponentPropsMap = { | ||
// key-value hack to avoid generics in type (like Omit, PropTypes, etc.) | ||
// `key: type` as result | ||
[componentName in VuesticComponentName]: { | ||
[key in keyof PropTypes<VuesticComponentsMap[componentName]>]?: PropTypes<VuesticComponentsMap[componentName]>[key] | ||
} & HTMLAttributes | ||
} & { all: Props, presets: Presets }> | ||
} | ||
|
||
export type Props = { [propName: string]: any } | ||
|
||
/** | ||
* Removes index signature from object | ||
* | ||
* @example | ||
* | ||
* Type: | ||
* ``` | ||
* RemoveIndex<{ | ||
* [index: string]: number | ||
* a: number | ||
* b: number | ||
* }> | ||
* ``` | ||
* Converts to | ||
* | ||
* ``` | ||
* { a: number, b: number } | ||
* ``` | ||
* | ||
*/ | ||
type RemoveIndex<T> = { | ||
[ K in keyof T as | ||
string extends K | ||
? never | ||
: number extends K | ||
? never | ||
: symbol extends K | ||
? never | ||
: K | ||
]: T[K]; | ||
} | ||
|
||
type VuesticComponentSlotsMap = { | ||
[componentName in VuesticComponentName]: { | ||
[key in keyof RemoveIndex<ComponentSlots<VuesticComponentsMap[componentName]>>]?: ComponentSlots<VuesticComponentsMap[componentName]>[key] | ||
} | ||
} | ||
|
||
type SlotPropPrefix<T extends string> = `slot:${T}` | ||
|
||
export type SlotProp<Scope> = VNode | string | DefineComponent<Partial<Scope>, {}, {}, {}, {}> | ||
|
||
type VuesticComponentSlotPropsMap = { | ||
[componentName in VuesticComponentName]: { | ||
// @ts-ignore | ||
[key in keyof VuesticComponentSlotsMap[componentName] as SlotPropPrefix<key>]: SlotProp<Parameters<VuesticComponentSlotsMap[componentName][key]>[0]> | ||
} | ||
} | ||
|
||
type VuesticComponentPreset<T extends VuesticComponentName> = VuesticComponentPropsMap[T] & VuesticComponentSlotPropsMap[T] | ||
|
||
export type Presets = { | ||
[componentName in VuesticComponentName]?: { | ||
[presetName: string]: VuesticComponentPreset<componentName> | ||
} | ||
} | ||
|
||
export type ComponentConfig = Partial<VuesticComponentPropsMap & { all: Props, presets: Presets }> | ||
|
||
export type { DefineComponent as VuesticComponent } from 'vue' |
61 changes: 48 additions & 13 deletions
61
packages/ui/src/services/component-config/utils/use-component-config-props.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,18 @@ | ||
const nilValues = [null, undefined, '' as const] | ||
const nullOrUndefined = [null, undefined] | ||
|
||
/** | ||
* Checks if provided value not exists. | ||
* | ||
* @param value any value to check it. | ||
*/ | ||
export const isNilValue = (value: any): value is null | undefined | '' => { | ||
return [null, undefined, ''].includes(value) | ||
// lodash `isNil` isn't an alternative, because we also want to handle empty string values | ||
return nilValues.includes(value) | ||
} | ||
|
||
export const notNil = <T>(value: T): value is NonNullable<T> => !isNilValue(value) | ||
|
||
export const isNil = (value: any): value is null | undefined => { | ||
return [null, undefined].includes(value) | ||
return nullOrUndefined.includes(value) | ||
} |