index.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { password as passwordDefinition } from '@formkit/inputs'
  3. import { useEventListener } from '@vueuse/core'
  4. import { cloneDeep } from 'lodash-es'
  5. import initializeFieldDefinition from '#shared/form/core/initializeFieldDefinition.ts'
  6. import formUpdaterTrigger from '#shared/form/features/formUpdaterTrigger.ts'
  7. import type { FormKitNode } from '@formkit/core'
  8. const localPasswordDefinition = cloneDeep(passwordDefinition)
  9. const switchPasswordVisibility = (node: FormKitNode) => {
  10. const { props } = node
  11. props.suffixIconClass = 'select-none cursor-pointer'
  12. props.suffixIcon = 'show'
  13. props.onSuffixIconClick = () => {
  14. props.type = props.type === 'password' ? 'text' : 'password'
  15. }
  16. node.on('mounted', ({ origin }) => {
  17. if (origin.name !== 'password' && !props.id) return
  18. const suffixIcon = document.getElementById(
  19. props.id as string,
  20. )?.nextElementSibling
  21. if (suffixIcon)
  22. useEventListener(suffixIcon, 'keydown', (event: KeyboardEvent) => {
  23. if (event.code === 'Space')
  24. props.type = props.type === 'password' ? 'text' : 'password'
  25. })
  26. })
  27. node.on('prop:type', ({ payload, origin }) => {
  28. const { props } = origin
  29. props.suffixIcon = payload === 'password' ? 'show' : 'hide'
  30. })
  31. }
  32. initializeFieldDefinition(localPasswordDefinition, {
  33. features: [switchPasswordVisibility, formUpdaterTrigger('delayed')],
  34. })
  35. export default {
  36. fieldType: 'password',
  37. definition: localPasswordDefinition,
  38. }