-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsearchform.php
More file actions
74 lines (70 loc) · 2.41 KB
/
searchform.php
File metadata and controls
74 lines (70 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<form
x-data="baseSearchComponent()"
role="search"
method="get"
class="searchform"
:class="{ 'searchform--loading': loading }"
action="<?php echo esc_url(home_url('/')); ?>"
@click.outside="focused = false;"
@submit.prevent
>
<label class="sr-only" for="s"><?php _e('Search for:', 'base'); ?></label>
<?php echo base_get_inline_svg_icon('search.svg', 'searchform__icon'); ?>
<input
x-model="query"
@input.debounce.500="performSearch()"
@focus="focused = true"
type="text"
class="searchform__control"
value="<?php echo get_search_query(); ?>"
name="s"
placeholder="<?php _e('What are you looking for?', 'base'); ?>"
>
<span class="preloader--circle searchform__preloader"></span>
<div x-cloak class="searchform__results" x-show="focused && searched">
<div class="searchform__helper">
<div x-show="results.length > 0" class="searchform__summary" x-text="results.length + ' <?php _e('result(s) found.', 'base'); ?>'"></div>
<ul x-show="results.length > 0">
<template x-for="result in results" :key="result.id">
<li><a :href="result.url" x-text="result.title"></a></li>
</template>
</ul>
<p
x-show="results.length == 0"
class="searchform__no-result"
>
<?php _e('No results found.', 'base'); ?>
</p>
</div>
</div>
</form>
<script>
function baseSearchComponent() {
return {
query: '',
results: [],
searched: false,
loading: false,
focused: false,
async performSearch() {
this.loading = true;
if (this.query.length < 3) {
this.results = [];
this.loading = false;
return;
}
try {
const response = await fetch('<?php echo admin_url( 'admin-ajax.php' ); ?>?action=ajax_search&s=' + encodeURIComponent(this.query));
if (!response.ok) throw new Error('Error fetching search results');
const data = await response.json();
this.results = data;
} catch (error) {
console.error(error);
} finally {
this.loading = false;
this.searched = true;
}
}
}
}
</script>