tabs.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var vue = require('vue');
  4. require('../../../utils/index.js');
  5. require('../../../constants/index.js');
  6. var index$3 = require('../../icon/index.js');
  7. var iconsVue = require('@element-plus/icons-vue');
  8. require('../../../tokens/index.js');
  9. require('../../../hooks/index.js');
  10. var tabNav = require('./tab-nav.js');
  11. var runtime = require('../../../utils/vue/props/runtime.js');
  12. var shared = require('@vue/shared');
  13. var core = require('@vueuse/core');
  14. var event = require('../../../constants/event.js');
  15. var index = require('../../../hooks/use-namespace/index.js');
  16. var index$1 = require('../../../hooks/use-ordered-children/index.js');
  17. var types = require('../../../utils/types.js');
  18. var index$2 = require('../../../hooks/use-deprecated/index.js');
  19. var tabs = require('../../../tokens/tabs.js');
  20. var aria = require('../../../constants/aria.js');
  21. const tabsProps = runtime.buildProps({
  22. type: {
  23. type: String,
  24. values: ["card", "border-card", ""],
  25. default: ""
  26. },
  27. activeName: {
  28. type: [String, Number]
  29. },
  30. closable: Boolean,
  31. addable: Boolean,
  32. modelValue: {
  33. type: [String, Number]
  34. },
  35. editable: Boolean,
  36. tabPosition: {
  37. type: String,
  38. values: ["top", "right", "bottom", "left"],
  39. default: "top"
  40. },
  41. beforeLeave: {
  42. type: runtime.definePropType(Function),
  43. default: () => true
  44. },
  45. stretch: Boolean
  46. });
  47. const isPaneName = (value) => shared.isString(value) || core.isNumber(value);
  48. const tabsEmits = {
  49. [event.UPDATE_MODEL_EVENT]: (name) => isPaneName(name),
  50. tabClick: (pane, ev) => ev instanceof Event,
  51. tabChange: (name) => isPaneName(name),
  52. edit: (paneName, action) => ["remove", "add"].includes(action),
  53. tabRemove: (name) => isPaneName(name),
  54. tabAdd: () => true
  55. };
  56. var Tabs = vue.defineComponent({
  57. name: "ElTabs",
  58. props: tabsProps,
  59. emits: tabsEmits,
  60. setup(props, {
  61. emit,
  62. slots,
  63. expose
  64. }) {
  65. var _a, _b;
  66. const ns = index.useNamespace("tabs");
  67. const {
  68. children: panes,
  69. addChild: registerPane,
  70. removeChild: unregisterPane
  71. } = index$1.useOrderedChildren(vue.getCurrentInstance(), "ElTabPane");
  72. const nav$ = vue.ref();
  73. const currentName = vue.ref((_b = (_a = props.modelValue) != null ? _a : props.activeName) != null ? _b : "0");
  74. const changeCurrentName = (value) => {
  75. currentName.value = value;
  76. emit(event.UPDATE_MODEL_EVENT, value);
  77. emit("tabChange", value);
  78. };
  79. const setCurrentName = async (value) => {
  80. var _a2, _b2, _c;
  81. if (currentName.value === value || types.isUndefined(value))
  82. return;
  83. try {
  84. const canLeave = await ((_a2 = props.beforeLeave) == null ? void 0 : _a2.call(props, value, currentName.value));
  85. if (canLeave !== false) {
  86. changeCurrentName(value);
  87. (_c = (_b2 = nav$.value) == null ? void 0 : _b2.removeFocus) == null ? void 0 : _c.call(_b2);
  88. }
  89. } catch (e) {
  90. }
  91. };
  92. const handleTabClick = (tab, tabName, event) => {
  93. if (tab.props.disabled)
  94. return;
  95. setCurrentName(tabName);
  96. emit("tabClick", tab, event);
  97. };
  98. const handleTabRemove = (pane, ev) => {
  99. if (pane.props.disabled || types.isUndefined(pane.props.name))
  100. return;
  101. ev.stopPropagation();
  102. emit("edit", pane.props.name, "remove");
  103. emit("tabRemove", pane.props.name);
  104. };
  105. const handleTabAdd = () => {
  106. emit("edit", void 0, "add");
  107. emit("tabAdd");
  108. };
  109. index$2.useDeprecated({
  110. from: '"activeName"',
  111. replacement: '"model-value" or "v-model"',
  112. scope: "ElTabs",
  113. version: "2.3.0",
  114. ref: "https://element-plus.org/en-US/component/tabs.html#attributes",
  115. type: "Attribute"
  116. }, vue.computed(() => !!props.activeName));
  117. vue.watch(() => props.activeName, (modelValue) => setCurrentName(modelValue));
  118. vue.watch(() => props.modelValue, (modelValue) => setCurrentName(modelValue));
  119. vue.watch(currentName, async () => {
  120. var _a2;
  121. await vue.nextTick();
  122. (_a2 = nav$.value) == null ? void 0 : _a2.scrollToActiveTab();
  123. });
  124. vue.provide(tabs.tabsRootContextKey, {
  125. props,
  126. currentName,
  127. registerPane,
  128. unregisterPane
  129. });
  130. expose({
  131. currentName
  132. });
  133. return () => {
  134. const newButton = props.editable || props.addable ? vue.createVNode("span", {
  135. "class": ns.e("new-tab"),
  136. "tabindex": "0",
  137. "onClick": handleTabAdd,
  138. "onKeydown": (ev) => {
  139. if (ev.code === aria.EVENT_CODE.enter)
  140. handleTabAdd();
  141. }
  142. }, [vue.createVNode(index$3.ElIcon, {
  143. "class": ns.is("icon-plus")
  144. }, {
  145. default: () => [vue.createVNode(iconsVue.Plus, null, null)]
  146. })]) : null;
  147. const header = vue.createVNode("div", {
  148. "class": [ns.e("header"), ns.is(props.tabPosition)]
  149. }, [newButton, vue.createVNode(tabNav["default"], {
  150. "ref": nav$,
  151. "currentName": currentName.value,
  152. "editable": props.editable,
  153. "type": props.type,
  154. "panes": panes.value,
  155. "stretch": props.stretch,
  156. "onTabClick": handleTabClick,
  157. "onTabRemove": handleTabRemove
  158. }, null)]);
  159. const panels = vue.createVNode("div", {
  160. "class": ns.e("content")
  161. }, [vue.renderSlot(slots, "default")]);
  162. return vue.createVNode("div", {
  163. "class": [ns.b(), ns.m(props.tabPosition), {
  164. [ns.m("card")]: props.type === "card",
  165. [ns.m("border-card")]: props.type === "border-card"
  166. }]
  167. }, [...props.tabPosition !== "bottom" ? [header, panels] : [panels, header]]);
  168. };
  169. }
  170. });
  171. exports["default"] = Tabs;
  172. exports.tabsEmits = tabsEmits;
  173. exports.tabsProps = tabsProps;
  174. //# sourceMappingURL=tabs.js.map