UNPKG

tdesign-vue

Version:
186 lines (182 loc) 6.44 kB
/** * tdesign v1.12.1 * (c) 2025 tdesign * @license MIT */ import _defineProperty from '@babel/runtime/helpers/defineProperty'; import { Loading } from '../loading/index.js'; import props from './props.js'; import { getClassPrefixMixins } from '../config-provider/config-receiver.js'; import mixins from '../utils/mixins.js'; import '../loading/directive.js'; import 'lodash-es'; import '../loading/plugin.js'; import 'vue'; import '../loading/loading.js'; import '../loading/icon/gradient.js'; import '../_common/js/loading/circle-adapter.js'; import '../_common/js/utils/setStyle.js'; import '../_common/js/utils/helper.js'; import '@babel/runtime/helpers/toConsumableArray'; import '@babel/runtime/helpers/objectWithoutProperties'; import '@babel/runtime/helpers/slicedToArray'; import '../utils/dom.js'; import 'raf'; import '../utils/easing.js'; import '../utils/render-tnode.js'; import '@babel/runtime/helpers/readOnlyError'; import '@babel/runtime/helpers/typeof'; import '@vue/composition-api'; import '../utils/transfer-dom.js'; import '../loading/props.js'; import '../config.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-c44a474d.js'; import '../_chunks/dep-d639fbd7.js'; import 'dayjs'; import '../_chunks/dep-3c66615e.js'; import '../config-provider/type.js'; import '../_common/js/global-config/t.js'; import '../utils/withInstall.js'; import './style/css.js'; import '../loading/type.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("switch"); var _Switch = mixins(classPrefixMixins).extend({ name: "TSwitch", props: _objectSpread({}, props), model: { prop: "value", event: "change" }, data: function data() { return { formDisabled: void 0 }; }, computed: { classes: function classes() { return ["".concat(this.componentName), this.commonSizeClassName[this.size], _defineProperty(_defineProperty(_defineProperty({}, this.commonStatusClassName.disabled, this.tDisabled), this.commonStatusClassName.loading, this.loading), this.commonStatusClassName.checked, this.value === this.activeValue)]; }, nodeClasses: function nodeClasses() { return ["".concat(this.componentName, "__handle"), _defineProperty(_defineProperty({}, this.commonStatusClassName.disabled, this.tDisabled), this.commonStatusClassName.loading, this.loading)]; }, contentClasses: function contentClasses() { return ["".concat(this.componentName, "__content"), this.commonSizeClassName[this.size], _defineProperty({}, this.commonStatusClassName.disabled, this.tDisabled)]; }, tDisabled: function tDisabled() { return this.formDisabled || this.disabled; }, activeValue: function activeValue() { if (this.customValue && this.customValue.length > 0) { return this.customValue[0]; } return true; }, inactiveValue: function inactiveValue() { if (this.customValue && this.customValue.length > 1) { return this.customValue[1]; } return false; }, content: function content() { if (typeof this.label === "function") { return this.label(this.$createElement, { value: this.value }); } if (typeof this.label === "string") { return this.label; } if (Array.isArray(this.label)) { var label = this.value === this.activeValue ? this.label[0] : this.label[1]; if (!label) return; if (typeof label === "string") { return label; } if (typeof label === "function") { return label(this.$createElement); } } if (this.$scopedSlots.label) { return this.$scopedSlots.label({ value: this.value }); } return null; } }, watch: { value: { handler: function handler(val) { if (this.customValue && this.customValue.length && !this.customValue.includes(val)) { throw "value is not in ".concat(JSON.stringify(this.customValue)); } }, immediate: true } }, methods: { handleToggle: function handleToggle() { var checked = this.value === this.activeValue ? this.inactiveValue : this.activeValue; typeof this.onChange === "function" && this.onChange(checked); this.$emit("change", checked); }, toggle: function toggle() { var _this = this; if (this.tDisabled || this.loading) { return; } if (!this.beforeChange) { this.handleToggle(); return; } Promise.resolve(this.beforeChange()).then(function (v) { if (v) { _this.handleToggle(); } })["catch"](function (e) { throw new Error("Switch: some error occurred: ".concat(e)); }); } }, render: function render() { var h = arguments[0]; var loading = this.loading, disabled = this.disabled, content = this.content, nodeClasses = this.nodeClasses, classes = this.classes, toggle = this.toggle, contentClasses = this.contentClasses; var switchContent; var loadingContent; if (loading) { loadingContent = h(Loading, { "attrs": { "size": "small" } }); } else if (content) { switchContent = content; } return h("div", { "class": classes, "attrs": { "disabled": disabled }, "on": { "click": toggle } }, [h("span", { "class": nodeClasses }, [loadingContent]), h("div", { "class": contentClasses }, [switchContent])]); } }); export { _Switch as default }; //# sourceMappingURL=switch.js.map