Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(icon): icon name extendable type #4045

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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" setup>
import { PropType, computed, useAttrs } from 'vue'
import omit from 'lodash/omit.js'
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'
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
11 changes: 5 additions & 6 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 @@ -59,11 +59,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
Loading