Skip to content

Commit

Permalink
feat(icon): icon name extendable type (#4045)
Browse files Browse the repository at this point in the history
  • Loading branch information
m0ksem committed Aug 26, 2024
1 parent 5acf074 commit 16776ff
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 9 deletions.
4 changes: 3 additions & 1 deletion packages/ui/src/components/va-icon/VaIcon.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

<script lang="ts" setup>
import { PropType, computed, useAttrs } from 'vue'
import { AnyStringPropType } from '../../utils/types/prop-type'
import { VaIconName } from './types'
import {
useComponentPresetProp,
Expand All @@ -34,7 +36,7 @@ defineOptions({
const props = defineProps({
...useSizeProps,
...useComponentPresetProp,
name: { type: String, default: '' },
name: { type: String as AnyStringPropType<VaIconName>, default: '' },
tag: { type: String },
component: { type: Object as PropType<any> },
color: { type: String },
Expand Down
2 changes: 2 additions & 0 deletions packages/ui/src/components/va-icon/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ import withConfigTransport from '../../services/config-transport/withConfigTrans
import _VaIcon from './VaIcon.vue'

export const VaIcon = withConfigTransport(_VaIcon)

export * from './types'
23 changes: 23 additions & 0 deletions packages/ui/src/components/va-icon/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { VuesticIconAliases } from './../../services/icon/presets/vuestic-aliases'
import type { ExtractIconAliasesName } from '../../services/icon/types/define-aliases'

type DefaultIconAliases = ExtractIconAliasesName<typeof VuesticIconAliases>

/**
* This is a placeholder meant to be implemented via TypeScript's Module
* Augmentation feature to allow key type inference
*
* @example
* ```ts
* declare module 'vuestic-ui' {
* interface CustomIconAliasesNKeys {
* from: string
* }
* }
* ```
*
* @see https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation
*/
export interface CustomIconAliasesNKeys {}

export type VaIconName = DefaultIconAliases | keyof CustomIconAliasesNKeys
10 changes: 5 additions & 5 deletions packages/ui/src/services/icon/presets/vuestic-aliases.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IconConfiguration } from '../types'
import { defineIconAliases } from '../types/define-aliases'

export const VuesticIconAliases: IconConfiguration[] = [
export const VuesticIconAliases = defineIconAliases([
{
name: 'va-unsorted',
to: 'swap_vert',
Expand Down Expand Up @@ -71,10 +71,10 @@ export const VuesticIconAliases: IconConfiguration[] = [
},
{
name: 'va-plus',
to: 'add',
to: 'mi-add',
},
{
name: 'va-minus',
to: 'remove',
to: 'mi-remove',
},
]
])
6 changes: 3 additions & 3 deletions packages/ui/src/services/icon/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export interface IconProps {
to?: string
}

export interface IconConfigurationString extends IconProps {
name: string
export interface IconConfigurationString<Name extends string = string> extends IconProps {
name: Name
resolve?: ((dynamicSegments: {[dynamicSegment: string]: string }) => IconProps)
}

Expand All @@ -25,7 +25,7 @@ export interface IconConfigurationRegex extends IconProps {
resolveFromRegex?: ((...regexGroupValues: string[]) => IconProps)
}

export type IconConfiguration = IconConfigurationString | IconConfigurationRegex
export type IconConfiguration<Name extends string = string> = IconConfigurationString<Name> | IconConfigurationRegex

export type IconConfig = IconConfiguration[]

Expand Down
9 changes: 9 additions & 0 deletions packages/ui/src/services/icon/types/define-aliases.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { IconConfiguration } from '../types'

export const defineIconAliases = <Name extends string>(aliases: IconConfiguration<Name>[]) => aliases

export type ExtractIconAliasesName<T> = T extends IconConfiguration<infer Name>
? Name
: T extends IconConfiguration<infer Name>[] ?
Name
: never

0 comments on commit 16776ff

Please sign in to comment.