Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(button): submit from outside of form (#25913)
Resolves #21194 Co-authored-by: Sean Perkins <sean@ionic.io>
- Loading branch information
1 parent
9278db2
commit b139838
Showing
7 changed files
with
214 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
core/src/components/button/test/form-reference/button.e2e.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { expect } from '@playwright/test'; | ||
import { test } from '@utils/test/playwright'; | ||
|
||
test.describe('button: form', () => { | ||
test('should submit the form by id', async ({ page }) => { | ||
await page.setContent(` | ||
<form id="myForm"></form> | ||
<ion-button form="myForm" type="submit">Submit</ion-button> | ||
`); | ||
|
||
const submitEvent = await page.spyOnEvent('submit'); | ||
|
||
await page.click('ion-button'); | ||
|
||
expect(submitEvent).toHaveReceivedEvent(); | ||
}); | ||
|
||
test('should submit the form by reference', async ({ page }) => { | ||
await page.setContent(` | ||
<form></form> | ||
<ion-button type="submit">Submit</ion-button> | ||
<script> | ||
const form = document.querySelector('form'); | ||
const button = document.querySelector('ion-button'); | ||
button.form = form; | ||
</script> | ||
`); | ||
|
||
const submitEvent = await page.spyOnEvent('submit'); | ||
|
||
await page.click('ion-button'); | ||
|
||
expect(submitEvent).toHaveReceivedEvent(); | ||
}); | ||
|
||
test('should submit the closest form', async ({ page }) => { | ||
await page.setContent(` | ||
<form> | ||
<ion-button type="submit">Submit</ion-button> | ||
</form> | ||
`); | ||
|
||
const submitEvent = await page.spyOnEvent('submit'); | ||
|
||
await page.click('ion-button'); | ||
|
||
expect(submitEvent).toHaveReceivedEvent(); | ||
}); | ||
|
||
test.describe('should throw a warning if the form cannot be found', () => { | ||
test('form is a string selector', async ({ page }) => { | ||
await page.setContent(`<ion-button type="submit" form="missingForm">Submit</ion-button>`); | ||
|
||
const logs: string[] = []; | ||
|
||
page.on('console', (msg) => { | ||
logs.push(msg.text()); | ||
}); | ||
|
||
await page.click('ion-button'); | ||
|
||
expect(logs.length).toBe(1); | ||
expect(logs[0]).toContain( | ||
'[Ionic Warning]: Form with selector: "#missingForm" could not be found. Verify that the id is correct and the form is rendered in the DOM.' | ||
); | ||
}); | ||
|
||
test('form is an element reference', async ({ page }) => { | ||
await page.setContent(` | ||
<ion-button type="submit">Submit</ion-button> | ||
<script> | ||
const form = document.querySelector('form'); | ||
const button = document.querySelector('ion-button'); | ||
button.form = form; | ||
</script> | ||
`); | ||
|
||
const logs: string[] = []; | ||
|
||
page.on('console', (msg) => { | ||
logs.push(msg.text()); | ||
}); | ||
|
||
await page.click('ion-button'); | ||
|
||
expect(logs.length).toBe(1); | ||
expect(logs[0]).toContain( | ||
'[Ionic Warning]: The provided "form" element is invalid. Verify that the form is a HTMLFormElement and rendered in the DOM.' | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<!DOCTYPE html> | ||
<html lang="en" dir="ltr"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<title>Button - Form</title> | ||
<meta | ||
name="viewport" | ||
content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" | ||
/> | ||
<link href="../../../../../css/ionic.bundle.css" rel="stylesheet" /> | ||
<link href="../../../../../scripts/testing/styles.css" rel="stylesheet" /> | ||
<script src="../../../../../scripts/testing/scripts.js"></script> | ||
<script nomodule src="../../../../../dist/ionic/ionic.js"></script> | ||
<script type="module" src="../../../../../dist/ionic/ionic.esm.js"></script> | ||
</head> | ||
|
||
<body> | ||
<ion-app> | ||
<ion-header> | ||
<ion-toolbar> | ||
<ion-title>Outside button - Form Submit</ion-title> | ||
</ion-toolbar> | ||
</ion-header> | ||
|
||
<ion-content class="ion-padding ion-text-center" id="content"> | ||
<form id="outside-submit" onsubmit="return validate(event)" action="http://httpbin.org/get" method="GET"> | ||
<div> | ||
<input name="name" required /> | ||
</div> | ||
</form> | ||
|
||
<ion-button type="submit" form="outside-submit"> Submit Form </ion-button> | ||
</ion-content> | ||
</ion-app> | ||
|
||
<script> | ||
document.querySelector('form').addEventListener('submit', (event) => { | ||
console.log('SUBMIT from event', event); | ||
}); | ||
function validate(event) { | ||
console.log('SUBMIT from attribute', event); | ||
if (event.target.elements[0].value === 'admin') { | ||
return true; | ||
} else { | ||
console.log('INCORRECT USER, use "admin"'); | ||
return false; | ||
} | ||
} | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters