{"version":3,"file":"tabs.mjs","sources":["../../../../../../packages/components/tabs/src/tabs.tsx"],"sourcesContent":["import {\n computed,\n createVNode,\n defineComponent,\n getCurrentInstance,\n nextTick,\n provide,\n ref,\n renderSlot,\n watch,\n} from 'vue'\nimport {\n buildProps,\n definePropType,\n isNumber,\n isString,\n isUndefined,\n} from '@element-plus/utils'\nimport { EVENT_CODE, UPDATE_MODEL_EVENT } from '@element-plus/constants'\nimport ElIcon from '@element-plus/components/icon'\nimport { Plus } from '@element-plus/icons-vue'\nimport { useNamespace, useOrderedChildren } from '@element-plus/hooks'\nimport { tabsRootContextKey } from './constants'\nimport TabNav from './tab-nav'\n\nimport type { TabNavInstance } from './tab-nav'\nimport type { TabsPaneContext } from './constants'\nimport type { ExtractPropTypes, FunctionalComponent, VNode } from 'vue'\nimport type { Awaitable } from '@element-plus/utils'\n\nexport type TabPaneName = string | number\n\nexport const tabsProps = buildProps({\n /**\n * @description type of Tab\n */\n type: {\n type: String,\n values: ['card', 'border-card', ''],\n default: '',\n },\n /**\n * @description whether Tab is closable\n */\n closable: Boolean,\n /**\n * @description whether Tab is addable\n */\n addable: Boolean,\n /**\n * @description binding value, name of the selected tab\n */\n modelValue: {\n type: [String, Number],\n },\n /**\n * @description whether Tab is addable and closable\n */\n editable: Boolean,\n /**\n * @description position of tabs\n */\n tabPosition: {\n type: String,\n values: ['top', 'right', 'bottom', 'left'],\n default: 'top',\n },\n /**\n * @description hook function before switching tab. If `false` is returned or a `Promise` is returned and then is rejected, switching will be prevented\n */\n beforeLeave: {\n type: definePropType<\n (newName: TabPaneName, oldName: TabPaneName) => Awaitable\n >(Function),\n default: () => true,\n },\n /**\n * @description whether width of tab automatically fits its container\n */\n stretch: Boolean,\n} as const)\nexport type TabsProps = ExtractPropTypes\n\nconst isPaneName = (value: unknown): value is string | number =>\n isString(value) || isNumber(value)\n\nexport const tabsEmits = {\n [UPDATE_MODEL_EVENT]: (name: TabPaneName) => isPaneName(name),\n tabClick: (pane: TabsPaneContext, ev: Event) => ev instanceof Event,\n tabChange: (name: TabPaneName) => isPaneName(name),\n edit: (paneName: TabPaneName | undefined, action: 'remove' | 'add') =>\n ['remove', 'add'].includes(action),\n tabRemove: (name: TabPaneName) => isPaneName(name),\n tabAdd: () => true,\n}\nexport type TabsEmits = typeof tabsEmits\n\nexport type TabsPanes = Record\n\nconst Tabs = defineComponent({\n name: 'ElTabs',\n\n props: tabsProps,\n emits: tabsEmits,\n\n setup(props, { emit, slots, expose }) {\n const ns = useNamespace('tabs')\n\n const isVertical = computed(() =>\n ['left', 'right'].includes(props.tabPosition)\n )\n\n const {\n children: panes,\n addChild: sortPane,\n removeChild: unregisterPane,\n } = useOrderedChildren(getCurrentInstance()!, 'ElTabPane')\n\n const nav$ = ref()\n const currentName = ref(props.modelValue ?? '0')\n\n const setCurrentName = async (value?: TabPaneName, trigger = false) => {\n // should do nothing.\n if (currentName.value === value || isUndefined(value)) return\n\n try {\n const canLeave = await props.beforeLeave?.(value, currentName.value)\n if (canLeave !== false) {\n currentName.value = value\n if (trigger) {\n emit(UPDATE_MODEL_EVENT, value)\n emit('tabChange', value)\n }\n\n nav$.value?.removeFocus?.()\n }\n } catch {}\n }\n\n const handleTabClick = (\n tab: TabsPaneContext,\n tabName: TabPaneName,\n event: Event\n ) => {\n if (tab.props.disabled) return\n setCurrentName(tabName, true)\n emit('tabClick', tab, event)\n }\n\n const handleTabRemove = (pane: TabsPaneContext, ev: Event) => {\n if (pane.props.disabled || isUndefined(pane.props.name)) return\n ev.stopPropagation()\n emit('edit', pane.props.name, 'remove')\n emit('tabRemove', pane.props.name)\n }\n\n const handleTabAdd = () => {\n emit('edit', undefined, 'add')\n emit('tabAdd')\n }\n\n watch(\n () => props.modelValue,\n (modelValue) => setCurrentName(modelValue)\n )\n\n watch(currentName, async () => {\n await nextTick()\n nav$.value?.scrollToActiveTab()\n })\n\n provide(tabsRootContextKey, {\n props,\n currentName,\n registerPane: (pane: TabsPaneContext) => {\n panes.value.push(pane)\n },\n sortPane,\n unregisterPane,\n })\n\n expose({\n currentName,\n })\n const TabNavRenderer: FunctionalComponent<{ render: () => VNode }> = ({\n render,\n }) => {\n return render()\n }\n return () => {\n const addSlot = slots['add-icon']\n const newButton =\n props.editable || props.addable ? (\n {\n if (ev.code === EVENT_CODE.enter) handleTabAdd()\n }}\n >\n {addSlot ? (\n renderSlot(slots, 'add-icon')\n ) : (\n \n \n \n )}\n \n ) : null\n\n const header = (\n \n {\n const hasLabelSlot = panes.value.some((pane) => pane.slots.label)\n return createVNode(\n TabNav,\n {\n ref: nav$,\n currentName: currentName.value,\n editable: props.editable,\n type: props.type,\n panes: panes.value,\n stretch: props.stretch,\n onTabClick: handleTabClick,\n onTabRemove: handleTabRemove,\n },\n { $stable: !hasLabelSlot }\n )\n }}\n />\n {newButton}\n \n )\n\n const panels = (\n
{renderSlot(slots, 'default')}
\n )\n\n return (\n \n {panels}\n {header}\n \n )\n }\n },\n})\n\nexport type TabsInstance = InstanceType & {\n currentName: TabPaneName\n}\n\nexport default Tabs\n"],"names":["tabsProps","buildProps","type","values","default","closable","addable","modelValue","editable","tabPosition","beforeLeave","definePropType","stretch","Boolean","isPaneName","name","ev","tabChange","edit","tabRemove","tabAdd","Tabs","defineComponent","props","emits","expose","ns","useNamespace","children","addChild","removeChild","getCurrentInstance","nav$","ref","currentName","setCurrentName","value","canLeave","trigger","emit","handleTabClick","pane","handleTabAdd","_createVNode","undefined","watch","scrollToActiveTab","panes","push","unregisterPane","TabNavRenderer","render","newButton"],"mappings":";;;;;;;;;;;;;;;;AAgCaA,MAAAA,SAAS,GAAGC,UAAU,CAAC;AAClC,EAAA,IAAA,EAAA;AACF,IAAA,IAAA,EAAA,MAAA;AACA,IAAA,MAAA,EAAA,CAAA,MAAA,EAAA,aAAA,EAAA,EAAA,CAAA;AACEC,IAAAA,OAAM,EAAA,EAAA;AACJA,GAAAA;AACAC,EAAAA,QAAM,EAAE,OAAA;AACRC,EAAAA,OAAAA,EAAO,OAAE;EAHL,UA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