cc8cd59b2deb7868ab4c493c5abbdf6cca6f0b4f496fc591345a5213208ea283c8bf6c1db9abd8acf730072a6122fb110263f9cf8f19702b105da374d10df3 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { inject, getCurrentInstance, computed, watch, nextTick } from 'vue';
  2. import { checkboxGroupContextKey } from '../constants.mjs';
  3. import { useFormItem } from '../../../form/src/hooks/use-form-item.mjs';
  4. import { debugWarn } from '../../../../utils/error.mjs';
  5. import { CHANGE_EVENT } from '../../../../constants/event.mjs';
  6. const useCheckboxEvent = (props, {
  7. model,
  8. isLimitExceeded,
  9. hasOwnLabel,
  10. isDisabled,
  11. isLabeledByFormItem
  12. }) => {
  13. const checkboxGroup = inject(checkboxGroupContextKey, void 0);
  14. const { formItem } = useFormItem();
  15. const { emit } = getCurrentInstance();
  16. function getLabeledValue(value) {
  17. var _a, _b, _c, _d;
  18. return [true, props.trueValue, props.trueLabel].includes(value) ? (_b = (_a = props.trueValue) != null ? _a : props.trueLabel) != null ? _b : true : (_d = (_c = props.falseValue) != null ? _c : props.falseLabel) != null ? _d : false;
  19. }
  20. function emitChangeEvent(checked, e) {
  21. emit(CHANGE_EVENT, getLabeledValue(checked), e);
  22. }
  23. function handleChange(e) {
  24. if (isLimitExceeded.value)
  25. return;
  26. const target = e.target;
  27. emit(CHANGE_EVENT, getLabeledValue(target.checked), e);
  28. }
  29. async function onClickRoot(e) {
  30. if (isLimitExceeded.value)
  31. return;
  32. if (!hasOwnLabel.value && !isDisabled.value && isLabeledByFormItem.value) {
  33. const eventTargets = e.composedPath();
  34. const hasLabel = eventTargets.some((item) => item.tagName === "LABEL");
  35. if (!hasLabel) {
  36. model.value = getLabeledValue([false, props.falseValue, props.falseLabel].includes(model.value));
  37. await nextTick();
  38. emitChangeEvent(model.value, e);
  39. }
  40. }
  41. }
  42. const validateEvent = computed(() => (checkboxGroup == null ? void 0 : checkboxGroup.validateEvent) || props.validateEvent);
  43. watch(() => props.modelValue, () => {
  44. if (validateEvent.value) {
  45. formItem == null ? void 0 : formItem.validate("change").catch((err) => debugWarn());
  46. }
  47. });
  48. return {
  49. handleChange,
  50. onClickRoot
  51. };
  52. };
  53. export { useCheckboxEvent };
  54. //# sourceMappingURL=use-checkbox-event.mjs.map