Files
fizzy/app/javascript/controllers/form_controller.js
T
2025-10-01 16:56:55 -05:00

54 lines
1.0 KiB
JavaScript

import { Controller } from "@hotwired/stimulus"
import { debounce } from "helpers/timing_helpers";
export default class extends Controller {
static targets = [ "cancel", "submit" ]
static values = {
debounceTimeout: { type: Number, default: 300 }
}
initialize() {
this.debouncedSubmit = debounce(this.debouncedSubmit.bind(this), this.debounceTimeoutValue)
}
submit() {
this.element.requestSubmit()
}
debouncedSubmit(event) {
this.submit(event)
}
submitToTopTarget(event) {
this.element.setAttribute("data-turbo-frame", "_top")
this.submit()
}
reset() {
this.element.reset()
}
cancel() {
this.cancelTarget?.click()
}
preventAttachment(event) {
event.preventDefault()
}
disableEmptySubmit(event) {
var value = event.target.value.replace(/<\/?[^>]+(>|$)/g, "")
if (value) {
this.submitTarget.removeAttribute("disabled")
} else {
this.submitTarget.setAttribute("disabled", true)
}
}
select(event) {
event.target.select()
}
}