Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce the need for custom javascript by leveraging Turbo and HTML conventions: - By using a form, Turbo handles submission for us and fires events we can use to toggle the loading icon. - Handle focusing the search input with html autofocus - Use css to handle display of loading icon
- Loading branch information
1 parent
047dd22
commit 94f2a7c
Showing
5 changed files
with
35 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
form#search .c-loader { | ||
display: none; | ||
} | ||
|
||
form#search.loading .c-loader { | ||
display: revert; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,18 @@ | ||
import { Controller } from '@hotwired/stimulus'; | ||
|
||
export default class extends Controller { | ||
static targets = ['loader', 'input']; | ||
static targets = ['input']; | ||
|
||
connect() { | ||
this.inputTarget.value = this.inputTarget.getAttribute('value'); | ||
} | ||
submit(e) { | ||
this.inputTarget.value = this.inputTarget.value.trim(); | ||
|
||
disconnect() { | ||
this.loaderTarget.classList.add('u-display-none'); | ||
this.inputTarget.value = ''; | ||
if (this.inputTarget.value.length === 0) { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
} | ||
} | ||
|
||
query(event) { | ||
const query = event.target.value.trim(); | ||
|
||
if (event.key != 'Enter' || query == '') { return; } | ||
|
||
this.loaderTarget.classList.remove('u-display-none'); | ||
const queryUrl = `/search${query ? `?query=${query}` : ''}`; | ||
|
||
Turbo.visit(queryUrl); | ||
this._focusInput(); | ||
} | ||
|
||
_focusInput() { | ||
document.addEventListener('turbo:load', () => { | ||
const searchElement = document.querySelector('#js-search-input'); | ||
const searchValueLength = searchElement.value.length; | ||
|
||
searchElement.focus(); | ||
searchElement.setSelectionRange(searchValueLength, searchValueLength); | ||
}, { once: true }); | ||
loading(e) { | ||
this.element.classList.toggle('loading', e.type == 'turbo:submit-start'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters