addSubmitEvent.ts 872 B

12345678910111213141516171819202122232425262728
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import type { FormKitNode } from '@formkit/core'
  3. const addSubmitEvent = (node: FormKitNode) => {
  4. // TODO: think about adding a `submit` boolean prop as well, and submitting the whole form implicitly if set to true.
  5. node.addProps(['onSubmit'])
  6. node.on('created', () => {
  7. if (!node.context || typeof node.props.onSubmit !== 'function') return
  8. node.context.attrs.onKeypress = (event: KeyboardEvent) => {
  9. if (event.key !== 'Enter') return
  10. // Prevent the form from being submitted, this should now be the responsibility of the custom handler.
  11. event.preventDefault()
  12. node.props.onSubmit.call(
  13. null,
  14. new SubmitEvent('submit', {
  15. submitter: event.target as HTMLElement,
  16. }),
  17. )
  18. }
  19. })
  20. }
  21. export default addSubmitEvent