Skip to content

Commit 5c0798e

Browse files
committed
feat: add resource search controller
1 parent 2b189c7 commit 5c0798e

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

app/components/avo/views/resource_index_component.html.erb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,16 @@
3636
<div class="flex flex-col xs:flex-row xs:justify-between space-y-2 xs:space-y-0 py-4 <%= "hidden" unless header_visible? %>">
3737
<div class="flex items-center px-4 w-64">
3838
<% if show_search_input %>
39-
<%= render partial: "avo/partials/resource_search", locals: {resource: @resource.route_key, via_reflection: via_reflection} %>
39+
<%#= render partial: "avo/partials/resource_search", locals: {resource: @resource.route_key, via_reflection: via_reflection} %>
40+
41+
<input type="text"
42+
name="q"
43+
id="resource_search"
44+
data-controller="resource-search"
45+
data-resource-search-target="input"
46+
data-action="input->resource-search#search"
47+
placeholder="Type to search..."
48+
class="block w-full rounded-md border-gray-300 shadow-sm focus:border-primary-500 focus:ring-primary-500 sm:text-sm">
4049
<% else %>
4150
<%# Offset for the space-y-2 property when the search is missing %>
4251
<div class="-mb-2"></div>

app/javascript/js/controllers.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import RecordSelectorController from './controllers/record_selector_controller'
3535
import ReloadBelongsToFieldController from './controllers/fields/reload_belongs_to_field_controller'
3636
import ResourceEditController from './controllers/resource_edit_controller'
3737
import ResourceIndexController from './controllers/resource_index_controller'
38+
import ResourceSearchController from './controllers/resource_search_controller'
3839
import ResourceShowController from './controllers/resource_show_controller'
3940
import SearchController from './controllers/search_controller'
4041
import SelectController from './controllers/select_controller'
@@ -80,6 +81,7 @@ application.register('preview', PreviewController)
8081
application.register('record-selector', RecordSelectorController)
8182
application.register('resource-edit', ResourceEditController)
8283
application.register('resource-index', ResourceIndexController)
84+
application.register('resource-search', ResourceSearchController)
8385
application.register('resource-show', ResourceShowController)
8486
application.register('search', SearchController)
8587
application.register('select', SelectController)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Controller } from "@hotwired/stimulus"
2+
import { get } from "@rails/request.js"
3+
4+
export default class extends Controller {
5+
static targets = ['input']
6+
7+
connect() {
8+
console.log('Resource search controller connected')
9+
}
10+
11+
async search() {
12+
const query = this.inputTarget.value
13+
const currentUrl = new URL(window.location.href)
14+
currentUrl.searchParams.set('q', query)
15+
16+
try {
17+
await get(currentUrl.pathname + currentUrl.search, {
18+
responseKind: 'turbo-stream'
19+
})
20+
} catch (error) {
21+
console.error('Error performing search:', error)
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)