runtime.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
  2. const require_runtime = require('../../../_virtual/_rolldown/runtime.js');
  3. let lodash_unified = require("lodash-unified");
  4. let vue = require("vue");
  5. let _vue_shared = require("@vue/shared");
  6. //#region ../../packages/utils/vue/props/runtime.ts
  7. const epPropKey = "__epPropKey";
  8. const definePropType = (val) => val;
  9. const isEpProp = (val) => (0, _vue_shared.isObject)(val) && !!val[epPropKey];
  10. /**
  11. * @description Build prop. It can better optimize prop types
  12. * @description 生成 prop,能更好地优化类型
  13. * @example
  14. // limited options
  15. // the type will be PropType<'light' | 'dark'>
  16. buildProp({
  17. type: String,
  18. values: ['light', 'dark'],
  19. } as const)
  20. * @example
  21. // limited options and other types
  22. // the type will be PropType<'small' | 'large' | number>
  23. buildProp({
  24. type: [String, Number],
  25. values: ['small', 'large'],
  26. validator: (val: unknown): val is number => typeof val === 'number',
  27. } as const)
  28. @link see more: https://github.com/element-plus/element-plus/pull/3341
  29. */
  30. const buildProp = (prop, key) => {
  31. if (!(0, _vue_shared.isObject)(prop) || isEpProp(prop)) return prop;
  32. const { values, required, default: defaultValue, type, validator } = prop;
  33. const epProp = {
  34. type,
  35. required: !!required,
  36. validator: values || validator ? (val) => {
  37. let valid = false;
  38. let allowedValues = [];
  39. if (values) {
  40. allowedValues = Array.from(values);
  41. if ((0, _vue_shared.hasOwn)(prop, "default")) allowedValues.push(defaultValue);
  42. valid ||= allowedValues.includes(val);
  43. }
  44. if (validator) valid ||= validator(val);
  45. if (!valid && allowedValues.length > 0) {
  46. const allowValuesText = [...new Set(allowedValues)].map((value) => JSON.stringify(value)).join(", ");
  47. (0, vue.warn)(`Invalid prop: validation failed${key ? ` for prop "${key}"` : ""}. Expected one of [${allowValuesText}], got value ${JSON.stringify(val)}.`);
  48. }
  49. return valid;
  50. } : void 0,
  51. [epPropKey]: true
  52. };
  53. if ((0, _vue_shared.hasOwn)(prop, "default")) epProp.default = defaultValue;
  54. return epProp;
  55. };
  56. const buildProps = (props) => (0, lodash_unified.fromPairs)(Object.entries(props).map(([key, option]) => [key, buildProp(option, key)]));
  57. //#endregion
  58. exports.buildProp = buildProp;
  59. exports.buildProps = buildProps;
  60. exports.definePropType = definePropType;
  61. exports.epPropKey = epPropKey;
  62. exports.isEpProp = isEpProp;
  63. //# sourceMappingURL=runtime.js.map