You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1 line
5.3 KiB

{"version":3,"file":"runtime.mjs","sources":["../../../../../../packages/utils/vue/props/runtime.ts"],"sourcesContent":["import { warn } from 'vue'\nimport { fromPairs } from 'lodash-unified'\nimport { isObject } from '../../types'\nimport { hasOwn } from '../../objects'\n\nimport type { PropType } from 'vue'\nimport type {\n EpProp,\n EpPropConvert,\n EpPropFinalized,\n EpPropInput,\n EpPropMergeType,\n IfEpProp,\n IfNativePropType,\n NativePropType,\n} from './types'\n\nexport const epPropKey = '__epPropKey'\n\nexport const definePropType = <T>(val: any): PropType<T> => val\n\nexport const isEpProp = (val: unknown): val is EpProp<any, any, any> =>\n isObject(val) && !!(val as any)[epPropKey]\n\n/**\n * @description Build prop. It can better optimize prop types\n * @description 生成 prop能更好地优化类型\n * @example\n // limited options\n // the type will be PropType<'light' | 'dark'>\n buildProp({\n type: String,\n values: ['light', 'dark'],\n } as const)\n * @example\n // limited options and other types\n // the type will be PropType<'small' | 'large' | number>\n buildProp({\n type: [String, Number],\n values: ['small', 'large'],\n validator: (val: unknown): val is number => typeof val === 'number',\n } as const)\n @link see more: https://github.com/element-plus/element-plus/pull/3341\n */\nexport const buildProp = <\n Type = never,\n Value = never,\n Validator = never,\n Default extends EpPropMergeType<Type, Value, Validator> = never,\n Required extends boolean = false\n>(\n prop: EpPropInput<Type, Value, Validator, Default, Required>,\n key?: string\n): EpPropFinalized<Type, Value, Validator, Default, Required> => {\n // filter native prop type and nested prop, e.g `null`, `undefined` (from `buildProps`)\n if (!isObject(prop) || isEpProp(prop)) return prop as any\n\n const { values, required, default: defaultValue, type, validator } = prop\n\n const _validator =\n values || validator\n ? (val: unknown) => {\n let valid = false\n let allowedValues: unknown[] = []\n\n if (values) {\n allowedValues = Array.from(values)\n if (hasOwn(prop, 'default')) {\n allowedValues.push(defaultValue)\n }\n valid ||= allowedValues.includes(val)\n }\n if (validator) valid ||= validator(val)\n\n if (!valid && allowedValues.length > 0) {\n const allowValuesText = [...new Set(allowedValues)]\n .map((value) => JSON.stringify(value))\n .join(', ')\n warn(\n `Invalid prop: validation failed${\n key ? ` for prop \"${key}\"` : ''\n }. Expected one of [${allowValuesText}], got value ${JSON.stringify(\n val\n )}.`\n )\n }\n return valid\n }\n : undefined\n\n const epProp: any = {\n type,\n required: !!required,\n validator: _validator,\n [epPropKey]: true,\n }\n if (hasOwn(prop, 'default')) epProp.default = defaultValue\n return epProp\n}\n\nexport const buildProps = <\n Props extends Record<\n string,\n | { [epPropKey]: true }\n | NativePropType\n | EpPropInput<any, any, any, any, any>\n >\n>(\n props: Props\n): {\n [K in keyof Props]: IfEpProp<\n Props[K],\n Props[K],\n IfNativePropType<Props[K], Props[K], EpPropConvert<Props[K]>>\n >\n} =>\n fromPairs(\n Object.entries(props).map(([key, option]) => [\n key,\n buildProp(option as any, key),\n ])\n ) as any\n"],"names":[],"mappings":";;;;;;AAIY,MAAC,SAAS,GAAG,cAAc;AAC3B,MAAC,cAAc,GAAG,CAAC,GAAG,KAAK,IAAI;AAC/B,MAAC,QAAQ,GAAG,CAAC,GAAG,KAAK,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,SAAS,EAAE;AACvD,MAAC,SAAS,GAAG,CAAC,IAAI,EAAE,GAAG,KAAK;AACxC,EAAE,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,QAAQ,CAAC,IAAI,CAAC;AACvC,IAAI,OAAO,IAAI,CAAC;AAChB,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,OAAO,EAAE,YAAY,EAAE,IAAI,EAAE,SAAS,EAAE,GAAG,IAAI,CAAC;AAC5E,EAAE,MAAM,UAAU,GAAG,MAAM,IAAI,SAAS,GAAG,CAAC,GAAG,KAAK;AACpD,IAAI,IAAI,KAAK,GA