Skip to content

Commit

Permalink
feat(icon): init icon name type
Browse files Browse the repository at this point in the history
  • Loading branch information
m0ksem committed Nov 29, 2023
1 parent 1c1de24 commit 9200429
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 7 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 @@ -19,6 +19,8 @@
<script lang="ts">
import { defineComponent, PropType, computed } from 'vue'
import omit from 'lodash/omit.js'
import { AnyStringPropType } from '../../utils/types/prop-type'
import { VaIconName } from './types'
import {
useComponentPresetProp,
Expand All @@ -32,7 +34,7 @@ export default defineComponent({
props: {
...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'
6 changes: 6 additions & 0 deletions packages/ui/src/components/va-icon/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { VuesticIconAliases } from './../../services/icon/presets/vuestic-aliases'
import type { ExtractIconAliasesName } from '../../services/icon/types/define-aliases'

type DefaultIconAliases = ExtractIconAliasesName<typeof VuesticIconAliases>

export type VaIconName = DefaultIconAliases
6 changes: 3 additions & 3 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-arrow-first',
to: 'mi-first_page',
Expand Down Expand Up @@ -57,4 +57,4 @@ export const VuesticIconAliases: IconConfiguration[] = [
name: 'va-loading',
to: 'mi-loop',
},
]
])
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 9200429

Please sign in to comment.