component.js 1.3 KB

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