Skip to content

Commit 126dff5

Browse files
authored
feat: allow form submission when pressing Enter (#82)
* feat: allow form submission when pressing `Enter` * test: pressing enter submits the form
1 parent 18c0b85 commit 126dff5

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

package-lock.json

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lib/CurrencyInput.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
const isModifier = event.metaKey || event.altKey || event.ctrlKey;
7676
const isArrowKey = event.key === 'ArrowLeft' || event.key === 'ArrowRight';
7777
const isTab = event.key === 'Tab';
78+
const isEnter = event.key === 'Enter';
7879
// Keys that are not a digit, comma, period or minus sign
7980
const isInvalidCharacter = !/^\d|,|\.|-$/g.test(event.key);
8081
@@ -88,7 +89,7 @@
8889
8990
if (
9091
isPunctuationDuplicated() ||
91-
(!isDeletion && !isModifier && !isArrowKey && isInvalidCharacter && !isTab)
92+
(!isDeletion && !isModifier && !isArrowKey && isInvalidCharacter && !isTab && !isEnter)
9293
)
9394
event.preventDefault();
9495
}

tests/svelte-currency-input.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,4 +534,18 @@ test.describe('CurrencyInput', () => {
534534
await expect(fourTwentySixNineInput).toBeVisible();
535535
await expect(fourTwentySixNineInput).toHaveValue('-$42,069.69');
536536
});
537+
538+
test('pressing enter submits the form', async ({ page }) => {
539+
const preTag = page.locator('.demoForm__pre');
540+
await expect(preTag).toContainText('Submit form to see a JSON output of the values');
541+
await expect(preTag).not.toContainText('bitcoin');
542+
543+
const allInputs = page.locator('.currencyInput__formatted');
544+
await allInputs.first().focus();
545+
await expect(allInputs.first()).toHaveValue('-$42,069.69');
546+
547+
await page.keyboard.press('Enter');
548+
await expect(preTag).not.toContainText('Submit form to see a JSON output of the values');
549+
await expect(preTag).toContainText('bitcoin');
550+
});
537551
});

0 commit comments

Comments
 (0)