element-plus/packages/utils/props.ts
三咲智子 0327d2d0b2
refactor(components): refactor button (#3341)
* feat(utils): add buildProp

* chore: disable eslint rule

* chore: typescript use strict

* refactor(components): refactor button

* feat: add comment for buildProp
2021-09-12 19:38:48 +08:00

67 lines
1.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import type { PropType } from 'vue'
/**
* @description Build prop. It can better optimize prop types
* @description 生成 prop能更好地优化类型
* @example
// limited options
// the type will be PropType<'light' | 'dark'>
buildProp({
type: String,
values: ['light', 'dark'],
} as const)
* @example
// limited options and other types
// the type will be PropType<'small' | 'medium' | number>
buildProp({
type: [String, Number],
values: ['small', 'medium'],
validator: (val: unknown): val is number => typeof val === 'number',
} as const)
@link for more explanation: https://github.com/element-plus/element-plus/pull/3341
*/
export function buildProp<
T = any,
R extends boolean = boolean,
D extends T = T,
C = never
>({
values,
required,
default: defaultValue,
type,
validator,
}: {
values?: readonly T[]
required?: R
default?: R extends true
? never
: D extends Record<string, unknown> | Array<any>
? () => D
: D
type?: any
validator?: ((val: any) => val is C) | ((val: any) => boolean)
} = {}) {
return {
type: type as PropType<T | C>,
required: !!required,
default: defaultValue,
validator:
values || validator
? (val: unknown) => {
let vaild = false
if (values)
vaild ||= ([...values, defaultValue] as unknown[]).includes(val)
if (validator) vaild ||= validator(val)
return vaild
}
: undefined,
} as const
}
export function keyOf<T>(arr: T) {
return Object.keys(arr) as Array<keyof T>
}
export const componentSize = ['large', 'medium', 'small', 'mini'] as const