hongluan-ui
Version:
Hongluan Component Library for Vue 3
76 lines (73 loc) • 2.82 kB
JavaScript
import { inject, ref, computed, toRaw } from 'vue';
import '../../../../utils/index.mjs';
import '../../../../tokens/index.mjs';
import { isEqual } from 'lodash-unified';
import '../../../../hooks/index.mjs';
import { checkboxGroupContextKey } from '../../../../tokens/checkbox.mjs';
import { isPropAbsent, isUndefined, isBoolean } from '../../../../utils/types.mjs';
import { isArray, isObject } from '@vue/shared';
import { useConsistentProp } from '../../../../hooks/use-consistent-prop/index.mjs';
const useCheckboxStatus = (props, slots, { model }) => {
const checkboxGroup = inject(checkboxGroupContextKey, void 0);
const isFocused = ref(false);
const actualValue = computed(() => {
if (!isPropAbsent(props.value)) {
return props.value;
}
return props.label;
});
const isGroup = computed(() => isUndefined(checkboxGroup) === false);
const isChecked = computed(() => {
const value = model.value;
if (isBoolean(value)) {
return value;
} else if (isArray(value)) {
if (isObject(actualValue.value)) {
return value.map(toRaw).some((o) => isEqual(o, actualValue.value));
} else {
return value.map(toRaw).includes(actualValue.value);
}
} else if (value !== null && value !== void 0) {
return value === props.trueValue || value === props.trueLabel;
} else {
return !!value;
}
});
const isCustom = computed(() => {
var _a;
return isGroup.value ? ((_a = checkboxGroup == null ? void 0 : checkboxGroup.custom) == null ? void 0 : _a.value) || props.custom : props.custom;
});
const isRound = computed(() => {
var _a;
return isGroup.value ? ((_a = checkboxGroup == null ? void 0 : checkboxGroup.round) == null ? void 0 : _a.value) || props.round : props.round;
});
const isFill = computed(() => {
var _a;
return isGroup.value ? ((_a = checkboxGroup == null ? void 0 : checkboxGroup.fill) == null ? void 0 : _a.value) || props.fill : props.fill;
});
const checkboxType = computed(() => {
var _a;
return isGroup.value ? ((_a = checkboxGroup == null ? void 0 : checkboxGroup.type) == null ? void 0 : _a.value) || props.type : props.type;
});
const checkboxSize = computed(() => {
var _a;
const { size: temCheckboxSize } = useConsistentProp();
return isGroup.value ? ((_a = checkboxGroup == null ? void 0 : checkboxGroup.size) == null ? void 0 : _a.value) || temCheckboxSize.value : temCheckboxSize.value;
});
const hasOwnLabel = computed(() => {
return !!slots.default || !isPropAbsent(actualValue.value);
});
return {
isChecked,
isFocused,
isCustom,
isRound,
isFill,
checkboxType,
checkboxSize,
hasOwnLabel,
actualValue
};
};
export { useCheckboxStatus };
//# sourceMappingURL=use-checkbox-status.mjs.map