Skip to content

Use fail fast for keydown handler instead of having an additional level #443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
<input
placeholder="what needs to be done?"
on:keydown={(e) => {
if (e.key === 'Enter') {
todos.add(e.currentTarget.value);
e.currentTarget.value = '';
}
if (e.key !== 'Enter') return;

todos.add(e.currentTarget.value);
e.currentTarget.value = '';
}}
/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ Inside the `keydown` event handler of the 'add a todo' `<input>`, let's post som
type="text"
autocomplete="off"
on:keydown={async (e) => {
if (e.key === 'Enter') {
const input = e.currentTarget;
const description = input.value;

+++ const response = await fetch('/todo', {
method: 'POST',
body: JSON.stringify({ description }),
headers: {
'Content-Type': 'application/json'
}
});+++

input.value = '';
}
if (e.key !== 'Enter') return;

const input = e.currentTarget;
const description = input.value;

+++ const response = await fetch('/todo', {
method: 'POST',
body: JSON.stringify({ description }),
headers: {
'Content-Type': 'application/json'
}
});+++

input.value = '';
}}
/>
```
Expand Down Expand Up @@ -59,27 +59,27 @@ We're returning a response with a [201 Created](https://httpstatusdogs.com/201-c
type="text"
autocomplete="off"
on:keydown={async (e) => {
if (e.key === 'Enter') {
const input = e.currentTarget;
const description = input.value;

const response = await fetch('/todo', {
method: 'POST',
body: JSON.stringify({ description }),
headers: {
'Content-Type': 'application/json'
}
});

+++ const { id } = await response.json();

data.todos = [...data.todos, {
id,
description
}];+++

input.value = '';
}
if (e.key !== 'Enter') return;

const input = e.currentTarget;
const description = input.value;

const response = await fetch('/todo', {
method: 'POST',
body: JSON.stringify({ description }),
headers: {
'Content-Type': 'application/json'
}
});

+++ const { id } = await response.json();

data.todos = [...data.todos, {
id,
description
}];+++

input.value = '';
}}
/>
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
type="text"
autocomplete="off"
on:keydown={async (e) => {
if (e.key === 'Enter') {
const input = e.currentTarget;
const description = input.value;

// TODO handle submit
if (e.key !== 'Enter') return;

input.value = '';
}
const input = e.currentTarget;
const description = input.value;

// TODO handle submit

input.value = '';
}}
/>
</label>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,27 @@
type="text"
autocomplete="off"
on:keydown={async (e) => {
if (e.key === 'Enter') {
const input = e.currentTarget;
const description = input.value;

const response = await fetch('/todo', {
method: 'POST',
body: JSON.stringify({ description }),
headers: {
'Content-Type': 'application/json'
}
});
if (e.key !== 'Enter') return;

const { id } = await response.json();
const input = e.currentTarget;
const description = input.value;

const response = await fetch('/todo', {
method: 'POST',
body: JSON.stringify({ description }),
headers: {
'Content-Type': 'application/json'
}
});

data.todos = [...data.todos, {
id,
description
}];
const { id } = await response.json();

input.value = '';
}
data.todos = [...data.todos, {
id,
description
}];

input.value = '';
}}
/>
</label>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,27 @@
type="text"
autocomplete="off"
on:keydown={async (e) => {
if (e.key === 'Enter') {
const input = e.currentTarget;
const description = input.value;

const response = await fetch('/todo', {
method: 'POST',
body: JSON.stringify({ description }),
headers: {
'Content-Type': 'application/json'
}
});
if (e.key !== 'Enter') return;

const { id } = await response.json();
const input = e.currentTarget;
const description = input.value;

const response = await fetch('/todo', {
method: 'POST',
body: JSON.stringify({ description }),
headers: {
'Content-Type': 'application/json'
}
});

data.todos = [...data.todos, {
id,
description
}];
const { id } = await response.json();

input.value = '';
}
data.todos = [...data.todos, {
id,
description
}];

input.value = '';
}}
/>
</label>
Expand Down
8 changes: 4 additions & 4 deletions src/routes/tutorial/[slug]/Chrome.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
dispatch('change', { value: e.currentTarget.value });
}}
on:keydown={(e) => {
if (e.key === 'Enter') {
dispatch('change', { value: e.currentTarget.value });
e.currentTarget.blur();
}
if (e.key !== 'Enter') return;

dispatch('change', { value: e.currentTarget.value });
e.currentTarget.blur();
}}
/>

Expand Down