Skip to content

Commit 6a54398

Browse files
authored
feat: search form shortcut (#1420)
1 parent fd5655a commit 6a54398

File tree

4 files changed

+38
-9
lines changed

4 files changed

+38
-9
lines changed

src/unfold/static/unfold/js/app.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,26 @@ const sortRecords = (e) => {
2626
};
2727

2828
/*************************************************************
29-
* Searh dropdown
29+
* Search form
30+
*************************************************************/
31+
function searchForm() {
32+
return {
33+
applyShortcut(event) {
34+
if (
35+
event.key === "/" &&
36+
document.activeElement.tagName.toLowerCase() !== "input" &&
37+
document.activeElement.tagName.toLowerCase() !== "textarea" &&
38+
!document.activeElement.isContentEditable
39+
) {
40+
event.preventDefault();
41+
this.$refs.searchInput.focus();
42+
}
43+
},
44+
};
45+
}
46+
47+
/*************************************************************
48+
* Search dropdown
3049
*************************************************************/
3150
function searchDropdown() {
3251
return {

src/unfold/templates/admin/search_form.html

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,22 @@
22

33
{% if cl.search_fields %}
44
<div id="toolbar">
5-
<form id="changelist-search" method="get" role="search">
6-
<div class="bg-white border border-base-200 flex rounded-default overflow-hidden shadow-xs lg:w-96 focus-within:outline-2 focus-within:-outline-offset-2 focus-within:outline-primary-600 dark:bg-base-900 dark:border-base-700">
7-
<input class="font-medium grow min-w-0 h-9 px-3 text-font-default-light text-sm focus:outline-hidden dark:bg-base-900 dark:text-font-default-dark placeholder-shown:truncate placeholder-base-400" type="text" name="{{ search_var }}" value="{{ cl.query }}" id="searchbar" placeholder="{% if cl.search_help_text %}{{ cl.search_help_text }}{% else %}{% trans "Type to search" %}{% endif %}" />
8-
9-
<button type="submit" class="flex items-center px-2 focus:outline-hidden" id="searchbar-submit">
5+
<form id="changelist-search" method="get" role="search" x-data="searchForm()">
6+
<div class="bg-white border border-base-200 flex flex-row items-center px-3 rounded-default relative shadow-xs w-full focus-within:outline-2 focus-within:-outline-offset-2 focus-within:outline-primary-600 lg:w-96 dark:bg-base-900 dark:border-base-700">
7+
<button type="submit" class="flex items-center focus:outline-hidden" id="searchbar-submit">
108
<span class="material-symbols-outlined md-18 text-base-400 dark:text-base-500">search</span>
119
</button>
10+
11+
<input type="text"
12+
x-ref="searchInput"
13+
x-on:keydown.window="applyShortcut($event)"
14+
class="grow font-medium min-w-0 overflow-hidden p-2 placeholder-font-subtle-light truncate focus:outline-hidden dark:bg-base-900 dark:placeholder-font-subtle-dark dark:text-font-default-dark"
15+
name="{{ search_var }}"
16+
value="{{ cl.query }}"
17+
id="searchbar"
18+
placeholder="{% if cl.search_help_text %}{{ cl.search_help_text }}{% else %}{% trans "Type to search" %}{% endif %}" />
19+
20+
{% include "unfold/helpers/shortcut.html" with shortcut="/" %}
1221
</div>
1322

1423
{% for pair in cl.filter_params.items %}

src/unfold/templates/unfold/helpers/search.html

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222
placeholder="{% translate 'Search apps and models...' %}"
2323
aria-label="{% translate 'Filter navigation items' %}">
2424

25-
<kbd class="bg-base-50 border border-base-200 font-mono text-xs text-font-subtle-light rounded-xs px-1">
26-
t
27-
</kbd>
25+
{% include "unfold/helpers/shortcut.html" with shortcut="t" %}
2826
</div>
2927

3028
<div id="search-results" x-show="openSearchResults"></div>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<kbd class="bg-base-50 border border-base-200 font-mono text-xs text-font-subtle-light rounded-xs px-1 dark:bg-white/[.04] dark:border-base-700 dark:text-font-subtle-dark">
2+
{{ shortcut }}
3+
</kbd>

0 commit comments

Comments
 (0)