Skip to content

Commit 1c873df

Browse files
waximabbaxJeroenDeDauw
authored andcommitted
Focusing on the input after adding category
1 parent dcfbbe8 commit 1c873df

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

templates/ManageApprovers.mustache

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</form>
77
</div>
88

9-
<table class="form-container">
9+
<table class="form-container" id="categories-form-container">
1010
<thead>
1111
<tr>
1212
<th>User</th>
@@ -29,10 +29,13 @@
2929
</form>
3030
</div>
3131
{{/categories}}
32-
<form method="POST" action="" class="add-category-form">
33-
<input type="text" name="category" placeholder="New Category" required>
32+
<form method="POST" class="add-category-form">
33+
<input type="text" name="category" placeholder="New Category" id="{{username}}category" required>
3434
<input type="hidden" name="username" value="{{username}}">
35-
<button class="add-button" type="submit" name="action" value="add">Add</button>
35+
<input type="hidden" name="action" value="add">
36+
<button class="add-button" value="add" onclick="addCategory( event => event)">
37+
Add
38+
</button>
3639
</form>
3740
</td>
3841
</tr>
@@ -41,7 +44,29 @@
4144
</table>
4245
</div>
4346
<script>
44-
if( window.history.replaceState ) {
45-
window.history.replaceState( null, null, window.location.href );
47+
window.history.replaceState?.( null, null, window.location.href );
48+
49+
function addCategory( event ) {
50+
event.preventDefault();
51+
const form = event.target;
52+
53+
if( !form.checkValidity() ) {
54+
return form.reportValidity();
55+
}
56+
57+
fetch( window.location.href, { method: 'POST', body: new FormData( form ) } )
58+
.then( response => response.text() )
59+
.then( html => {
60+
const newDoc = new DOMParser().parseFromString( html, 'text/html' );
61+
const container = document.querySelector( "#categories-form-container" );
62+
const newContainer = newDoc.querySelector( "#categories-form-container" );
63+
64+
container.replaceWith( newContainer );
65+
66+
const username = form.elements.username.value;
67+
const inputField = document.getElementById( `${ username }category` );
68+
inputField?.focus();
69+
} )
70+
.catch( error => console.error( 'Error:', error ) );
4671
}
4772
</script>

0 commit comments

Comments
 (0)