Don't dismiss on any key, only with N or ESC

Also, this reworks the system to rely on stimulus event handling. This cover cases like unbinding events automatically when the controller get disconnected and such.

See:
https://3.basecamp.com/2914079/buckets/37331921/todos/8617430347
This commit is contained in:
Jorge Manrubia
2025-05-08 11:21:39 +02:00
parent 05e55adbaa
commit 5cd71a7699
3 changed files with 27 additions and 26 deletions
@@ -5,6 +5,10 @@ export default class extends Controller {
static targets = [ "input", "form", "confirmation" ]
static classes = [ "error", "confirmation", "help" ]
disconnect() {
if (this.waitingForConfirmation) { this.#reset() }
}
// Actions
focus() {
@@ -26,6 +30,13 @@ export default class extends Controller {
this.element.classList.remove(this.helpClass)
}
handleKeyPress(event) {
if (this.waitingForConfirmation) {
this.#handleConfirmationKey(event.key.toLowerCase())
event.preventDefault()
}
}
handleCommandResponse(event) {
if (event.detail.success) {
this.#reset()
@@ -74,6 +85,8 @@ export default class extends Controller {
this.formTarget.reset()
this.inputTarget.value = inputValue
this.confirmationTarget.value = ""
this.waitingForConfirmation = false
this.originalInputValue = null
this.element.classList.remove(this.errorClass)
this.element.classList.remove(this.confirmationClass)
@@ -84,35 +97,23 @@ export default class extends Controller {
}
async #requestConfirmation(message) {
const originalInputValue = this.inputTarget.value
this.originalInputValue = this.inputTarget.value
this.element.classList.add(this.confirmationClass)
this.inputTarget.value = `${message}? [Y/n] `
try {
await this.#waitForConfirmation()
this.#submitWithConfirmation(originalInputValue)
} catch {
this.#reset(originalInputValue)
this.waitingForConfirmation = true
}
#handleConfirmationKey(key) {
if (key === "enter" || key === "y") {
this.#submitWithConfirmation()
} else if (key === "escape" || key === "n") {
this.#reset(this.originalInputValue)
}
}
#waitForConfirmation() {
return new Promise((resolve, reject) => {
this.inputTarget.addEventListener("keydown", (event) => {
event.preventDefault()
const key = event.key.toLowerCase()
if (key === "enter" || key === "y") {
resolve()
} else {
reject()
}
}, { once: true })
})
}
#submitWithConfirmation(inputValue) {
this.inputTarget.value = inputValue
#submitWithConfirmation() {
this.inputTarget.value = this.originalInputValue
this.confirmationTarget.value = "confirmed"
this.formTarget.requestSubmit()
}