Skip to content

Commit a5126fc

Browse files
登録ページとログインページで、はじめの入力欄にフォーカスする #8
1 parent c70c7ae commit a5126fc

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

.vscode/settings.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{
22
"cSpell.words": [
3+
"instanceof",
4+
"onfocus",
35
"sveltejs"
46
]
57
}

src/routes/login/+page.svelte

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
11
<script lang="ts">
2+
import { onMount } from 'svelte'
23
import type { ActionData } from './$types'
34
45
export let form: ActionData
6+
7+
let username_element: HTMLInputElement
8+
9+
onMount(() => {
10+
document.onfocus = (event) => {
11+
if (event.target instanceof HTMLInputElement) event.target.select()
12+
}
13+
14+
username_element.select()
15+
})
516
</script>
617

718
<h1>Log in</h1>
819

920
<form method="POST">
10-
<input type="text" name="username" placeholder="Username" required value={form?.username ?? ''} />
21+
<input
22+
type="text"
23+
name="username"
24+
placeholder="Username"
25+
required
26+
value={form?.username ?? ''}
27+
bind:this={username_element}
28+
/>
1129
<input type="password" name="password" placeholder="Password" required />
1230

1331
{#if form?.missing}<p class="error">Username and password is required.</p>{/if}

src/routes/register/+page.svelte

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
11
<script lang="ts">
2-
import type { ActionData } from './$types';
2+
import { onMount } from 'svelte'
3+
import type { ActionData } from './$types'
34
4-
export let form: ActionData;
5+
export let form: ActionData
6+
7+
let username_element: HTMLInputElement
8+
9+
onMount(() => {
10+
document.onfocus = (event) => {
11+
if (event.target instanceof HTMLInputElement) event.target.select()
12+
}
13+
14+
username_element.select()
15+
})
516
</script>
617

718
<h1>Register</h1>
819

920
<form method="POST">
10-
<input type="text" name="username" placeholder="Username" required value={form?.username ?? ''} />
21+
<input
22+
type="text"
23+
name="username"
24+
placeholder="Username"
25+
required
26+
value={form?.username ?? ''}
27+
bind:this={username_element}
28+
/>
1129
<input type="email" name="email" placeholder="email" required value={form?.email ?? ''} />
1230
<input type="password" name="password" placeholder="Password" required />
1331

0 commit comments

Comments
 (0)