Skip to content

Commit

Permalink
feat: allow form submission when pressing Enter (#82)
Browse files Browse the repository at this point in the history
* feat: allow form submission when pressing `Enter`

* test: pressing enter submits the form
  • Loading branch information
fmaclen authored Nov 1, 2024
1 parent 18c0b85 commit 126dff5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/lib/CurrencyInput.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
const isModifier = event.metaKey || event.altKey || event.ctrlKey;
const isArrowKey = event.key === 'ArrowLeft' || event.key === 'ArrowRight';
const isTab = event.key === 'Tab';
const isEnter = event.key === 'Enter';
// Keys that are not a digit, comma, period or minus sign
const isInvalidCharacter = !/^\d|,|\.|-$/g.test(event.key);
Expand All @@ -88,7 +89,7 @@
if (
isPunctuationDuplicated() ||
(!isDeletion && !isModifier && !isArrowKey && isInvalidCharacter && !isTab)
(!isDeletion && !isModifier && !isArrowKey && isInvalidCharacter && !isTab && !isEnter)
)
event.preventDefault();
}
Expand Down
14 changes: 14 additions & 0 deletions tests/svelte-currency-input.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,4 +534,18 @@ test.describe('CurrencyInput', () => {
await expect(fourTwentySixNineInput).toBeVisible();
await expect(fourTwentySixNineInput).toHaveValue('-$42,069.69');
});

test('pressing enter submits the form', async ({ page }) => {
const preTag = page.locator('.demoForm__pre');
await expect(preTag).toContainText('Submit form to see a JSON output of the values');
await expect(preTag).not.toContainText('bitcoin');

const allInputs = page.locator('.currencyInput__formatted');
await allInputs.first().focus();
await expect(allInputs.first()).toHaveValue('-$42,069.69');

await page.keyboard.press('Enter');
await expect(preTag).not.toContainText('Submit form to see a JSON output of the values');
await expect(preTag).toContainText('bitcoin');
});
});

0 comments on commit 126dff5

Please sign in to comment.