tabs.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. require("../../../_virtual/_rolldown/runtime.js");
  2. const require_aria = require("../../../constants/aria.js");
  3. const require_event = require("../../../constants/event.js");
  4. const require_event$1 = require("../../../utils/dom/event.js");
  5. const require_types = require("../../../utils/types.js");
  6. const require_runtime$1 = require("../../../utils/vue/props/runtime.js");
  7. const require_index = require("../../../hooks/use-namespace/index.js");
  8. const require_index$1 = require("../../../hooks/use-ordered-children/index.js");
  9. const require_index$2 = require("../../icon/index.js");
  10. const require_constants = require("./constants.js");
  11. const require_tab_nav = require("./tab-nav.js");
  12. let lodash_unified = require("lodash-unified");
  13. let _element_plus_icons_vue = require("@element-plus/icons-vue");
  14. let vue = require("vue");
  15. let _vue_shared = require("@vue/shared");
  16. //#region ../../packages/components/tabs/src/tabs.tsx
  17. const tabsProps = require_runtime$1.buildProps({
  18. /**
  19. * @description type of Tab
  20. */
  21. type: {
  22. type: String,
  23. values: [
  24. "card",
  25. "border-card",
  26. ""
  27. ],
  28. default: ""
  29. },
  30. /**
  31. * @description whether Tab is closable
  32. */
  33. closable: Boolean,
  34. /**
  35. * @description whether Tab is addable
  36. */
  37. addable: Boolean,
  38. /**
  39. * @description binding value, name of the selected tab
  40. */
  41. modelValue: { type: [String, Number] },
  42. /**
  43. * @description initial value when `model-value` is not set
  44. */
  45. defaultValue: { type: [String, Number] },
  46. /**
  47. * @description whether Tab is addable and closable
  48. */
  49. editable: Boolean,
  50. /**
  51. * @description position of tabs
  52. */
  53. tabPosition: {
  54. type: String,
  55. values: [
  56. "top",
  57. "right",
  58. "bottom",
  59. "left"
  60. ],
  61. default: "top"
  62. },
  63. /**
  64. * @description hook function before switching tab. If `false` is returned or a `Promise` is returned and then is rejected, switching will be prevented
  65. */
  66. beforeLeave: {
  67. type: require_runtime$1.definePropType(Function),
  68. default: () => true
  69. },
  70. /**
  71. * @description whether width of tab automatically fits its container
  72. */
  73. stretch: Boolean,
  74. /**
  75. * @description tabs tabindex
  76. */
  77. tabindex: {
  78. type: [String, Number],
  79. default: 0
  80. }
  81. });
  82. const isPaneName = (value) => (0, _vue_shared.isString)(value) || require_types.isNumber(value);
  83. const tabsEmits = {
  84. [require_event.UPDATE_MODEL_EVENT]: (name) => isPaneName(name),
  85. tabClick: (pane, ev) => ev instanceof Event,
  86. tabChange: (name) => isPaneName(name),
  87. edit: (paneName, action) => ["remove", "add"].includes(action),
  88. tabRemove: (name) => isPaneName(name),
  89. tabAdd: () => true
  90. };
  91. const Tabs = /* @__PURE__ */ (0, vue.defineComponent)({
  92. name: "ElTabs",
  93. props: tabsProps,
  94. emits: tabsEmits,
  95. setup(props, { emit, slots, expose }) {
  96. const ns = require_index.useNamespace("tabs");
  97. const isVertical = (0, vue.computed)(() => ["left", "right"].includes(props.tabPosition));
  98. const { children: panes, addChild: registerPane, removeChild: unregisterPane, ChildrenSorter: PanesSorter } = require_index$1.useOrderedChildren((0, vue.getCurrentInstance)(), "ElTabPane");
  99. const nav$ = (0, vue.ref)();
  100. const currentName = (0, vue.ref)((require_types.isUndefined(props.modelValue) ? props.defaultValue : props.modelValue) ?? "0");
  101. const setCurrentName = async (value, trigger = false) => {
  102. if (currentName.value === value || require_types.isUndefined(value)) return;
  103. try {
  104. let canLeave;
  105. if (props.beforeLeave) {
  106. const result = props.beforeLeave(value, currentName.value);
  107. canLeave = result instanceof Promise ? await result : result;
  108. } else canLeave = true;
  109. if (canLeave !== false) {
  110. const isFocusInsidePane = panes.value.find((item) => item.paneName === currentName.value)?.isFocusInsidePane();
  111. currentName.value = value;
  112. if (trigger) {
  113. emit(require_event.UPDATE_MODEL_EVENT, value);
  114. emit("tabChange", value);
  115. }
  116. nav$.value?.removeFocus?.();
  117. if (isFocusInsidePane) nav$.value?.focusActiveTab();
  118. }
  119. } catch {}
  120. };
  121. const handleTabClick = (tab, tabName, event) => {
  122. if (tab.props.disabled) return;
  123. emit("tabClick", tab, event);
  124. setCurrentName(tabName, true);
  125. };
  126. const handleTabRemove = (pane, ev) => {
  127. if (pane.props.disabled || require_types.isUndefined(pane.props.name)) return;
  128. ev.stopPropagation();
  129. emit("edit", pane.props.name, "remove");
  130. emit("tabRemove", pane.props.name);
  131. };
  132. const handleTabAdd = () => {
  133. emit("edit", void 0, "add");
  134. emit("tabAdd");
  135. };
  136. const handleKeydown = (event) => {
  137. const code = require_event$1.getEventCode(event);
  138. if ([require_aria.EVENT_CODE.enter, require_aria.EVENT_CODE.numpadEnter].includes(code)) handleTabAdd();
  139. };
  140. const swapChildren = (vnode) => {
  141. const actualFirstChild = vnode.el.firstChild;
  142. const firstChild = ["bottom", "right"].includes(props.tabPosition) ? vnode.children[0].el : vnode.children[1].el;
  143. if (actualFirstChild !== firstChild) actualFirstChild.before(firstChild);
  144. };
  145. (0, vue.watch)(() => props.modelValue, (modelValue) => setCurrentName(modelValue));
  146. (0, vue.watch)(currentName, async () => {
  147. await (0, vue.nextTick)();
  148. nav$.value?.scrollToActiveTab();
  149. });
  150. (0, vue.provide)(require_constants.tabsRootContextKey, {
  151. props,
  152. currentName,
  153. registerPane,
  154. unregisterPane,
  155. nav$
  156. });
  157. expose({
  158. currentName,
  159. get tabNavRef() {
  160. return (0, lodash_unified.omit)(nav$.value, ["scheduleRender"]);
  161. }
  162. });
  163. return () => {
  164. const addSlot = slots["add-icon"];
  165. const newButton = props.editable || props.addable ? (0, vue.createVNode)("div", {
  166. "class": [ns.e("new-tab"), isVertical.value && ns.e("new-tab-vertical")],
  167. "tabindex": props.tabindex,
  168. "onClick": handleTabAdd,
  169. "onKeydown": handleKeydown
  170. }, [addSlot ? (0, vue.renderSlot)(slots, "add-icon") : (0, vue.createVNode)(require_index$2.ElIcon, { "class": ns.is("icon-plus") }, { default: () => [(0, vue.createVNode)(_element_plus_icons_vue.Plus, null, null)] })]) : null;
  171. const tabNav = () => (0, vue.createVNode)(require_tab_nav.default, {
  172. "ref": nav$,
  173. "currentName": currentName.value,
  174. "editable": props.editable,
  175. "type": props.type,
  176. "panes": panes.value,
  177. "stretch": props.stretch,
  178. "onTabClick": handleTabClick,
  179. "onTabRemove": handleTabRemove
  180. }, null);
  181. const header = (0, vue.createVNode)("div", { "class": [
  182. ns.e("header"),
  183. isVertical.value && ns.e("header-vertical"),
  184. ns.is(props.tabPosition)
  185. ] }, [(0, vue.createVNode)(PanesSorter, null, {
  186. default: tabNav,
  187. $stable: true
  188. }), newButton]);
  189. const panels = (0, vue.createVNode)("div", { "class": ns.e("content") }, [(0, vue.renderSlot)(slots, "default")]);
  190. return (0, vue.createVNode)("div", {
  191. "class": [
  192. ns.b(),
  193. ns.m(props.tabPosition),
  194. {
  195. [ns.m("card")]: props.type === "card",
  196. [ns.m("border-card")]: props.type === "border-card"
  197. }
  198. ],
  199. "onVnodeMounted": swapChildren,
  200. "onVnodeUpdated": swapChildren
  201. }, [panels, header]);
  202. };
  203. }
  204. });
  205. //#endregion
  206. exports.default = Tabs;
  207. exports.tabsEmits = tabsEmits;
  208. exports.tabsProps = tabsProps;
  209. //# sourceMappingURL=tabs.js.map