Skip to content

Commit

Permalink
Form submission redirect is optional
Browse files Browse the repository at this point in the history
Co-Authored-By: Sam Stephenson <[email protected]>
  • Loading branch information
javan and sstephenson committed Aug 7, 2019
1 parent c23f392 commit 7a75cb5
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/form_submission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ export class FormSubmission {
readonly formElement: HTMLFormElement
readonly formData: FormData
readonly fetchRequest: FetchRequest
readonly mustRedirect: boolean
state = FormSubmissionState.initialized

constructor(delegate: FormSubmissionDelegate, formElement: HTMLFormElement) {
constructor(delegate: FormSubmissionDelegate, formElement: HTMLFormElement, mustRedirect = false) {
this.delegate = delegate
this.formElement = formElement
this.formData = new FormData(formElement)
this.fetchRequest = new FetchRequest(this, this.method, this.location, this.body)
this.mustRedirect = mustRedirect
}

get method(): FetchMethod {
Expand Down Expand Up @@ -91,7 +93,7 @@ export class FormSubmission {
}

requestSucceededWithResponse(request: FetchRequest, response: FetchResponse) {
if (response.redirected) {
if (response.redirected || !this.mustRedirect) {
this.state = FormSubmissionState.receiving
this.delegate.formSubmissionSucceededWithResponse(this, response)
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/navigator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class Navigator {
}

submit(form: HTMLFormElement, options: Partial<NavigationOptions> = {}) {
const formSubmission = new FormSubmission(this, form)
const formSubmission = new FormSubmission(this, form, true)
const { target } = { ...navigationOptionsForForm(form), ...options }

if (target != NavigationTarget.background) {
Expand Down

0 comments on commit 7a75cb5

Please sign in to comment.