I’m wondering how Basecamp pulled this off without the use of JavaScript*. I’m doing a live search but instead of showing a dropdown with results, I’m updating the page itself.
*Javan says:
Say what now? Ok. I’m using Turbolinks to make a GET request which passes a query string to the controller, updates the variable which renders the value:
# Controller
def index
@customers = Customers.all
@query = ''
if params[:q].present?
@customers = Customer.all.limit(4)
@query = params[:q]
end
end
// Simulus controller
[..]
search(event) {
const query = event.target.value.toLowerCase()
Turbolinks.visit("/customers?q=" + query)
}
[..]
<!-- Filter section -->
<input
type="text"
data-target="foo.searchinput"
data-action="keyup->foo#search"
placeholder="Search"
value="<%= @query %>"
autocomplete="off"
/>
<!-- The customers list -->
<%= render partial: 'list', locals: {customers: @customers} %>
The logic works but here’s what I don’t get:
Turbolinks is fast because it doesn’t reload the page when you follow a link.
What? The entire dom is being replaced. Ok, maybe this happens after I hit the controller. My issue is that the input focus is lost and if I do use js to add autofocus
, the cursor goes back to the start of the value. Could someone show me how to get a live search going with the result updating the page while the input is still being focused? Possible? I’m coming from a js-only client (React).