useFormBlock.ts 835 B

12345678910111213141516171819202122232425262728
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import type { FormFieldContext } from '#shared/components/Form/types/field.ts'
  3. import type { Ref } from 'vue'
  4. import { onUnmounted } 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. }