sfc.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. exports.__esModule = true;
  4. exports.unifySlots = unifySlots;
  5. exports.useSFC = void 0;
  6. require("../../locale");
  7. var _ = require("..");
  8. var _slots = require("../../mixins/slots");
  9. var _vue = _interopRequireDefault(require("vue"));
  10. /**
  11. * Create a basic component with common options
  12. */
  13. var arrayProp = {
  14. type: Array,
  15. "default": function _default() {
  16. return [];
  17. }
  18. };
  19. var numberProp = {
  20. type: Number,
  21. "default": 0
  22. };
  23. function defaultProps(props) {
  24. Object.keys(props).forEach(function (key) {
  25. if (props[key] === Array) {
  26. props[key] = arrayProp;
  27. } else if (props[key] === Number) {
  28. props[key] = numberProp;
  29. }
  30. });
  31. }
  32. function install(Vue) {
  33. var name = this.name;
  34. Vue.component(name, this);
  35. Vue.component((0, _.camelize)("-" + name), this);
  36. } // unify slots & scopedSlots
  37. function unifySlots(context) {
  38. // use data.scopedSlots in lower Vue version
  39. var scopedSlots = context.scopedSlots || context.data.scopedSlots || {};
  40. var slots = context.slots();
  41. Object.keys(slots).forEach(function (key) {
  42. if (!scopedSlots[key]) {
  43. scopedSlots[key] = function () {
  44. return slots[key];
  45. };
  46. }
  47. });
  48. return scopedSlots;
  49. } // should be removed after Vue 3
  50. function transformFunctionComponent(pure) {
  51. return {
  52. functional: true,
  53. props: pure.props,
  54. model: pure.model,
  55. render: function render(h, context) {
  56. return pure(h, context.props, unifySlots(context), context);
  57. }
  58. };
  59. }
  60. var useSFC = function useSFC(name) {
  61. return function (sfc) {
  62. if (typeof sfc === 'function') {
  63. sfc = transformFunctionComponent(sfc);
  64. }
  65. if (!sfc.functional) {
  66. sfc.mixins = sfc.mixins || [];
  67. sfc.mixins.push(_slots.SlotsMixin);
  68. }
  69. if (sfc.props) {
  70. defaultProps(sfc.props);
  71. }
  72. sfc.name = name;
  73. sfc.install = install;
  74. return sfc;
  75. };
  76. };
  77. exports.useSFC = useSFC;