import { computed, defineComponent, onBeforeUpdate, type PropType, ref, type VNode, } from 'vue'; import { useTranslation } from '../../translations'; import BCMSButton from '../button'; import { BCMSCheckboxInput } from '.'; import { DefaultComponentProps } from '../_default'; interface Data { label?: string; desc: string; selected: boolean; helperText?: string; invalidText?: string; } const component = defineComponent({ props: { ...DefaultComponentProps, title: { type: Object as PropType, default: <>, }, initialValue: { type: Array as PropType, required: true, }, }, emits: { change: (_data: Data[]) => { return true; }, }, setup(props, { emit }) { const translations = computed(() => { return useTranslation(); }); const data = ref(getData()); const allChecked = computed(() => { return !data.value.find((e) => !e.selected); }); function getData(): Data[] { if (props.initialValue) { return props.initialValue; } return []; } function checkAll() { if (allChecked.value) { for (let i = 0; i < data.value.length; i++) { data.value[i].selected = false; } } else { for (let i = 0; i < data.value.length; i++) { data.value[i].selected = true; } } emit('change', data.value); } function change(index: number, selected: boolean) { data.value[index].selected = selected; emit('change', data.value); } onBeforeUpdate(() => { if (JSON.stringify(data.value) !== JSON.stringify(props.initialValue)) { data.value = getData(); } }); return () => (

{props.title}

{allChecked.value ? translations.value.input.checkboxArray.uncheck : translations.value.input.checkboxArray.check} {data.value.map((item, itemIndex) => ( { change(itemIndex, event); }} /> ))}
); }, }); export default component;