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

Make data-turbo-confirm work with multiple submitters inside a form #564

Merged
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/core/drive/form_submission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export class FormSubmission {
}

get confirmationMessage() {
return this.formElement.getAttribute("data-turbo-confirm")
return this.submitter?.getAttribute("data-turbo-confirm") || this.formElement.getAttribute("data-turbo-confirm")
}

get needsConfirmation() {
Expand Down
1 change: 1 addition & 0 deletions src/tests/fixtures/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ <h1>Form</h1>
</form>
<form action="/__turbo/redirect" method="post" class="confirm" data-turbo-confirm="Are you sure?">
<input type="submit">
<button type="submit" name="greeting" id="secondary_submitter" data-turbo-confirm="Are you really sure?" formaction="/__turbo/redirect?path=/src/tests/fixtures/one.html" formmethod="post" value="secondary_submitter">Secondary action</button>
</form>
</div>
<hr>
Expand Down
20 changes: 20 additions & 0 deletions src/tests/functional/form_submission_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export class FormSubmissionTests extends TurboDriveTestCase {
this.assert.equal(await this.getAlertText(), "Are you sure?")
await this.acceptAlert()
this.assert.ok(await this.formSubmitStarted)
this.assert.equal(await this.pathname, "/src/tests/fixtures/one.html")
}

async "test form submission with confirmation cancelled"() {
Expand All @@ -36,6 +37,25 @@ export class FormSubmissionTests extends TurboDriveTestCase {
this.assert.notOk(await this.formSubmitStarted)
}

async "test form submission with secondary submitter click - confirmation confirmed"() {
await this.clickSelector("#standard form.confirm #secondary_submitter")

this.assert.equal(await this.getAlertText(), "Are you really sure?")
await this.acceptAlert()
this.assert.ok(await this.formSubmitStarted)
this.assert.equal(await this.pathname, "/src/tests/fixtures/one.html")
this.assert.equal(await this.visitAction, "advance")
this.assert.equal(await this.getSearchParam("greeting"), "secondary_submitter")
}

async "test form submission with secondary submitter click - confirmation cancelled"() {
await this.clickSelector("#standard form.confirm #secondary_submitter")

this.assert.equal(await this.getAlertText(), "Are you really sure?")
await this.dismissAlert()
this.assert.notOk(await this.formSubmitStarted)
}

async "test from submission with confirmation overriden"() {
await this.remote.execute(() => window.Turbo.setConfirmMethod((message, element) => confirm("Overriden message")))

Expand Down