tdesign-vue
Version:
393 lines (389 loc) • 15 kB
JavaScript
/**
* tdesign v1.11.2
* (c) 2025 tdesign
* @license MIT
*/
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { debounce } from 'lodash-es';
import { ChevronLeftIcon, ChevronRightIcon, AddIcon } from 'tdesign-icons-vue';
import TTabNavItem from './tab-nav-item.js';
import { emitEvent } from '../utils/event.js';
import { firstUpperCase } from '../utils/helper.js';
import props from './props.js';
import { renderTNodeJSX } from '../utils/render-tnode.js';
import { getClassPrefixMixins, getGlobalIconMixins } from '../config-provider/config-receiver.js';
import mixins from '../utils/mixins.js';
import { calcValidOffset, calcMaxOffset, calcPrevOrNextOffset, calculateOffset } from '../_common/js/tabs/base.js';
import '../utils/ripple.js';
import '../_common/js/utils/setStyle.js';
import '../config.js';
import './tab-panel-props.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@babel/runtime/helpers/readOnlyError';
import '@babel/runtime/helpers/typeof';
import 'vue';
import '@vue/composition-api';
import '../config-provider/context.js';
import '../_common/js/global-config/default-config.js';
import '../_common/js/global-config/locale/zh_CN.js';
import '../_chunks/dep-84fc5ae7.js';
import '../_chunks/dep-eb45b5e0.js';
import 'dayjs';
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var classPrefixMixins = getClassPrefixMixins("tab__nav");
var TTabNav = mixins(classPrefixMixins, getGlobalIconMixins()).extend({
name: "TTabNav",
components: {
TTabNavItem: TTabNavItem
},
props: {
theme: props.theme,
panels: {
type: Array,
"default": function _default() {
return [];
}
},
value: props.value,
placement: props.placement,
scrollPosition: props.scrollPosition,
size: props.size,
disabled: props.disabled,
addable: props.addable
},
data: function data() {
return {
scrollLeft: 0,
maxScrollLeft: 0,
navBarStyle: {}
};
},
computed: {
navs: function navs() {
var _this = this;
return this.panels.map(function (panel, index) {
return {
ref: "tabItem".concat(index),
key: panel.value,
theme: _this.theme,
size: _this.size,
placement: _this.placement,
active: panel.value === _this.value,
disabled: _this.disabled || panel.disabled,
removable: panel.removable,
value: panel.value,
panel: panel
};
});
},
wrapTransformStyle: function wrapTransformStyle() {
if (["left", "right"].includes(this.placement.toLowerCase())) return {};
return {
transform: "translate(".concat(-this.scrollLeft, "px, 0)")
};
},
dataCanUpdateNavBarStyle: function dataCanUpdateNavBarStyle() {
return [this.scrollLeft, this.placement, this.theme, this.navs, this.value];
},
iconBaseClass: function iconBaseClass() {
return _defineProperty(_defineProperty(_defineProperty({}, "".concat(this.classPrefix, "-tabs__btn"), true), "".concat(this.classPrefix, "-size-m"), this.size === "medium"), "".concat(this.classPrefix, "-size-l"), this.size === "large");
},
leftIconClass: function leftIconClass() {
return _objectSpread(_defineProperty({}, "".concat(this.classPrefix, "-tabs__btn--left"), true), this.iconBaseClass);
},
rightIconClass: function rightIconClass() {
return _objectSpread(_defineProperty({}, "".concat(this.classPrefix, "-tabs__btn--right"), true), this.iconBaseClass);
},
addIconClass: function addIconClass() {
return _objectSpread(_defineProperty({}, "".concat(this.classPrefix, "-tabs__add-btn"), true), this.iconBaseClass);
},
navContainerClass: function navContainerClass() {
return _defineProperty(_defineProperty(_defineProperty(_defineProperty({}, "".concat(this.classPrefix, "-tabs__nav-container"), true), "".concat(this.classPrefix, "-tabs__nav--card"), this.theme === "card"), "".concat(this.classPrefix, "-is-").concat(this.placement), true), "".concat(this.classPrefix, "-is-addable"), this.theme === "card" && this.addable);
},
navScrollContainerClass: function navScrollContainerClass() {
return _defineProperty(_defineProperty({}, "".concat(this.classPrefix, "-tabs__nav-scroll"), true), "".concat(this.classPrefix, "-is-scrollable"), this.canToLeft || this.canToRight);
},
navsWrapClass: function navsWrapClass() {
return ["".concat(this.classPrefix, "-tabs__nav-wrap"), "".concat(this.classPrefix, "-is-smooth"), _defineProperty({}, "".concat(this.classPrefix, "-is-vertical"), this.placement === "left" || this.placement === "right")];
},
navBarClass: function navBarClass() {
return ["".concat(this.classPrefix, "-tabs__bar"), "".concat(this.classPrefix, "-is-").concat(this.placement)];
},
navsContainerStyle: function navsContainerStyle() {
return this.addable ? {
"min-height": "48px"
} : null;
},
activeElement: function activeElement() {
var activeIndx = this.navs.findIndex(function (nav) {
return nav.active;
});
if (activeIndx > -1) {
var _this$$refs;
return (_this$$refs = this.$refs["tabItem".concat(activeIndx)]) === null || _this$$refs === void 0 ? void 0 : _this$$refs.$el;
}
return null;
},
canToLeft: function canToLeft() {
return this.scrollLeft > 1;
},
canToRight: function canToRight() {
return this.scrollLeft < this.maxScrollLeft - 1;
}
},
watch: {
dataCanUpdateNavBarStyle: function dataCanUpdateNavBarStyle() {
var _this2 = this;
this.$nextTick(function () {
_this2.calculateNavBarStyle();
});
},
value: function value() {
var _this3 = this;
this.$nextTick(function () {
_this3.moveActiveTabIntoView();
});
},
navs: function navs() {
var _this4 = this;
this.$nextTick(function () {
_this4.getMaxScrollLeft();
_this4.moveActiveTabIntoView();
});
}
},
methods: {
calculateNavBarStyle: function calculateNavBarStyle() {
var _this5 = this;
var getNavBarStyle = function getNavBarStyle() {
if (_this5.theme === "card") return {};
var getPropName = function getPropName() {
if (["left", "right"].includes(_this5.placement.toLowerCase())) {
return ["height", "top"];
}
return ["width", "left"];
};
var offset = 0;
var activeElement = _this5.activeElement;
if (!activeElement) return {};
var _getPropName = getPropName(),
_getPropName2 = _slicedToArray(_getPropName, 2),
sizePropName = _getPropName2[0],
offsetPropName = _getPropName2[1];
var i = 0;
for (; i < _this5.navs.length; i++) {
var _this5$$refs;
if (_this5.navs[i].active) {
break;
}
offset += ((_this5$$refs = _this5.$refs["tabItem".concat(i)]) === null || _this5$$refs === void 0 || (_this5$$refs = _this5$$refs.$el) === null || _this5$$refs === void 0 ? void 0 : _this5$$refs["client".concat(firstUpperCase(sizePropName))]) || 0;
}
return _defineProperty(_defineProperty({}, offsetPropName, "".concat(offset, "px")), sizePropName, "".concat((activeElement === null || activeElement === void 0 ? void 0 : activeElement["client".concat(firstUpperCase(sizePropName))]) || 0, "px"));
};
this.navBarStyle = getNavBarStyle();
},
watchDomChange: function watchDomChange() {
var _this6 = this;
var onResize = debounce(function () {
_this6.getMaxScrollLeft();
}, 300);
window.addEventListener("resize", onResize);
this.$once("beforeDestroy", function () {
window.removeEventListener("resize", onResize);
});
if (!this.$refs.navsContainer) return;
if (!window.ResizeObserver) return;
var resizeObserver = new window.ResizeObserver(onResize);
resizeObserver.observe(this.$refs.navsContainer);
this.$once("beforeDestroy", function () {
resizeObserver.disconnect();
});
},
setOffset: function setOffset(offset) {
this.scrollLeft = calcValidOffset(offset, this.maxScrollLeft);
},
getMaxScrollLeft: function getMaxScrollLeft() {
this.maxScrollLeft = calcMaxOffset({
navsWrap: this.$refs.navsWrap,
navsContainer: this.$refs.navsContainer,
rightOperations: this.$refs.rightOperationsZone,
toRightBtn: this.$refs.rightIcon
});
},
handleScroll: function handleScroll(action) {
var offset = calcPrevOrNextOffset({
navsContainer: this.$refs.navsContainer,
activeTab: this.activeElement
}, this.scrollLeft, action);
this.setOffset(offset);
},
handleWheel: function handleWheel(e) {
if (!this.canToLeft && !this.canToRight) return;
e.preventDefault();
var deltaX = e.deltaX,
deltaY = e.deltaY;
if (Math.abs(deltaX) > Math.abs(deltaY)) {
this.setOffset(this.scrollLeft + deltaX);
} else {
this.setOffset(this.scrollLeft + deltaY);
}
},
moveActiveTabIntoView: function moveActiveTabIntoView() {
if (["left", "right"].includes(this.placement.toLowerCase())) return;
this.setOffset(calculateOffset({
activeTab: this.activeElement,
navsContainer: this.$refs.navsContainer,
leftOperations: this.$refs.leftOperationsZone,
rightOperations: this.$refs.rightOperationsZone
}, this.scrollLeft, this.scrollPosition));
},
handleAddTab: function handleAddTab(e) {
emitEvent(this, "add", {
e: e
});
},
tabClick: function tabClick(event, nav) {
var value = nav.value,
disabled = nav.disabled;
if (disabled || this.value === value) {
return false;
}
emitEvent(this, "change", value);
},
removeBtnClick: function removeBtnClick(_ref6) {
var e = _ref6.e,
value = _ref6.value,
index = _ref6.index;
emitEvent(this, "remove", {
e: e,
value: value,
index: index
});
},
renderPanelContent: function renderPanelContent() {
var _this7 = this;
var h = this.$createElement;
return this.navs.map(function (panel, index) {
return h(TTabNavItem, {
"ref": "tabItem".concat(index),
"attrs": {
"index": index,
"theme": panel.theme,
"size": panel.size,
"placement": panel.placement,
"active": panel.active,
"disabled": panel.disabled,
"removable": panel.removable,
"value": panel.value,
"label": renderTNodeJSX(panel.panel, "label", "\u9009\u9879\u5361".concat(index + 1))
},
"key": panel.value,
"on": {
"click": function click(e) {
return _this7.tabClick(e, panel.panel);
},
"remove": _this7.removeBtnClick
}
});
});
},
renderArrows: function renderArrows() {
var _this8 = this;
var h = this.$createElement;
var _this$useGlobalIcon = this.useGlobalIcon({
ChevronLeftIcon: ChevronLeftIcon,
ChevronRightIcon: ChevronRightIcon,
AddIcon: AddIcon
}),
ChevronLeftIcon$1 = _this$useGlobalIcon.ChevronLeftIcon,
ChevronRightIcon$1 = _this$useGlobalIcon.ChevronRightIcon,
AddIcon$1 = _this$useGlobalIcon.AddIcon;
return [h("div", {
"ref": "leftOperationsZone",
"class": ["".concat(this.classPrefix, "-tabs__operations"), "".concat(this.classPrefix, "-tabs__operations--left")]
}, [h("transition", {
"attrs": {
"name": "fade",
"mode": "out-in",
"appear": true
}
}, [this.canToLeft ? h("div", {
"ref": "leftIcon",
"class": this.leftIconClass,
"on": {
"click": function click() {
return _this8.handleScroll("prev");
}
}
}, [h(ChevronLeftIcon$1)]) : null])]), h("div", {
"ref": "rightOperationsZone",
"class": ["".concat(this.classPrefix, "-tabs__operations"), "".concat(this.classPrefix, "-tabs__operations--right")]
}, [h("transition", {
"attrs": {
"name": "fade",
"mode": "out-in",
"appear": true
}
}, [this.canToRight ? h("div", {
"ref": "rightIcon",
"class": this.rightIconClass,
"on": {
"click": function click() {
return _this8.handleScroll("next");
}
}
}, [h(ChevronRightIcon$1)]) : null]), this.addable ? h("div", {
"class": this.addIconClass,
"on": {
"click": this.handleAddTab
}
}, [h(AddIcon$1)]) : null])];
},
renderNavs: function renderNavs() {
var h = this.$createElement;
return h("div", {
"class": this.navContainerClass
}, [h("div", {
"class": this.navScrollContainerClass,
"on": {
"wheel": this.handleWheel
}
}, [h("div", {
"ref": "navsWrap",
"class": this.navsWrapClass,
"style": this.wrapTransformStyle
}, [this.renderNavBar(), this.renderPanelContent()])])]);
},
renderNavBar: function renderNavBar() {
var h = this.$createElement;
if (this.theme === "card") return null;
return h("div", {
"class": this.navBarClass,
"style": this.navBarStyle
});
}
},
mounted: function mounted() {
var _this9 = this;
this.$nextTick(function () {
_this9.watchDomChange();
_this9.calculateNavBarStyle();
_this9.getMaxScrollLeft();
});
setTimeout(function () {
_this9.moveActiveTabIntoView();
});
},
render: function render() {
var h = arguments[0];
return h("div", {
"ref": "navsContainer",
"class": ["".concat(this.classPrefix, "-tabs__nav")],
"style": this.navsContainerStyle
}, [this.renderArrows(), this.renderNavs()]);
}
});
export { TTabNav as default };
//# sourceMappingURL=tab-nav.js.map