setPopulatedOnWebkitAutofill.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import type { FormKitNode } from '@formkit/core'
  3. export const setPopulatedOnWebkitAutofill = (node: FormKitNode) => {
  4. let autofillField = false
  5. const onAnimationstart = (e?: AnimationEvent) => {
  6. if (
  7. !e ||
  8. (e.animationName !== 'onAutoFillStart' &&
  9. e.animationName !== 'onAutoFillEnd') ||
  10. (e.animationName === 'onAutoFillEnd' && !autofillField)
  11. )
  12. return
  13. const inputElement = e.currentTarget as HTMLInputElement
  14. const outerElement = inputElement.closest(
  15. '.formkit-outer',
  16. ) as HTMLDivElement
  17. if (!outerElement) return
  18. if (e.animationName === 'onAutoFillStart') {
  19. autofillField = true
  20. outerElement.dataset.populated = 'true'
  21. return
  22. }
  23. autofillField = false
  24. delete outerElement.dataset.populated
  25. }
  26. node.on('created', () => {
  27. if (!node.context) return
  28. // This is not a typo, all event handlers have just a first letter capitalized!
  29. node.context.attrs.onAnimationstart = onAnimationstart
  30. })
  31. }