|
|
@@ -2,6 +2,7 @@ import classNames from 'classnames'
|
|
|
import { FetchError, postJSON } from '../../infrastructure/fetch-json'
|
|
|
import { validateCaptchaV2 } from './captcha'
|
|
|
import inputValidator from './input-validator'
|
|
|
+import { disableElement, enableElement } from '../utils/disableElement'
|
|
|
|
|
|
// Form helper(s) to handle:
|
|
|
// - Attaching to the relevant form elements
|
|
|
@@ -122,16 +123,12 @@ export function inflightHelper(el) {
|
|
|
const showWhenInflight = el.querySelectorAll('[data-ol-inflight="pending"]')
|
|
|
|
|
|
el.addEventListener('pending', () => {
|
|
|
- disabledInflight.forEach(el => {
|
|
|
- el.disabled = true
|
|
|
- })
|
|
|
+ disabledInflight.forEach(disableElement)
|
|
|
toggleDisplay(showWhenNotInflight, showWhenInflight)
|
|
|
})
|
|
|
|
|
|
el.addEventListener('idle', () => {
|
|
|
- disabledInflight.forEach(el => {
|
|
|
- el.disabled = false
|
|
|
- })
|
|
|
+ disabledInflight.forEach(enableElement)
|
|
|
toggleDisplay(showWhenInflight, showWhenNotInflight)
|
|
|
})
|
|
|
}
|
|
|
@@ -146,6 +143,17 @@ function formSentHelper(el) {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
+function formValidationHelper(el) {
|
|
|
+ el.querySelectorAll('input').forEach(inputEl => {
|
|
|
+ if (
|
|
|
+ inputEl.willValidate &&
|
|
|
+ !inputEl.hasAttribute('data-ol-no-custom-form-validation-messages')
|
|
|
+ ) {
|
|
|
+ inputValidator(inputEl)
|
|
|
+ }
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
export function toggleDisplay(hide, show) {
|
|
|
hide.forEach(el => {
|
|
|
el.hidden = true
|
|
|
@@ -155,21 +163,22 @@ export function toggleDisplay(hide, show) {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
-export function hydrateForm(el) {
|
|
|
+function hydrateAsyncForm(el) {
|
|
|
formSubmitHelper(el)
|
|
|
inflightHelper(el)
|
|
|
formSentHelper(el)
|
|
|
+ formValidationHelper(el)
|
|
|
+}
|
|
|
|
|
|
- el.querySelectorAll('input').forEach(inputEl => {
|
|
|
- if (
|
|
|
- inputEl.willValidate &&
|
|
|
- !inputEl.hasAttribute('data-ol-no-custom-form-validation-messages')
|
|
|
- ) {
|
|
|
- inputValidator(inputEl)
|
|
|
- }
|
|
|
+function hydrateRegularForm(el) {
|
|
|
+ inflightHelper(el)
|
|
|
+ formValidationHelper(el)
|
|
|
+
|
|
|
+ el.addEventListener('submit', () => {
|
|
|
+ el.dispatchEvent(new Event('pending'))
|
|
|
})
|
|
|
}
|
|
|
|
|
|
-document
|
|
|
- .querySelectorAll(`[data-ol-async-form]`)
|
|
|
- .forEach(form => hydrateForm(form))
|
|
|
+document.querySelectorAll(`[data-ol-async-form]`).forEach(hydrateAsyncForm)
|
|
|
+
|
|
|
+document.querySelectorAll(`[data-ol-regular-form]`).forEach(hydrateRegularForm)
|