use-checkbox-model.mjs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import { ref, getCurrentInstance, inject, computed } from 'vue';
  2. import { checkboxGroupContextKey } from '../constants.mjs';
  3. import { isUndefined } from '../../../../utils/types.mjs';
  4. import { isArray } from '@vue/shared';
  5. import { UPDATE_MODEL_EVENT } from '../../../../constants/event.mjs';
  6. const useCheckboxModel = (props) => {
  7. const selfModel = ref(false);
  8. const { emit } = getCurrentInstance();
  9. const checkboxGroup = inject(checkboxGroupContextKey, void 0);
  10. const isGroup = computed(() => isUndefined(checkboxGroup) === false);
  11. const isLimitExceeded = ref(false);
  12. const model = computed({
  13. get() {
  14. var _a, _b;
  15. return isGroup.value ? (_a = checkboxGroup == null ? void 0 : checkboxGroup.modelValue) == null ? void 0 : _a.value : (_b = props.modelValue) != null ? _b : selfModel.value;
  16. },
  17. set(val) {
  18. var _a, _b;
  19. if (isGroup.value && isArray(val)) {
  20. 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) && val.length > model.value.length;
  21. isLimitExceeded.value === false && ((_b = checkboxGroup == null ? void 0 : checkboxGroup.changeEvent) == null ? void 0 : _b.call(checkboxGroup, val));
  22. } else {
  23. emit(UPDATE_MODEL_EVENT, val);
  24. selfModel.value = val;
  25. }
  26. }
  27. });
  28. return {
  29. model,
  30. isGroup,
  31. isLimitExceeded
  32. };
  33. };
  34. export { useCheckboxModel };
  35. //# sourceMappingURL=use-checkbox-model.mjs.map