-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LWS-260: Codemirror extension for submitting forms on enter key (#1155)
* Add CodeMirror extension for submitting form on enter keypress * Add test * Add optional form attribute * Add hidden textarea inside component for name and value attributes
- Loading branch information
1 parent
4f818e8
commit 1be6379
Showing
7 changed files
with
88 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { test, expect } from '@playwright/test'; | ||
|
||
test.beforeEach(async ({ page }) => { | ||
await page.goto('/'); | ||
}); | ||
|
||
test('submits closest form on enter key press', async ({ page }) => { | ||
await page.locator('[data-test-id="test1"]').getByRole('textbox').locator('div').fill('hello world') | ||
await page.keyboard.press('Enter'); | ||
await expect(page).toHaveURL('/test1?q=hello+world') | ||
}); | ||
|
||
test('submits form identified by form attribute on enter key press', async ({ page }) => { | ||
await page.locator('[data-test-id="test2"]').getByRole('textbox').locator('div').fill('hello world') | ||
await page.keyboard.press('Enter'); | ||
await expect(page).toHaveURL('/test2?q=hello+world') | ||
}); |
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
37 changes: 37 additions & 0 deletions
37
packages/supersearch/src/lib/extensions/submitFormOnEnterKey.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,37 @@ | ||
import { Prec } from '@codemirror/state'; | ||
import { EditorView, keymap } from '@codemirror/view'; | ||
|
||
/** | ||
* CodeMirror extension that submits form elements (either the closest or by specified id using the `form` attribute) on enter keypresses. | ||
* | ||
* @param {string} form Optional id of the `<form>` element with which the form control should be associated with (equivalent with | ||
* the [form attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#form) on HTML Input elements). | ||
*/ | ||
|
||
const submitFormOnEnterKey = (form?: string) => { | ||
const submitForm = (editorView: EditorView) => { | ||
const formElement = form ? document.getElementById(form) : editorView.dom?.closest('form'); | ||
|
||
if (formElement && formElement instanceof HTMLFormElement) { | ||
formElement.requestSubmit(); | ||
return true; // return true to prevent further commands to be tried | ||
} | ||
|
||
return false; | ||
}; | ||
|
||
return Prec.highest( | ||
keymap.of([ | ||
{ | ||
key: 'Enter', | ||
run: submitForm | ||
}, | ||
{ | ||
key: 'Shift-Enter', | ||
run: submitForm | ||
} | ||
]) | ||
); | ||
}; | ||
|
||
export default submitFormOnEnterKey; |
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 |
---|---|---|
@@ -1,9 +1,24 @@ | ||
<script lang="ts"> | ||
import SuperSearch from '$lib/components/SuperSearch.svelte'; | ||
let value = $state(''); | ||
let placeholder = $state('Search'); | ||
</script> | ||
|
||
<SuperSearch {placeholder} /> | ||
<form action="test1"> | ||
<fieldset data-test-id="test1"> | ||
<legend>Supersearch inside <code><form></code> element</legend> | ||
<SuperSearch name="q" bind:value {placeholder} /> | ||
</fieldset> | ||
</form> | ||
|
||
<form> | ||
<fieldset data-test-id="test2"> | ||
<legend>Supersearch using <code>form</code> attribute</legend> | ||
<SuperSearch name="q" bind:value {placeholder} form="form-outside" /> | ||
</fieldset> | ||
</form> | ||
|
||
<form action="test2" id="form-outside"></form> | ||
|
||
<label>Placeholder:<input type="text" bind:value={placeholder} /></label> |
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 @@ | ||
<h1>A test route</h1> |
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 @@ | ||
<h1>Another test route</h1> |