UNPKG

hongluan-ui

Version:
222 lines (219 loc) 7.35 kB
import { defineComponent, computed, getCurrentInstance, ref, watch, nextTick, provide, createVNode, renderSlot } from 'vue'; import '../../../utils/index.mjs'; import '../../../constants/index.mjs'; import { HlIcon } from '../../icon/index.mjs'; import '../../system-icon/index.mjs'; import '../../../tokens/index.mjs'; import '../../../hooks/index.mjs'; import TabNav from './tab-nav.mjs'; import { buildProps, definePropType } from '../../../utils/vue/props/runtime.mjs'; import { isString, isFunction } from '@vue/shared'; import { isNumber, isUndefined } from '../../../utils/types.mjs'; import { UPDATE_MODEL_EVENT, INPUT_EVENT } from '../../../constants/event.mjs'; import { useOrderedChildren } from '../../../hooks/use-ordered-children/index.mjs'; import { useDeprecated } from '../../../hooks/use-deprecated/index.mjs'; import { useNamespace } from '../../../hooks/use-namespace/index.mjs'; import { tabsRootContextKey } from '../../../tokens/tabs.mjs'; import { EVENT_CODE } from '../../../constants/aria.mjs'; import SystemAdd from '../../system-icon/src/add.mjs'; const tabsProps = buildProps({ type: { type: String, values: ["line", "button", ""], default: "" }, activeName: { type: [String, Number] }, closable: Boolean, addable: Boolean, modelValue: { type: [String, Number] }, editable: Boolean, tabPosition: { type: String, values: ["top", "right", "bottom", "left"], default: "top" }, beforeLeave: { type: definePropType(Function), default: () => true }, showPane: { type: Boolean, default: true }, paneHeight: { type: String }, navHeight: { type: String }, navPadding: { type: String }, gap: { type: String }, stretch: Boolean }); const isPanelName = (value) => isString(value) || isNumber(value); const tabsEmits = { [UPDATE_MODEL_EVENT]: (name) => isPanelName(name), [INPUT_EVENT]: (name) => isPanelName(name), tabClick: (pane, ev) => ev instanceof Event, tabChange: (name) => isPanelName(name), edit: (paneName, action) => ["remove", "add"].includes(action), tabRemove: (name) => isPanelName(name), tabAdd: () => true }; const Tabs = defineComponent({ name: "Tabs", props: tabsProps, emits: tabsEmits, setup(props, { emit, slots, expose }) { var _a, _b; const isVertical = computed(() => ["left", "right"].includes(props.tabPosition)); const { children: panes, addChild: sortPane, removeChild: unregisterPane } = useOrderedChildren(getCurrentInstance(), "TabPane"); const instance = getCurrentInstance(); const nav$ = ref(); const currentName = ref((_b = (_a = props.modelValue) != null ? _a : props.activeName) != null ? _b : "0"); useDeprecated({ scope: "Tabs", type: "Event", from: "input", replacement: "tab-change", version: "2.5.0", ref: "https://hongluan-ui.github.io/hongluan-ui/2x/#/zh-CN/component/tabs.html#tabs-events" }, computed(() => { var _a2; return isFunction((_a2 = instance.vnode.props) == null ? void 0 : _a2.onInput); })); useDeprecated({ from: '"activeName"', replacement: '"model-value" or "v-model"', scope: "Tabs", version: "2.5.0", ref: "https://hongluan-ui.github.io/hongluan-ui/2x/#/zh-CN/component/tabs.html#attributes", type: "Attribute" }, computed(() => !!props.activeName)); const { namespace } = useNamespace("tabs"); const setCurrentName = async (value, trigger = false) => { var _a2, _b2, _c; if (currentName.value === value || isUndefined(value)) return; try { const canLeave = await ((_a2 = props.beforeLeave) == null ? void 0 : _a2.call(props, value, currentName.value)); if (canLeave !== false) { currentName.value = value; if (trigger) { emit(UPDATE_MODEL_EVENT, value); emit("tabChange", value); } (_c = (_b2 = nav$.value) == null ? void 0 : _b2.removeFocus) == null ? void 0 : _c.call(_b2); } } catch (e) { } }; const handleTabClick = (tab, tabName, event) => { if (tab.props.disabled) return; setCurrentName(tabName, true); emit("tabClick", tab, event); }; const handleTabRemove = (pane, ev) => { if (pane.props.disabled || isUndefined(pane.props.name)) return; ev.stopPropagation(); emit("edit", pane.props.name, "remove"); emit("tabRemove", pane.props.name); }; const handleTabAdd = () => { emit("edit", void 0, "add"); emit("tabAdd"); }; watch(() => props.activeName, (modelValue) => setCurrentName(modelValue)); watch(() => props.modelValue, (modelValue) => setCurrentName(modelValue)); watch(currentName, async () => { var _a2; await nextTick(); (_a2 = nav$.value) == null ? void 0 : _a2.scrollToActiveTab(); }); provide(tabsRootContextKey, { props, currentName, registerPane: (pane) => { panes.value.push(pane); }, sortPane, unregisterPane }); expose({ currentName }); const TabNavRenderer = ({ render }) => { return render(); }; return () => { const addSlot = slots["add-icon"]; const newButton = props.editable || props.addable ? createVNode("div", { "class": ["tab-new", isVertical.value && "new-tab-vertical"], "tabindex": "0", "onClick": handleTabAdd, "onKeydown": (ev) => { if (ev.code === EVENT_CODE.enter) handleTabAdd(); } }, [addSlot ? renderSlot(slots, "add-icon") : createVNode(HlIcon, null, { default: () => [createVNode(SystemAdd, null, null)] })]) : null; const header = createVNode("div", { "class": ["tabs-header", `at-${props.tabPosition}`, isVertical.value && "header-vertical"] }, [createVNode(TabNavRenderer, { "render": () => { const hasLabelSlot = panes.value.some((pane) => pane.slots.label); return createVNode(TabNav, { ref: nav$, currentName: currentName.value, editable: props.editable, type: props.type, panes: panes.value, stretch: props.stretch, onTabClick: handleTabClick, onTabRemove: handleTabRemove }, { $stable: !hasLabelSlot }); } }, null), newButton]); const panels = createVNode("div", { "class": "tabs-content" }, [renderSlot(slots, "default")]); return createVNode("div", { "class": { [namespace.value]: true, "tabs-line": props.type === "line", "tabs-button": props.type === "button", "no-pane": !props.showPane, [`tabs-${props.tabPosition}`]: true }, "style": [props.paneHeight ? `--tabs-pane-height:${props.paneHeight}` : "", props.navHeight ? `--tabs-height:${props.navHeight}` : "", props.navPadding ? `--tabs-padding:${props.navPadding}` : "", props.gap ? `--tabs-gap:${props.gap}` : ""] }, [...props.tabPosition !== "bottom" ? [header, panels] : [panels, header]]); }; } }); export { Tabs as default, tabsEmits, tabsProps }; //# sourceMappingURL=tabs.mjs.map