element-plus
Version:
A Component Library for Vue 3
115 lines (112 loc) • 4.2 kB
JavaScript
import { defineComponent, computed, provide, toRefs, watch, openBlock, createBlock, resolveDynamicComponent, unref, normalizeClass, withCtx, renderSlot, createElementBlock, Fragment, renderList, mergeProps, nextTick } from 'vue';
import { pick, isEqual, omit } from 'lodash-unified';
import { checkboxGroupProps, checkboxGroupEmits, checkboxDefaultProps } from './checkbox-group.mjs';
import { checkboxGroupContextKey } from './constants.mjs';
import Checkbox from './checkbox2.mjs';
import CheckboxButton from './checkbox-button.mjs';
import _export_sfc from '../../../_virtual/plugin-vue_export-helper.mjs';
import { useNamespace } from '../../../hooks/use-namespace/index.mjs';
import { useFormDisabled } from '../../form/src/hooks/use-form-common-props.mjs';
import { useFormItem, useFormItemInputId } from '../../form/src/hooks/use-form-item.mjs';
import { debugWarn } from '../../../utils/error.mjs';
import { UPDATE_MODEL_EVENT, CHANGE_EVENT } from '../../../constants/event.mjs';
const _sfc_main = defineComponent({
...{
name: "ElCheckboxGroup"
},
__name: "checkbox-group",
props: checkboxGroupProps,
emits: checkboxGroupEmits,
setup(__props, { emit: __emit }) {
const props = __props;
const emit = __emit;
const ns = useNamespace("checkbox");
const checkboxDisabled = useFormDisabled();
const { formItem } = useFormItem();
const { inputId: groupId, isLabeledByFormItem } = useFormItemInputId(props, {
formItemContext: formItem
});
const changeEvent = async (value) => {
emit(UPDATE_MODEL_EVENT, value);
await nextTick();
emit(CHANGE_EVENT, value);
};
const modelValue = computed({
get() {
return props.modelValue;
},
set(val) {
changeEvent(val);
}
});
const aliasProps = computed(() => ({
...checkboxDefaultProps,
...props.props
}));
const getOptionProps = (option) => {
const { label, value, disabled } = aliasProps.value;
const base = {
label: option[label],
value: option[value],
disabled: option[disabled]
};
return { ...omit(option, [label, value, disabled]), ...base };
};
const optionComponent = computed(
() => props.type === "button" ? CheckboxButton : Checkbox
);
provide(checkboxGroupContextKey, {
...pick(toRefs(props), [
"size",
"min",
"max",
"validateEvent",
"fill",
"textColor"
]),
disabled: checkboxDisabled,
modelValue,
changeEvent
});
watch(
() => props.modelValue,
(newVal, oldValue) => {
if (props.validateEvent && !isEqual(newVal, oldValue)) {
formItem == null ? void 0 : formItem.validate("change").catch((err) => debugWarn(err));
}
}
);
return (_ctx, _cache) => {
var _a;
return openBlock(), createBlock(resolveDynamicComponent(_ctx.tag), {
id: unref(groupId),
class: normalizeClass(unref(ns).b("group")),
role: "group",
"aria-label": !unref(isLabeledByFormItem) ? _ctx.ariaLabel || "checkbox-group" : void 0,
"aria-labelledby": unref(isLabeledByFormItem) ? (_a = unref(formItem)) == null ? void 0 : _a.labelId : void 0
}, {
default: withCtx(() => [
renderSlot(_ctx.$slots, "default", {}, () => [
(openBlock(true), createElementBlock(
Fragment,
null,
renderList(_ctx.options, (item, index) => {
return openBlock(), createBlock(
resolveDynamicComponent(optionComponent.value),
mergeProps({ key: index }, { ref_for: true }, getOptionProps(item)),
null,
16
);
}),
128
))
])
]),
_: 3
}, 8, ["id", "class", "aria-label", "aria-labelledby"]);
};
}
});
var CheckboxGroup = /* @__PURE__ */ _export_sfc(_sfc_main, [["__file", "/home/runner/work/element-plus/element-plus/packages/components/checkbox/src/checkbox-group.vue"]]);
export { CheckboxGroup as default };
//# sourceMappingURL=checkbox-group2.mjs.map