Skip to content

Commit

Permalink
@uppy/form: do not emit 'submit' event more than once (#5299)
Browse files Browse the repository at this point in the history
Instead of using `requestSubmit` from the `'submit'` event handler (which was causing a loop), we can call `submit()` after checking `reportValidity` to get the same result without re-emitting the event.
  • Loading branch information
Murderlon committed Jul 2, 2024
1 parent 7c174e8 commit 03a4cd8
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions packages/@uppy/form/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,6 @@ export default class Form<M extends Meta, B extends Body> extends BasePlugin<

form: HTMLFormElement // TODO: make this private (or at least, mark it as readonly)

/**
* Unfortunately Uppy isn't a state machine in which we can guarantee it's
* currently in one state and one state only so we use this completed property which is set on `upload-success'.
*/
#completed = false

constructor(uppy: Uppy<M, B>, opts?: FormOptions) {
super(uppy, { ...defaultOptions, ...opts })
this.type = 'acquirer'
Expand All @@ -71,25 +65,28 @@ export default class Form<M extends Meta, B extends Body> extends BasePlugin<
}

handleUploadStart(): void {
this.#completed = false
if (this.opts.getMetaFromForm) {
this.getMetaFromForm()
}
}

handleSuccess(result: Result<M, B>): void {
this.#completed = true
if (this.opts.addResultToForm) {
this.addResultToForm(result)
}

if (this.opts.submitOnSuccess) {
this.form.requestSubmit()
// Returns true if the element's child controls satisfy their validation constraints.
// When false is returned, cancelable invalid events are fired for each invalid child
// and validation problems are reported to the user.
if (this.form.reportValidity()) {
this.form.submit()
}
}
}

handleFormSubmit(ev: Event): void {
if (this.opts.triggerUploadOnSubmit && !this.#completed) {
if (this.opts.triggerUploadOnSubmit) {
ev.preventDefault()
const elements = toArray((ev.target as HTMLFormElement).elements)
const disabledByUppy: HTMLButtonElement[] = []
Expand Down

0 comments on commit 03a4cd8

Please sign in to comment.