UNPKG

@cqmcui/cqmcui

Version:

轻量级移动端 Vue2、Vue3 组件库(支持小程序开发)

102 lines (101 loc) 2.75 kB
import { reactive, provide, computed, watch, h } from "vue"; import { c as createComponent } from "./component-81a4c1d0.js"; import { u as useExpose } from "./index-79c5dc33.js"; import "../locale/lang"; const { create, componentName } = createComponent("checkbox-group"); const _sfc_main = create({ props: { modelValue: { type: Array, default: () => [] }, disabled: { type: Boolean, default: false }, max: { type: Number, default: 0 } }, emits: ["change", "update:modelValue"], setup(props, { slots, emit }) { const state = reactive({ children: [] }); const relation = (child, cancel = false) => { var _a; if (child.proxy) { if (!cancel) { state.children.push(child.proxy); } else { let key_1 = state.children.indexOf(child.proxy); if (key_1 > -1) { state.children.splice(key_1, 1); } let key_2 = props.modelValue.indexOf((_a = child.proxy) == null ? void 0 : _a.label); if (key_2 > -1) { const value = props.modelValue.filter((_, index) => index !== key_2); emit("update:modelValue", value); } } } }; const updateValue = (value) => { emit("update:modelValue", value); emit("change", value); }; const toggleAll = (checked) => { let values = []; if (!!checked) { state.children.forEach((item) => { if (!(item == null ? void 0 : item.disabled)) { values.push(item == null ? void 0 : item.label); } }); } emit("update:modelValue", values); }; const toggleReverse = () => { let values = props.modelValue.slice(); state.children.forEach((item) => { let findIndex = values.findIndex((value) => value === item.label); if (findIndex > -1) { values.splice(findIndex, 1); } else { if (!(item == null ? void 0 : item.disabled)) { values.push(item == null ? void 0 : item.label); } } }); emit("update:modelValue", values); }; provide("parent", { value: computed(() => props.modelValue), disabled: computed(() => props.disabled), max: computed(() => props.max), updateValue, relation }); watch( () => props.modelValue, (value) => { emit("change", value); } ); useExpose({ toggleAll, toggleReverse }); return () => { var _a; return h( "view", { class: componentName }, (_a = slots.default) == null ? void 0 : _a.call(slots) ); }; } }); export { _sfc_main as default };