Skip to content

Commit 6f53df1

Browse files
Merge pull request #322 from open-source-labs/dev
fix: keydown listener was not returning properly when input elements were focused
2 parents 3be9a3d + d4cbe75 commit 6f53df1

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

src/lib/containers/Graph/Graph.svelte

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -333,17 +333,15 @@
333333
334334
function handleKeyDown(e: KeyboardEvent) {
335335
const { key, code } = e;
336-
336+
const target = e.target as HTMLElement;
337337
// We dont want to prevent users from refreshing the page
338+
// Or interacting with inputs
338339
if (code === 'KeyR' && e.metaKey) return;
339-
340-
const target = e.target as HTMLElement;
340+
if (target.tagName == 'INPUT' || target.tagName == 'TEXTAREA') return;
341341
342342
//Otherwise we prevent default keydown behavior
343-
if (target.tagName !== 'INPUT' && target.tagName !== 'TEXTAREA') {
344-
e.preventDefault();
345-
}
346343
344+
e.preventDefault();
347345
if (code === 'KeyA' && e[`${modifier}Key`]) {
348346
const unlockedNodes = graph.nodes.getAll().filter((node) => !get(node.locked));
349347
$selected = new Set(unlockedNodes);

tests/inputs/test.ts

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const testRoute = '/inputs';
55
test('inputs can be interacted with', async ({ page }) => {
66
await page.goto(testRoute);
77

8+
// Grab the input elements for testing
89
const textField = page.locator('#text-test');
910
const checkBox = page.locator('#checkbox-test');
1011

@@ -20,8 +21,10 @@ test('inputs can be interacted with', async ({ page }) => {
2021
await textField.press('B');
2122
await textField.press('C');
2223

24+
// Chcek that the value has changed
2325
await expect(textField).toHaveValue('TestABC');
2426

27+
// Clear the input
2528
await textField.press('Backspace');
2629
await textField.press('Backspace');
2730
await textField.press('Backspace');
@@ -31,22 +34,47 @@ test('inputs can be interacted with', async ({ page }) => {
3134
await textField.press('Backspace');
3235
await textField.press('Backspace');
3336

37+
// Check that it was cleared
38+
await expect(textField).toHaveValue('');
39+
3440
await textField.press('1');
3541
await textField.press('Space');
3642
await textField.press('2');
3743

3844
await expect(textField).toHaveValue('1 2');
3945

40-
// Select the checkbox element
41-
const checkbox = await page.$('#checkbox-test');
42-
if (!checkbox) throw new Error('Checkbox not found');
46+
if (!checkBox) throw new Error('Checkbox not found');
4347
// Check if the checkbox is checked
44-
const isChecked = await checkbox.isChecked();
48+
const isChecked = await checkBox.isChecked();
4549
await expect(isChecked).toBe(true);
4650
// Click the checkbox
4751
await checkBox.click();
4852

49-
const isNowChecked = await checkbox.isChecked();
53+
const isNowChecked = await checkBox.isChecked();
54+
5055
// Check the current value of the chebox
5156
await expect(isNowChecked).toBe(false);
57+
58+
// Grab the graph element
59+
const graphWrapper = page.locator('.svelvet-graph-wrapper');
60+
61+
// Check the transforms
62+
await expect(graphWrapper).toHaveAttribute('style', 'transform: translate(0px, 0px) scale(1);');
63+
64+
// These commands should not affect the graph
65+
await textField.press('=');
66+
await textField.press('=');
67+
await textField.press('=');
68+
69+
await expect(graphWrapper).toHaveAttribute('style', 'transform: translate(0px, 0px) scale(1);');
70+
71+
await textField.press('-');
72+
await textField.press('-');
73+
74+
await expect(graphWrapper).toHaveAttribute('style', 'transform: translate(0px, 0px) scale(1);');
75+
76+
await textField.press('0');
77+
await textField.press('0');
78+
79+
await expect(graphWrapper).toHaveAttribute('style', 'transform: translate(0px, 0px) scale(1);');
5280
});

0 commit comments

Comments
 (0)