UNPKG

tdesign-mobile-vue

Version:
154 lines (150 loc) 6.53 kB
/** * tdesign v1.15.0 * (c) 2026 TDesign Group * @license MIT */ import { _ as _defineProperty } from '../_chunks/dep-71bf849e.mjs'; import { _ as _slicedToArray } from '../_chunks/dep-a3709177.mjs'; import { defineComponent, toRefs, ref, computed, nextTick, watch, onMounted, createVNode, h } from 'vue'; import { isFunction } from 'lodash-es'; import config from '../config.mjs'; import props from './props.mjs'; import useVModel from '../hooks/useVModel.mjs'; import { usePrefixClass } from '../hooks/useClass.mjs'; import { useResizeObserver } from '../hooks/useResizeObserver.mjs'; import '../_chunks/dep-fe785552.mjs'; import '../_chunks/dep-8efef78c.mjs'; import '../config-provider/useConfig.mjs'; import '../config-provider/context.mjs'; import '../_common/js/global-config/mobile/default-config.mjs'; import '../_common/js/global-config/mobile/locale/zh_CN.mjs'; import '../_chunks/dep-e4589fcf.mjs'; import '../_chunks/dep-9fdda17b.mjs'; import '../_chunks/dep-7fbd1cf5.mjs'; import '../_chunks/dep-0f7db25a.mjs'; import '../config-provider/type.mjs'; 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.mjs.map