You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

22 lines
609 B

3 months ago
  1. import { Controller } from "@hotwired/stimulus";
  2. // Connects to data-controller="hot-search"
  3. export default class extends Controller {
  4. connect() {
  5. this.debouncedSearch = debounce(this.debouncedSearch.bind(this), 400)
  6. }
  7. debouncedSearch() {
  8. Turbo.visit('/scores?q[name_cont]=' + this.element.value, { frame: 'scores', turbo: true, acceptsStreamResponse: true })
  9. }
  10. }
  11. function debounce(callback, delay) {
  12. let timeoutId;
  13. return (...args) => {
  14. if (timeoutId) { clearTimeout(timeoutId) }
  15. timeoutId = setTimeout(() => {
  16. callback(...args)
  17. timeoutId = null
  18. }, delay)
  19. }
  20. }