tdesign-mobile-vue
Version:
tdesign-mobile-vue
151 lines (147 loc) • 6.38 kB
JavaScript
/**
* tdesign v1.15.0
* (c) 2026 TDesign Group
* @license MIT
*/
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import { defineComponent, toRefs, ref, computed, watch, nextTick, onMounted, createVNode, h } from 'vue';
import { isFunction } from 'lodash-es';
import config from '../config.js';
import props from './props.js';
import useVModel from '../hooks/useVModel.js';
import { usePrefixClass } from '../hooks/useClass.js';
import { useResizeObserver } from '../hooks/useResizeObserver.js';
import '../config-provider/useConfig.js';
import '../config-provider/context.js';
import '../_common/js/global-config/mobile/default-config.js';
import '../_common/js/global-config/mobile/locale/zh_CN.js';
import '../_chunks/dep-5428cfc4.js';
import '@babel/runtime/helpers/typeof';
import '../_chunks/dep-db9f269e.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 prefix = config.prefix;
var _Segmented = defineComponent({
name: "".concat(prefix, "-segmented"),
props: props,
emits: ["update:value", "update:modelValue", "change"],
setup: function setup(props2) {
var segmentedClass = usePrefixClass("segmented");
var _toRefs = toRefs(props2),
value = _toRefs.value,
modelValue = _toRefs.modelValue;
var _useVModel = useVModel(value, modelValue, props2.defaultValue),
_useVModel2 = _slicedToArray(_useVModel, 2),
innerValue = _useVModel2[0],
setInnerValue = _useVModel2[1];
var thumbStyle = ref({});
var groupRef = ref();
var itemRefs = ref([]);
var segmentItems = computed(function () {
var _props2$options;
if (!((_props2$options = props2.options) !== null && _props2$options !== void 0 && _props2$options.length)) return [];
return props2.options.map(function (option) {
var _option$label;
if (typeof option === "string" || typeof option === "number") {
return {
value: option,
label: String(option)
};
}
return _objectSpread(_objectSpread({}, option), {}, {
label: (_option$label = option.label) !== null && _option$label !== void 0 ? _option$label : String(option.value)
});
});
});
var activeIndex = computed(function () {
if (innerValue.value == null) return -1;
return segmentItems.value.findIndex(function (item) {
return item.value === innerValue.value;
});
});
var updateThumb = function updateThumb() {
if (activeIndex.value < 0) {
thumbStyle.value = {};
return;
}
nextTick(function () {
var groupEl = groupRef.value;
var itemEl = itemRefs.value[activeIndex.value];
if (!groupEl || !itemEl) return;
var groupRect = groupEl.getBoundingClientRect();
var itemRect = itemEl.getBoundingClientRect();
thumbStyle.value = {
width: "".concat(itemRect.width, "px"),
transform: "translateX(".concat(itemRect.left - groupRect.left, "px)")
};
});
};
var handleSelect = function handleSelect(index) {
var _props2$onChange;
var item = segmentItems.value[index];
if (props2.disabled || !item || item.disabled) return;
if (index === activeIndex.value) return;
setInnerValue(item.value);
(_props2$onChange = props2.onChange) === null || _props2$onChange === void 0 || _props2$onChange.call(props2, {
value: item.value,
selectedOption: item
});
};
watch(activeIndex, function () {
updateThumb();
});
watch(function () {
return props2.options;
}, function () {
nextTick(function () {
updateThumb();
});
}, {
deep: true
});
useResizeObserver(groupRef, function () {
return updateThumb();
}, {
onVisibilityChange: true
});
onMounted(function () {
updateThumb();
});
return function () {
var rootClasses = [segmentedClass.value, _defineProperty({}, "".concat(segmentedClass.value, "--block"), props2.block)];
return createVNode("div", {
"class": rootClasses
}, [createVNode("div", {
"class": "".concat(segmentedClass.value, "__group"),
"ref": groupRef
}, [createVNode("div", {
"class": "".concat(segmentedClass.value, "__thumb"),
"style": thumbStyle.value
}, null), segmentItems.value.map(function (item, index) {
var itemClasses = ["".concat(segmentedClass.value, "__item"), "".concat(segmentedClass.value, "-item-").concat(index), _defineProperty(_defineProperty({}, "".concat(segmentedClass.value, "__item--active"), index === activeIndex.value), "".concat(segmentedClass.value, "__item--disabled"), props2.disabled || item.disabled)];
var iconNode = item.icon && createVNode("span", {
"class": "".concat(segmentedClass.value, "__item-icon")
}, [isFunction(item.icon) ? item.icon(h) : item.icon]);
var labelNode = item.label && createVNode("span", {
"class": "".concat(segmentedClass.value, "__item-label")
}, [isFunction(item.label) ? item.label(h) : item.label]);
return createVNode("div", {
"key": item.value,
"ref": function ref(el) {
itemRefs.value[index] = el;
},
"class": itemClasses,
"onClick": function onClick() {
return handleSelect(index);
}
}, [createVNode("div", {
"class": "".concat(segmentedClass.value, "__item-inner")
}, [iconNode, labelNode])]);
})])]);
};
}
});
export { _Segmented as default };
//# sourceMappingURL=segmented.js.map