UNPKG

element-plus

Version:

A Component Library for Vue 3

49 lines (46 loc) 1.7 kB
import { warn } from 'vue'; import { fromPairs } from 'lodash-unified'; import '../types.mjs'; import '../objects.mjs'; import { isObject, hasOwn } from '@vue/shared'; const wrapperKey = Symbol(); const propKey = "__elPropsReservedKey"; function buildProp(option, key) { if (!isObject(option) || !!option[propKey]) return option; const { values, required, default: defaultValue, type, validator } = option; const _validator = values || validator ? (val) => { let valid = false; let allowedValues = []; if (values) { allowedValues = Array.from(values); if (hasOwn(option, "default")) { allowedValues.push(defaultValue); } valid || (valid = allowedValues.includes(val)); } if (validator) valid || (valid = validator(val)); if (!valid && allowedValues.length > 0) { 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)}.`); } return valid; } : void 0; const prop = { type: isObject(type) && Object.getOwnPropertySymbols(type).includes(wrapperKey) ? type[wrapperKey] : type, required: !!required, validator: _validator, [propKey]: true }; if (hasOwn(option, "default")) prop.default = defaultValue; return prop; } const buildProps = (props) => fromPairs(Object.entries(props).map(([key, option]) => [ key, buildProp(option, key) ])); const definePropType = (val) => ({ [wrapperKey]: val }); export { buildProp, buildProps, definePropType, propKey }; //# sourceMappingURL=props.mjs.map