Skip to content

Commit e71bd5c

Browse files
committed
Add custom input action option
Adds the ability to supress an event if an input element has focus
1 parent 422eb81 commit e71bd5c

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

docs/reference/actions.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ Custom action option | Description
170170
`:stop` | calls `.stopPropagation()` on the event before invoking the method
171171
`:prevent` | calls `.preventDefault()` on the event before invoking the method
172172
`:self` | only invokes the method if the event was fired by the element itself
173+
`:!input` | suppress an event if it was fired while an input element has focus
173174

174175
You can register your own action options with the `Application.registerActionOption` method.
175176

src/core/action_descriptor.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,19 @@ export const defaultActionDescriptorFilters: ActionDescriptorFilters = {
3030
return true
3131
}
3232
},
33+
34+
input({ event, value }) {
35+
if (value) return true
36+
37+
const target = event.target
38+
if (!(target instanceof Element)) return true
39+
40+
const isInput =
41+
target.tagName === "INPUT" ||
42+
target.tagName === "TEXTAREA" ||
43+
(target instanceof HTMLElement && target.isContentEditable)
44+
return !isInput
45+
},
3346
}
3447

3548
export interface ActionDescriptor {

src/tests/modules/core/event_options_tests.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export default class EventOptionsTests extends LogControllerTestCase {
77
<div data-controller="c d">
88
<button></button>
99
<details></details>
10+
<input>
1011
</div>
1112
<div id="outside"></div>
1213
`
@@ -178,6 +179,22 @@ export default class EventOptionsTests extends LogControllerTestCase {
178179
this.assertNoActions()
179180
}
180181

182+
async "test true input option"() {
183+
await this.setAction(this.inputElement, "keydown@window->c#log:input")
184+
185+
await this.triggerEvent(this.inputElement, "keydown")
186+
187+
this.assertActions({ name: "log", eventType: "keydown" })
188+
}
189+
190+
async "test false input option"() {
191+
await this.setAction(this.inputElement, "keydown@window->c#log:!input")
192+
193+
await this.triggerEvent(this.inputElement, "keydown")
194+
195+
this.assertNoActions()
196+
}
197+
181198
async "test custom action option callback params contain the controller instance"() {
182199
let lastActionOptions: { controller?: Controller<Element> } = {}
183200

@@ -313,4 +330,8 @@ export default class EventOptionsTests extends LogControllerTestCase {
313330
get detailsElement() {
314331
return this.findElement("details")
315332
}
333+
334+
get inputElement() {
335+
return this.findElement("input")
336+
}
316337
}

0 commit comments

Comments
 (0)