You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
1.6 KiB
40 lines
1.6 KiB
|
2 years ago
|
import { ref, getCurrentInstance, inject, computed } from 'vue';
|
||
|
|
import '../../../../utils/index.mjs';
|
||
|
|
import '../../../../constants/index.mjs';
|
||
|
|
import { checkboxGroupContextKey } from '../constants.mjs';
|
||
|
|
import { isUndefined } from '../../../../utils/types.mjs';
|
||
|
|
import { isArray } from '@vue/shared';
|
||
|
|
import { UPDATE_MODEL_EVENT } from '../../../../constants/event.mjs';
|
||
|
|
|
||
|
|
const useCheckboxModel = (props) => {
|
||
|
|
const selfModel = ref(false);
|
||
|
|
const { emit } = getCurrentInstance();
|
||
|
|
const checkboxGroup = inject(checkboxGroupContextKey, void 0);
|
||
|
|
const isGroup = computed(() => isUndefined(checkboxGroup) === false);
|
||
|
|
const isLimitExceeded = ref(false);
|
||
|
|
const model = computed({
|
||
|
|
get() {
|
||
|
|
var _a, _b;
|
||
|
|
return isGroup.value ? (_a = checkboxGroup == null ? void 0 : checkboxGroup.modelValue) == null ? void 0 : _a.value : (_b = props.modelValue) != null ? _b : selfModel.value;
|
||
|
|
},
|
||
|
|
set(val) {
|
||
|
|
var _a, _b;
|
||
|
|
if (isGroup.value && isArray(val)) {
|
||
|
|
isLimitExceeded.value = ((_a = checkboxGroup == null ? void 0 : checkboxGroup.max) == null ? void 0 : _a.value) !== void 0 && val.length > (checkboxGroup == null ? void 0 : checkboxGroup.max.value);
|
||
|
|
isLimitExceeded.value === false && ((_b = checkboxGroup == null ? void 0 : checkboxGroup.changeEvent) == null ? void 0 : _b.call(checkboxGroup, val));
|
||
|
|
} else {
|
||
|
|
emit(UPDATE_MODEL_EVENT, val);
|
||
|
|
selfModel.value = val;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
});
|
||
|
|
return {
|
||
|
|
model,
|
||
|
|
isGroup,
|
||
|
|
isLimitExceeded
|
||
|
|
};
|
||
|
|
};
|
||
|
|
|
||
|
|
export { useCheckboxModel };
|
||
|
|
//# sourceMappingURL=use-checkbox-model.mjs.map
|