mirror of
https://github.com/transloadit/uppy.git
synced 2026-07-22 01:40:29 +00:00
form: Disable form submit buttons while upload is in progress with triggerUploadOnSubmit
This commit is contained in:
parent
4fb7226ede
commit
59bfbce94a
1 changed files with 20 additions and 1 deletions
|
|
@ -1,5 +1,6 @@
|
|||
const { Plugin } = require('@uppy/core')
|
||||
const findDOMElement = require('@uppy/utils/lib/findDOMElement')
|
||||
const toArray = require('@uppy/utils/lib/toArray')
|
||||
// Rollup uses get-form-data's ES modules build, and rollup-plugin-commonjs automatically resolves `.default`.
|
||||
// So, if we are being built using rollup, this require() won't have a `.default` property.
|
||||
const getFormData = require('get-form-data').default || require('get-form-data')
|
||||
|
|
@ -53,7 +54,25 @@ module.exports = class Form extends Plugin {
|
|||
handleFormSubmit (ev) {
|
||||
if (this.opts.triggerUploadOnSubmit) {
|
||||
ev.preventDefault()
|
||||
this.uppy.upload().catch((err) => {
|
||||
const elements = toArray(ev.target.elements)
|
||||
const disabledByUppy = []
|
||||
elements.forEach((el) => {
|
||||
const isButton = el.tagName === 'BUTTON' || (el.tagName === 'INPUT' && el.type === 'submit')
|
||||
if (isButton && !el.disabled) {
|
||||
el.disabled = true
|
||||
disabledByUppy.push(el)
|
||||
}
|
||||
})
|
||||
this.uppy.upload().then(() => {
|
||||
disabledByUppy.forEach((button) => {
|
||||
button.disabled = false
|
||||
})
|
||||
}, (err) => {
|
||||
disabledByUppy.forEach((button) => {
|
||||
button.disabled = false
|
||||
})
|
||||
return Promise.reject(err)
|
||||
}).catch((err) => {
|
||||
this.uppy.log(err.stack || err.message || err)
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue