tdesign-vue
Version:
221 lines (217 loc) • 8.62 kB
JavaScript
/**
* tdesign v1.14.1
* (c) 2025 tdesign
* @license MIT
*/
import _asyncToGenerator from '@babel/runtime/helpers/asyncToGenerator';
import { h as helper } from '../_chunks/dep-6a4dc7bb.js';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _regeneratorRuntime from '@babel/runtime/regenerator';
import { isNumber, isString, throttle } from 'lodash-es';
import props from './radio-group-props.js';
import _Radio from './radio.js';
import _RadioButton from './radio-button.js';
import { emitEvent } from '../utils/event.js';
import { getClassPrefixMixins } from '../config-provider/config-receiver.js';
import mixins from '../utils/mixins.js';
import { on, off } from '../utils/dom.js';
import { CHECKED_CODE_REG } from '../_common/js/common.js';
import 'vue';
import '../utils/helper.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@babel/runtime/helpers/slicedToArray';
import '../utils/render-tnode.js';
import '@babel/runtime/helpers/readOnlyError';
import '@babel/runtime/helpers/typeof';
import '@vue/composition-api';
import './props.js';
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-ba613a02.js';
import '../_chunks/dep-fdb1b253.js';
import 'dayjs';
import '../_common/js/global-config/t.js';
import 'raf';
import '../utils/easing.js';
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("radio-group");
var _RadioGroup = mixins(classPrefixMixins).extend({
name: "TRadioGroup",
props: _objectSpread({}, props),
components: {
Radio: _Radio
},
provide: function provide() {
return {
radioGroup: this
};
},
data: function data() {
return {
groupResizeObserver: null,
barStyle: {
width: "0px",
left: "0px"
}
};
},
computed: {
checkedClassName: function checkedClassName() {
return ".".concat(this.classPrefix, "-radio-button.").concat(this.commonStatusClassName.checked);
}
},
render: function render(h) {
var _this = this;
var $scopedSlots = this.$scopedSlots;
var children = $scopedSlots["default"] && $scopedSlots["default"](null);
if (this.options && this.options.length) {
children = this.options.map(function (option, index) {
var opt = option;
if (isNumber(option) || isString(option)) {
opt = {
value: option,
label: option.toString()
};
}
var RadioComponent = _this.theme === "button" ? _RadioButton : _Radio;
return h(RadioComponent, helper([{}, {
"props": option
}, {
"key": "radio-group-options-".concat(opt.value, "-").concat(index),
"attrs": {
"name": _this.name,
"checked": _this.value === opt.value,
"disabled": "disabled" in opt ? opt.disabled : _this.disabled,
"value": opt.value
}
}]), [typeof opt.label === "function" ? opt.label(h) : opt.label]);
});
}
var groupClass = ["".concat(this.componentName), this.commonSizeClassName[this.size], _defineProperty(_defineProperty(_defineProperty({}, "".concat(this.componentName, "__outline"), this.variant === "outline"), "".concat(this.componentName, "--filled"), this.variant.includes("filled")), "".concat(this.componentName, "--primary-filled"), this.variant === "primary-filled")];
if (this.variant.includes("filled")) {
children && children.push(h("div", {
"style": this.barStyle,
"class": "".concat(this.componentName, "__bg-block")
}));
}
return h("div", {
"class": groupClass
}, [children]);
},
watch: {
value: function value() {
var _this2 = this;
this.$nextTick(function () {
return _this2.calcBarStyle();
});
}
},
mounted: function mounted() {
var _this3 = this;
this.calcBarStyle();
this.groupResizeObserver = this.addResizeObserver(this.$el, throttle(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
return _regeneratorRuntime.wrap(function (_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
_this3.$nextTick(function () {
return _this3.calcBarStyle();
});
case 1:
case "end":
return _context.stop();
}
}, _callee);
})), 300));
this.addKeyboardListeners();
},
beforeDestroy: function beforeDestroy() {
this.removeKeyboardListeners();
this.cleanupResizeObserver(this.groupResizeObserver, this.$el);
},
methods: {
addKeyboardListeners: function addKeyboardListeners() {
on(this.$el, "keydown", this.checkRadioInGroup);
},
removeKeyboardListeners: function removeKeyboardListeners() {
off(this.$el, "keydown", this.checkRadioInGroup);
},
addResizeObserver: function addResizeObserver(el, callback) {
var isSupport = typeof window !== "undefined" && window.ResizeObserver;
if (!isSupport) return;
var containerObserver = new ResizeObserver(callback);
containerObserver.observe(el);
return containerObserver;
},
cleanupResizeObserver: function cleanupResizeObserver(observer, container) {
if (!observer || !container) return;
observer.unobserve(container);
observer.disconnect();
},
checkRadioInGroup: function checkRadioInGroup(e) {
var isCheckedCode = CHECKED_CODE_REG.test(e.key) || CHECKED_CODE_REG.test(e.code);
if (isCheckedCode) {
e.preventDefault();
var inputNode = e.target.querySelector("input");
var data = inputNode.dataset;
if (inputNode.checked && data.allowUncheck) {
this.handleRadioChange(void 0, {
e: e
});
} else {
var value = !isNaN(Number(data.value)) ? Number(data.value) : data.value;
value = typeof value === "string" && {
"true": true,
"false": false
}[value] || value;
value = typeof value === "string" && value[0] === "'" ? value.replace(/'/g, "") : value;
this.handleRadioChange(value, {
e: e
});
}
}
},
handleRadioChange: function handleRadioChange(value, context) {
emitEvent(this, "change", value, context);
},
calcDefaultBarStyle: function calcDefaultBarStyle() {
var defaultNode = this.$el.cloneNode(true);
var div = document.createElement("div");
div.setAttribute("style", "position: absolute; visibility: hidden;");
div.appendChild(defaultNode);
document.body.appendChild(div);
var defaultCheckedRadio = div.querySelector(this.checkedClassName);
var offsetWidth = defaultCheckedRadio.offsetWidth,
offsetLeft = defaultCheckedRadio.offsetLeft;
document.body.removeChild(div);
return {
offsetWidth: offsetWidth,
offsetLeft: offsetLeft
};
},
calcBarStyle: function calcBarStyle() {
if (this.variant === "outline") return;
var checkedRadio = this.$el.querySelector(this.checkedClassName);
if (!checkedRadio) return;
var offsetWidth = checkedRadio.offsetWidth,
offsetLeft = checkedRadio.offsetLeft;
if (!offsetWidth) {
var _this$calcDefaultBarS = this.calcDefaultBarStyle(),
width2 = _this$calcDefaultBarS.offsetWidth,
left2 = _this$calcDefaultBarS.offsetLeft;
offsetWidth = width2;
offsetLeft = left2;
}
var width = "".concat(offsetWidth, "px");
var left = "".concat(offsetLeft, "px");
if (this.barStyle.width === width && this.barStyle.left === left) return;
this.barStyle = {
width: width,
left: left
};
}
}
});
export { _RadioGroup as default };
//# sourceMappingURL=group.js.map