53 lines
1.3 KiB
JavaScript

import { watch } from 'vue';
import { createNamespace } from '../utils';
import { CHECKBOX_KEY } from '../checkbox';
import { useExpose } from '../composition/use-expose';
import { useChildren } from '../composition/use-relation';
import { useParentField } from '../composition/use-parent-field';
const [createComponent, bem] = createNamespace('checkbox-group');
export default createComponent({
props: {
max: [Number, String],
disabled: Boolean,
direction: String,
iconSize: [Number, String],
checkedColor: String,
modelValue: {
type: Array,
default: () => [],
},
},
emits: ['change', 'update:modelValue'],
setup(props, { emit, slots }) {
const { children, linkChildren } = useChildren(CHECKBOX_KEY);
const toggleAll = (checked) => {
if (checked === false) {
emit('update:modelValue', []);
} else {
const names = children
.filter((item) => checked || !item.checked.value)
.map((item) => item.name);
emit('update:modelValue', names);
}
};
watch(
() => props.modelValue,
(value) => {
emit('change', value);
}
);
useExpose({ toggleAll });
useParentField(() => props.modelValue);
linkChildren({ emit, props });
return () => <div class={bem([props.direction])}>{slots.default?.()}</div>;
},
});