Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@uppy/form: fix submitOnSuccess and triggerUploadOnSubmit combination #5058

Merged
merged 3 commits into from
Apr 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/@uppy/form/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ 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 @@ -65,12 +71,14 @@ 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)
}
Expand All @@ -81,7 +89,7 @@ export default class Form<M extends Meta, B extends Body> extends BasePlugin<
}

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