2021-09-12 19:38:48 +08:00
|
|
|
|
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)
|
2021-09-21 17:19:35 +08:00
|
|
|
|
@link see more: https://github.com/element-plus/element-plus/pull/3341
|
2021-09-12 19:38:48 +08:00
|
|
|
|
*/
|
|
|
|
|
export function buildProp<
|
|
|
|
|
T = any,
|
2021-09-17 15:07:32 +08:00
|
|
|
|
R extends boolean = false,
|
2021-09-12 19:38:48 +08:00
|
|
|
|
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)
|
|
|
|
|
} = {}) {
|
2021-09-17 15:07:32 +08:00
|
|
|
|
type DefaultType = typeof defaultValue
|
|
|
|
|
type HasDefaultValue = Exclude<T, D> extends never ? false : true
|
|
|
|
|
|
2021-09-12 19:38:48 +08:00
|
|
|
|
return {
|
|
|
|
|
type: type as PropType<T | C>,
|
2021-09-17 15:07:32 +08:00
|
|
|
|
required: !!required as R,
|
|
|
|
|
|
|
|
|
|
default: defaultValue as R extends true
|
|
|
|
|
? never
|
|
|
|
|
: HasDefaultValue extends true
|
|
|
|
|
? Exclude<DefaultType, undefined>
|
|
|
|
|
: undefined,
|
|
|
|
|
|
2021-09-12 19:38:48 +08:00
|
|
|
|
validator:
|
|
|
|
|
values || validator
|
|
|
|
|
? (val: unknown) => {
|
2021-09-17 15:07:32 +08:00
|
|
|
|
let valid = false
|
2021-09-12 19:38:48 +08:00
|
|
|
|
if (values)
|
2021-09-17 15:07:32 +08:00
|
|
|
|
valid ||= ([...values, defaultValue] as unknown[]).includes(val)
|
|
|
|
|
if (validator) valid ||= validator(val)
|
|
|
|
|
return valid
|
2021-09-12 19:38:48 +08:00
|
|
|
|
}
|
|
|
|
|
: undefined,
|
|
|
|
|
} as const
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-17 15:07:32 +08:00
|
|
|
|
export const keyOf = <T>(arr: T) => Object.keys(arr) as Array<keyof T>
|
2021-09-12 19:38:48 +08:00
|
|
|
|
|
|
|
|
|
export const componentSize = ['large', 'medium', 'small', 'mini'] as const
|