12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
- import type { FormKitNode } from '@formkit/core'
- import type { FormUpdaterTrigger } from '../../types/form.ts'
- const formUpdaterTrigger = (
- defaultTrigger: FormUpdaterTrigger = 'direct',
- defaultTriggerDelay = 300,
- ) => {
- return (node: FormKitNode) => {
- const { props } = node
- node.addProps([
- 'triggerFormUpdater',
- 'formUpdaterTrigger',
- 'pendingValueUpdate',
- ])
- node.on('created', () => {
- if (!props.formUpdaterTrigger) {
- props.formUpdaterTrigger = defaultTrigger
- }
- if (
- props.triggerFormUpdater &&
- props.formUpdaterTrigger === 'delayed' &&
- (!props.delay || props.delay < defaultTriggerDelay)
- ) {
- props.delay = defaultTriggerDelay
- }
- const { context } = node
- if (!context) return
- // Reset pending value update prop if needed.
- node.hook.input((payload, next) => {
- if (context.pendingValueUpdate) context.pendingValueUpdate = false
- return next(payload)
- })
- })
- }
- }
- export default formUpdaterTrigger
|