UNPKG

tdesign-vue

Version:
288 lines (284 loc) 10.3 kB
/** * tdesign v1.14.1 * (c) 2025 tdesign * @license MIT */ import { h as helper } from '../_chunks/dep-6a4dc7bb.js'; import _typeof from '@babel/runtime/helpers/typeof'; import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray'; import _slicedToArray from '@babel/runtime/helpers/slicedToArray'; import { defineComponent, toRefs, ref, computed, watch, nextTick, onMounted, watchEffect, provide } from '@vue/composition-api'; import { intersection, isUndefined, isObject } from 'lodash-es'; import { getVNodeComponentName, getVueComponentName } from '../utils/helper.js'; import _Checkbox from './checkbox.js'; import props from './checkbox-group-props.js'; import { CheckboxGroupInjectionKey } from './constants.js'; import { createCheckboxStore } from './store.js'; import { useChildComponentSlots } from '../hooks/slots.js'; import { usePrefixClass } from '../hooks/useConfig.js'; import { useVModel } from '../hooks/useVModel.js'; import '@babel/runtime/helpers/objectWithoutProperties'; import '@babel/runtime/helpers/defineProperty'; import './props.js'; import './hooks/useKeyboardEvent.js'; import '../_common/js/common.js'; import '../hooks/useFormDisabled.js'; import '../hooks/useElementLazyRender.js'; import '../_common/js/utils/observe.js'; import '../utils/render-tnode.js'; import '@babel/runtime/helpers/readOnlyError'; import 'vue'; import '@babel/runtime/helpers/classCallCheck'; import '@babel/runtime/helpers/createClass'; import '../config-provider/useConfig.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'; var _CheckboxGroup = defineComponent({ name: "TCheckboxGroup", props: props, setup: function setup(props2) { var COMPONENT_NAME = usePrefixClass("checkbox-group"); var _createCheckboxStore = createCheckboxStore(), checkboxStore = _createCheckboxStore.checkboxStore, storeKey = _createCheckboxStore.storeKey; checkboxStore.init(); var isArray = Array.isArray; var _toRefs = toRefs(props2), value = _toRefs.value, disabled = _toRefs.disabled, name = _toRefs.name, options = _toRefs.options; var _useVModel = useVModel(value, props2.defaultValue, props2.onChange), _useVModel2 = _slicedToArray(_useVModel, 2), innerValue = _useVModel2[0], setInnerValue = _useVModel2[1]; var optionList = ref([]); var intersectionLen = computed(function () { if (!isArray(innerValue.value)) return 0; var values = optionList.value.map(function (item) { return item.value; }); var n = intersection(innerValue.value, values); return n.length; }); var isCheckAll = computed(function () { var optionItems = optionList.value.filter(function (item) { return !item.disabled && !item.checkAll; }).map(function (t) { return t.value; }); var intersectionValues = intersection(optionItems, innerValue.value); return intersectionValues.length === optionItems.length; }); var indeterminate = computed(function () { return !isCheckAll.value && intersectionLen.value < optionList.value.length && intersectionLen.value !== 0; }); var maxExceeded = computed(function () { return !isUndefined(props2.max) && innerValue.value.length === props2.max; }); watch([disabled, maxExceeded, name], function (_ref) { var _ref2 = _slicedToArray(_ref, 3), disabled2 = _ref2[0], maxExceeded2 = _ref2[1], checkboxName = _ref2[2]; checkboxStore.updateCheckbox({ disabled: disabled2, maxExceeded: maxExceeded2, checkboxName: checkboxName }); }); watch([options], function () { nextTick(function () { checkboxStore.updateCheckbox({ disabled: disabled.value, maxExceeded: maxExceeded.value, checkboxName: name.value }); }); }); onMounted(function () { checkboxStore.updateCheckbox({ disabled: disabled.value, maxExceeded: maxExceeded.value, checkboxName: name.value }); }); watchEffect(function () { if (!props2.options) return []; optionList.value = props2.options.map(function (item) { return isObject(item) ? item : { label: String(item), value: item }; }); }); var getAllCheckboxValue = function getAllCheckboxValue() { var val = /* @__PURE__ */new Set(); for (var i = 0, len = optionList.value.length; i < len; i++) { var item = optionList.value[i]; if (item.checkAll) continue; if (item.disabled) continue; val.add(item.value); if (maxExceeded.value) break; } return _toConsumableArray(val); }; var onCheckAllChange = function onCheckAllChange(checked, context) { var value2 = checked ? getAllCheckboxValue() : []; setInnerValue(value2, { e: context.e, type: checked ? "check" : "uncheck", current: void 0, option: void 0 }); }; var handleCheckboxChange = function handleCheckboxChange(data) { var currentValue = data.option.value; if (!isArray(innerValue.value)) { console.warn("TDesign CheckboxGroup Warn: `value` must be an array, instead of ".concat(_typeof(innerValue.value))); return; } var val = _toConsumableArray(innerValue.value); if (data.checked) { val.push(currentValue); } else { var i = val.indexOf(currentValue); val.splice(i, 1); } setInnerValue(val, { e: data.e, current: data.option.value, option: data.option, type: data.checked ? "check" : "uncheck" }); }; var onCheckedChange = function onCheckedChange(p) { var checked = p.checked, checkAll = p.checkAll, e = p.e; if (checkAll) { onCheckAllChange(checked, { e: e }); } else { handleCheckboxChange(p); } }; var getChildComponentSlots = useChildComponentSlots(); var getOptionListBySlots = function getOptionListBySlots() { var nodes = getChildComponentSlots("Checkbox"); var arr = []; nodes === null || nodes === void 0 || nodes.forEach(function (node) { var option = node.componentOptions.propsData; if (!option) return; if (option["check-all"] === "" || option["check-all"] === true) { option.checkAll = true; } arr.push(option); }); return arr; }; provide(CheckboxGroupInjectionKey, computed(function () { return { handleCheckboxChange: handleCheckboxChange, onCheckedChange: onCheckedChange }; })); watch(function () { return _toConsumableArray(innerValue.value); }, function (val, oldValue) { nextTick(function () { checkboxStore.updateChecked({ checked: val, oldChecked: oldValue, isCheckAll: isCheckAll.value, indeterminate: indeterminate.value }); }); }, { immediate: true }); watch([isCheckAll, indeterminate, options], function (_ref3) { var _ref4 = _slicedToArray(_ref3, 2), isCheckAll2 = _ref4[0], indeterminate2 = _ref4[1]; nextTick(function () { checkboxStore.updateChecked({ checked: innerValue.value, isCheckAll: isCheckAll2, indeterminate: indeterminate2 }); }); }); var _addStoreKeyToCheckbox = function addStoreKeyToCheckbox(nodes) { if (!nodes) return; for (var i = 0, len = nodes.length; i < len; i++) { var _vNode$children; var vNode = nodes[i]; var componentName = getVNodeComponentName(vNode); if (vNode.componentOptions && componentName && componentName === getVueComponentName(_Checkbox)) { vNode.componentOptions.propsData.storeKey = storeKey; } if ((_vNode$children = vNode.children) !== null && _vNode$children !== void 0 && _vNode$children.length) { _addStoreKeyToCheckbox(vNode.children); } } }; return { storeKey: storeKey, optionList: optionList, innerValue: innerValue, COMPONENT_NAME: COMPONENT_NAME, getOptionListBySlots: getOptionListBySlots, addStoreKeyToCheckbox: _addStoreKeyToCheckbox }; }, render: function render() { var _this$options, _this = this; var h = arguments[0]; var children = null; if ((_this$options = this.options) !== null && _this$options !== void 0 && _this$options.length) { var _this$optionList; children = (_this$optionList = this.optionList) === null || _this$optionList === void 0 ? void 0 : _this$optionList.map(function (option, index) { var _option$value, _this$innerValue; return h(_Checkbox, helper([{ "key": (_option$value = option.value) !== null && _option$value !== void 0 ? _option$value : index, "attrs": { "lazyLoad": _this.lazyLoad } }, { "props": option }, { "attrs": { "index": index, "data": option, "checked": ((_this$innerValue = _this.innerValue) === null || _this$innerValue === void 0 ? void 0 : _this$innerValue.includes(option.value)) || false, "storeKey": _this.storeKey }, "scopedSlots": _this.$scopedSlots }])); }); } else { var _this$$scopedSlots$de, _this$$scopedSlots; var nodes = (_this$$scopedSlots$de = (_this$$scopedSlots = this.$scopedSlots)["default"]) === null || _this$$scopedSlots$de === void 0 ? void 0 : _this$$scopedSlots$de.call(_this$$scopedSlots, null); this.optionList = this.getOptionListBySlots(); this.addStoreKeyToCheckbox(nodes); children = nodes; } return h("div", { "class": this.COMPONENT_NAME, "attrs": { "role": "group", "aria-label": "checkbox-group" } }, [children]); } }); export { _CheckboxGroup as default }; //# sourceMappingURL=group.js.map