Sha256: cf7bc925307618f1350b118eb5445a5f9d0bf3b5a928386a88f1b0a3c6721c86
Contents?: true
Size: 1.23 KB
Versions: 31
Compression:
Stored size: 1.23 KB
Contents
import { Controller } from "@hotwired/stimulus"; export default class IndexActionsController extends Controller { static targets = ["create", "search", "sort"]; initialize() { // debounce search this.update = debounce(this, this.update); } disconnect() { clearTimeout(this.timer); } create() { this.createTarget.click(); } search() { this.searchTarget.focus(); } clear() { this.searchTarget.value = ""; this.searchTarget.closest("form").requestSubmit(); } update() { this.searchTarget.closest("form").requestSubmit(); } submit() { const shouldFocus = document.activeElement === this.searchTarget; if (this.searchTarget.value === "") { this.searchTarget.disabled = true; } if (this.sortTarget.value === "") { this.sortTarget.disabled = true; } // restore state and focus after submit Promise.resolve().then(() => { this.searchTarget.disabled = false; this.sortTarget.disabled = false; if (shouldFocus) { this.searchTarget.focus(); } }); } } function debounce(self, f) { return (...args) => { clearTimeout(self.timer); self.timer = setTimeout(() => { f.apply(self, ...args); }, 300); }; }
Version data entries
31 entries across 31 versions & 1 rubygems