useFormBlock.ts 837 B

123456789101112131415161718192021222324252627282930
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { onUnmounted } from 'vue'
  3. import type { FormFieldContext } from '#shared/components/Form/types/field.ts'
  4. import type { Ref } from 'vue'
  5. // TODO maybe there is a better way to do this with FormKit?
  6. export const useFormBlock = (
  7. context: Ref<FormFieldContext>,
  8. cb: (e: MouseEvent) => void,
  9. ) => {
  10. const receipt = context.value.node.on('block-click', ({ payload }) => {
  11. if (context.value.disabled) return
  12. const target = payload.target as HTMLElement | null
  13. // ignore link
  14. if (!target || target.classList.contains('formkit-link')) return
  15. if (target.querySelector('.formkit-link')) return
  16. if (target.closest('.formkit-link')) return
  17. cb(payload)
  18. })
  19. onUnmounted(() => {
  20. context.value.node.off(receipt)
  21. })
  22. }