2021-10-15 18:24:49 +08:00
|
|
|
|
import { warn } from 'vue'
|
2021-10-06 19:56:24 +08:00
|
|
|
|
import { isObject } from '@vue/shared'
|
2022-02-08 10:37:21 +08:00
|
|
|
|
import { fromPairs } from 'lodash-unified'
|
2021-09-22 20:27:23 +08:00
|
|
|
|
import type { ExtractPropTypes, PropType } from '@vue/runtime-core'
|
2021-09-22 01:19:04 +08:00
|
|
|
|
import type { Mutable } from './types'
|
2021-09-22 20:27:23 +08:00
|
|
|
|
|
|
|
|
|
const wrapperKey = Symbol()
|
|
|
|
|
export type PropWrapper<T> = { [wrapperKey]: T }
|
|
|
|
|
|
2022-01-04 09:15:15 +08:00
|
|
|
|
export const propKey = '__elPropsReservedKey'
|
2021-10-06 19:56:24 +08:00
|
|
|
|
|
2021-09-22 20:27:23 +08:00
|
|
|
|
type ResolveProp<T> = ExtractPropTypes<{
|
|
|
|
|
key: { type: T; required: true }
|
|
|
|
|
}>['key']
|
|
|
|
|
type ResolvePropType<T> = ResolveProp<T> extends { type: infer V }
|
|
|
|
|
? V
|
|
|
|
|
: ResolveProp<T>
|
|
|
|
|
type ResolvePropTypeWithReadonly<T> = Readonly<T> extends Readonly<
|
|
|
|
|
Array<infer A>
|
|
|
|
|
>
|
|
|
|
|
? ResolvePropType<A[]>
|
|
|
|
|
: ResolvePropType<T>
|
2021-10-08 14:58:48 +08:00
|
|
|
|
|
|
|
|
|
type IfUnknown<T, V> = [unknown] extends [T] ? V : T
|
2021-09-12 19:38:48 +08:00
|
|
|
|
|
2021-10-06 19:56:24 +08:00
|
|
|
|
export type BuildPropOption<T, D extends BuildPropType<T, V, C>, R, V, C> = {
|
2021-09-29 09:52:58 +08:00
|
|
|
|
type?: T
|
|
|
|
|
values?: readonly V[]
|
|
|
|
|
required?: R
|
|
|
|
|
default?: R extends true
|
|
|
|
|
? never
|
|
|
|
|
: D extends Record<string, unknown> | Array<any>
|
|
|
|
|
? () => D
|
2021-10-08 14:58:48 +08:00
|
|
|
|
: (() => D) | D
|
2021-09-29 09:52:58 +08:00
|
|
|
|
validator?: ((val: any) => val is C) | ((val: any) => boolean)
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-06 19:56:24 +08:00
|
|
|
|
type _BuildPropType<T, V, C> =
|
2021-09-29 09:52:58 +08:00
|
|
|
|
| (T extends PropWrapper<unknown>
|
|
|
|
|
? T[typeof wrapperKey]
|
|
|
|
|
: [V] extends [never]
|
|
|
|
|
? ResolvePropTypeWithReadonly<T>
|
|
|
|
|
: never)
|
|
|
|
|
| V
|
|
|
|
|
| C
|
2021-10-06 19:56:24 +08:00
|
|
|
|
export type BuildPropType<T, V, C> = _BuildPropType<
|
|
|
|
|
IfUnknown<T, never>,
|
|
|
|
|
IfUnknown<V, never>,
|
|
|
|
|
IfUnknown<C, never>
|
|
|
|
|
>
|
2021-09-29 09:52:58 +08:00
|
|
|
|
|
2021-10-08 14:58:48 +08:00
|
|
|
|
type _BuildPropDefault<T, D> = [T] extends [
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
|
|
|
Record<string, unknown> | Array<any> | Function
|
|
|
|
|
]
|
|
|
|
|
? D
|
|
|
|
|
: D extends () => T
|
|
|
|
|
? ReturnType<D>
|
|
|
|
|
: D
|
|
|
|
|
|
|
|
|
|
export type BuildPropDefault<T, D, R> = R extends true
|
2021-09-29 09:52:58 +08:00
|
|
|
|
? { readonly default?: undefined }
|
|
|
|
|
: {
|
|
|
|
|
readonly default: Exclude<D, undefined> extends never
|
|
|
|
|
? undefined
|
2021-10-08 14:58:48 +08:00
|
|
|
|
: Exclude<_BuildPropDefault<T, D>, undefined>
|
2021-09-29 09:52:58 +08:00
|
|
|
|
}
|
2021-10-06 19:56:24 +08:00
|
|
|
|
export type BuildPropReturn<T, D, R, V, C> = {
|
|
|
|
|
readonly type: PropType<BuildPropType<T, V, C>>
|
2021-09-29 09:52:58 +08:00
|
|
|
|
readonly required: IfUnknown<R, false>
|
|
|
|
|
readonly validator: ((val: unknown) => boolean) | undefined
|
2021-10-06 19:56:24 +08:00
|
|
|
|
[propKey]: true
|
2021-10-08 14:58:48 +08:00
|
|
|
|
} & BuildPropDefault<
|
|
|
|
|
BuildPropType<T, V, C>,
|
|
|
|
|
IfUnknown<D, never>,
|
|
|
|
|
IfUnknown<R, false>
|
|
|
|
|
>
|
2021-09-29 09:52:58 +08:00
|
|
|
|
|
2021-09-12 19:38:48 +08:00
|
|
|
|
/**
|
|
|
|
|
* @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
|
2021-12-12 17:54:21 +08:00
|
|
|
|
// the type will be PropType<'small' | 'large' | number>
|
2021-09-12 19:38:48 +08:00
|
|
|
|
buildProp({
|
|
|
|
|
type: [String, Number],
|
2021-12-12 17:54:21 +08:00
|
|
|
|
values: ['small', 'large'],
|
2021-09-12 19:38:48 +08:00
|
|
|
|
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<
|
2021-09-22 20:27:23 +08:00
|
|
|
|
T = never,
|
2021-10-06 19:56:24 +08:00
|
|
|
|
D extends BuildPropType<T, V, C> = never,
|
2021-09-17 15:07:32 +08:00
|
|
|
|
R extends boolean = false,
|
2021-09-22 20:27:23 +08:00
|
|
|
|
V = never,
|
2021-09-12 19:38:48 +08:00
|
|
|
|
C = never
|
2021-10-15 18:24:49 +08:00
|
|
|
|
>(
|
|
|
|
|
option: BuildPropOption<T, D, R, V, C>,
|
|
|
|
|
key?: string
|
|
|
|
|
): BuildPropReturn<T, D, R, V, C> {
|
2021-10-06 19:56:24 +08:00
|
|
|
|
// filter native prop type and nested prop, e.g `null`, `undefined` (from `buildProps`)
|
|
|
|
|
if (!isObject(option) || !!option[propKey]) return option as any
|
|
|
|
|
|
2021-09-29 09:52:58 +08:00
|
|
|
|
const { values, required, default: defaultValue, type, validator } = option
|
|
|
|
|
|
2021-09-23 20:06:07 +08:00
|
|
|
|
const _validator =
|
|
|
|
|
values || validator
|
|
|
|
|
? (val: unknown) => {
|
|
|
|
|
let valid = false
|
|
|
|
|
let allowedValues: unknown[] = []
|
|
|
|
|
|
|
|
|
|
if (values) {
|
|
|
|
|
allowedValues = [...values, defaultValue]
|
|
|
|
|
valid ||= allowedValues.includes(val)
|
|
|
|
|
}
|
|
|
|
|
if (validator) valid ||= validator(val)
|
|
|
|
|
|
|
|
|
|
if (!valid && allowedValues.length > 0) {
|
2021-10-15 18:24:49 +08:00
|
|
|
|
const allowValuesText = [...new Set(allowedValues)]
|
|
|
|
|
.map((value) => JSON.stringify(value))
|
|
|
|
|
.join(', ')
|
|
|
|
|
warn(
|
|
|
|
|
`Invalid prop: validation failed${
|
|
|
|
|
key ? ` for prop "${key}"` : ''
|
|
|
|
|
}. Expected one of [${allowValuesText}], got value ${JSON.stringify(
|
|
|
|
|
val
|
|
|
|
|
)}.`
|
2021-09-23 20:06:07 +08:00
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
return valid
|
|
|
|
|
}
|
|
|
|
|
: undefined
|
|
|
|
|
|
2021-09-12 19:38:48 +08:00
|
|
|
|
return {
|
2021-10-18 16:21:30 +08:00
|
|
|
|
type:
|
|
|
|
|
typeof type === 'object' &&
|
|
|
|
|
Object.getOwnPropertySymbols(type).includes(wrapperKey)
|
|
|
|
|
? type[wrapperKey]
|
|
|
|
|
: type,
|
2021-09-23 20:06:07 +08:00
|
|
|
|
required: !!required,
|
|
|
|
|
default: defaultValue,
|
|
|
|
|
validator: _validator,
|
2021-10-06 19:56:24 +08:00
|
|
|
|
[propKey]: true,
|
2021-09-29 09:52:58 +08:00
|
|
|
|
} as unknown as BuildPropReturn<T, D, R, V, C>
|
2021-09-12 19:38:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-06 19:56:24 +08:00
|
|
|
|
type NativePropType = [
|
|
|
|
|
((...args: any) => any) | { new (...args: any): any } | undefined | null
|
|
|
|
|
]
|
|
|
|
|
|
2021-09-29 09:52:58 +08:00
|
|
|
|
export const buildProps = <
|
2021-10-06 19:56:24 +08:00
|
|
|
|
O extends {
|
|
|
|
|
[K in keyof O]: O[K] extends BuildPropReturn<any, any, any, any, any>
|
|
|
|
|
? O[K]
|
|
|
|
|
: [O[K]] extends NativePropType
|
|
|
|
|
? O[K]
|
|
|
|
|
: O[K] extends BuildPropOption<
|
|
|
|
|
infer T,
|
|
|
|
|
infer D,
|
|
|
|
|
infer R,
|
|
|
|
|
infer V,
|
|
|
|
|
infer C
|
|
|
|
|
>
|
|
|
|
|
? D extends BuildPropType<T, V, C>
|
|
|
|
|
? BuildPropOption<T, D, R, V, C>
|
|
|
|
|
: never
|
|
|
|
|
: never
|
|
|
|
|
}
|
2021-09-29 09:52:58 +08:00
|
|
|
|
>(
|
2021-10-15 18:24:49 +08:00
|
|
|
|
props: O
|
2021-09-29 09:52:58 +08:00
|
|
|
|
) =>
|
2021-10-15 18:24:49 +08:00
|
|
|
|
fromPairs(
|
|
|
|
|
Object.entries(props).map(([key, option]) => [
|
|
|
|
|
key,
|
|
|
|
|
buildProp(option as any, key),
|
|
|
|
|
])
|
|
|
|
|
) as unknown as {
|
2021-10-06 19:56:24 +08:00
|
|
|
|
[K in keyof O]: O[K] extends { [propKey]: boolean }
|
|
|
|
|
? O[K]
|
|
|
|
|
: [O[K]] extends NativePropType
|
|
|
|
|
? O[K]
|
|
|
|
|
: O[K] extends BuildPropOption<
|
|
|
|
|
infer T,
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
|
infer _D,
|
|
|
|
|
infer R,
|
|
|
|
|
infer V,
|
|
|
|
|
infer C
|
|
|
|
|
>
|
|
|
|
|
? BuildPropReturn<T, O[K]['default'], R, V, C>
|
2021-09-29 09:52:58 +08:00
|
|
|
|
: never
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-22 20:27:23 +08:00
|
|
|
|
export const definePropType = <T>(val: any) =>
|
|
|
|
|
({ [wrapperKey]: val } as PropWrapper<T>)
|
|
|
|
|
|
2021-09-17 15:07:32 +08:00
|
|
|
|
export const keyOf = <T>(arr: T) => Object.keys(arr) as Array<keyof T>
|
2021-09-22 20:27:23 +08:00
|
|
|
|
export const mutable = <T extends readonly any[] | Record<string, unknown>>(
|
|
|
|
|
val: T
|
|
|
|
|
) => val as Mutable<typeof val>
|
2021-09-12 19:38:48 +08:00
|
|
|
|
|
2021-12-12 17:54:21 +08:00
|
|
|
|
export const componentSize = ['large', 'default', 'small'] as const
|