component.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. exports.__esModule = true;
  4. exports.unifySlots = unifySlots;
  5. exports.createComponent = createComponent;
  6. require("../../locale");
  7. var _ = require("..");
  8. var _string = require("../format/string");
  9. var _slots = require("../../mixins/slots");
  10. var _vue = _interopRequireDefault(require("vue"));
  11. /**
  12. * Create a basic component with common options
  13. */
  14. function install(Vue) {
  15. var name = this.name;
  16. Vue.component(name, this);
  17. Vue.component((0, _string.camelize)("-" + name), this);
  18. } // unify slots & scopedSlots
  19. function unifySlots(context) {
  20. // use data.scopedSlots in lower Vue version
  21. var scopedSlots = context.scopedSlots || context.data.scopedSlots || {};
  22. var slots = context.slots();
  23. Object.keys(slots).forEach(function (key) {
  24. if (!scopedSlots[key]) {
  25. scopedSlots[key] = function () {
  26. return slots[key];
  27. };
  28. }
  29. });
  30. return scopedSlots;
  31. } // should be removed after Vue 3
  32. function transformFunctionComponent(pure) {
  33. return {
  34. functional: true,
  35. props: pure.props,
  36. model: pure.model,
  37. render: function render(h, context) {
  38. return pure(h, context.props, unifySlots(context), context);
  39. }
  40. };
  41. }
  42. function createComponent(name) {
  43. return function (sfc) {
  44. if ((0, _.isFunction)(sfc)) {
  45. sfc = transformFunctionComponent(sfc);
  46. }
  47. if (!sfc.functional) {
  48. sfc.mixins = sfc.mixins || [];
  49. sfc.mixins.push(_slots.SlotsMixin);
  50. }
  51. sfc.name = name;
  52. sfc.install = install;
  53. return sfc;
  54. };
  55. }