364db534c3dec0a6c24b01fd31acf3edca8bfe9f09f7a877c34cd3a59697492c613d2bbec13dc96fe1cd69c3d12039a0ea46c3d780e1dbc68cf07021ffb144 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { inject, ref, computed, toRaw } from 'vue';
  2. import { isEqual } from 'lodash-unified';
  3. import { checkboxGroupContextKey } from '../constants.mjs';
  4. import { isPropAbsent, isBoolean } from '../../../../utils/types.mjs';
  5. import { isArray, isObject } from '@vue/shared';
  6. import { useFormSize } from '../../../form/src/hooks/use-form-common-props.mjs';
  7. const useCheckboxStatus = (props, slots, { model }) => {
  8. const checkboxGroup = inject(checkboxGroupContextKey, void 0);
  9. const isFocused = ref(false);
  10. const actualValue = computed(() => {
  11. if (!isPropAbsent(props.value)) {
  12. return props.value;
  13. }
  14. return props.label;
  15. });
  16. const isChecked = computed(() => {
  17. const value = model.value;
  18. if (isBoolean(value)) {
  19. return value;
  20. } else if (isArray(value)) {
  21. if (isObject(actualValue.value)) {
  22. return value.map(toRaw).some((o) => isEqual(o, actualValue.value));
  23. } else {
  24. return value.map(toRaw).includes(actualValue.value);
  25. }
  26. } else if (value !== null && value !== void 0) {
  27. return value === props.trueValue || value === props.trueLabel;
  28. } else {
  29. return !!value;
  30. }
  31. });
  32. const checkboxButtonSize = useFormSize(computed(() => {
  33. var _a;
  34. return (_a = checkboxGroup == null ? void 0 : checkboxGroup.size) == null ? void 0 : _a.value;
  35. }), {
  36. prop: true
  37. });
  38. const checkboxSize = useFormSize(computed(() => {
  39. var _a;
  40. return (_a = checkboxGroup == null ? void 0 : checkboxGroup.size) == null ? void 0 : _a.value;
  41. }));
  42. const hasOwnLabel = computed(() => {
  43. return !!slots.default || !isPropAbsent(actualValue.value);
  44. });
  45. return {
  46. checkboxButtonSize,
  47. isChecked,
  48. isFocused,
  49. checkboxSize,
  50. hasOwnLabel,
  51. actualValue
  52. };
  53. };
  54. export { useCheckboxStatus };
  55. //# sourceMappingURL=use-checkbox-status.mjs.map