Skip to content

Commit 205de55

Browse files
committed
Allow using events used by rails-ujs
Based on the code source https://github.com/rails/rails/blob/main/actionview/app/assets/javascripts/rails-ujs.js
1 parent 53a7e0f commit 205de55

File tree

1 file changed

+44
-10
lines changed

1 file changed

+44
-10
lines changed

Diff for: src/index.ts

+44-10
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,50 @@
1+
// Allow to use the Rails UJS events
2+
// UJS code source: https://github.com/rails/rails/blob/main/actionview/app/assets/javascripts/rails-ujs.js
3+
export type WindowWithRailsUJSEventMap = WindowEventMap & {
4+
"rails:attachBindings": CustomEvent
5+
"ujs:everythingStopped": CustomEvent
6+
confirm: CustomEvent
7+
"confirm:complete": CustomEvent<[boolean]>
8+
"ajax:before": CustomEvent
9+
"ajax:stopped": CustomEvent
10+
"ajax:beforeSend": CustomEvent<
11+
[
12+
XMLHttpRequest,
13+
{
14+
url: string
15+
type: string
16+
data: string
17+
dataType: string
18+
accept: string
19+
}
20+
]
21+
>
22+
"ajax:send": CustomEvent<[XMLHttpRequest]>
23+
// response has to be casted afterwards by the event handler
24+
"ajax:success": CustomEvent<
25+
[(response: unknown, textStatus: string, xhr: XMLHttpRequest) => void]
26+
>
27+
"ajax:error": CustomEvent<
28+
[(response: unknown, textStatus: string, xhr: XMLHttpRequest) => void]
29+
>
30+
"ajax:complete": CustomEvent<
31+
[(xhr: XMLHttpRequest, textStatus: string) => void]
32+
>
33+
}
34+
135
export interface EnhancedHTMLElement {
236
isEnhancedHTMLElement: true
3-
on: <K extends keyof WindowEventMap>(
37+
on: <K extends keyof WindowWithRailsUJSEventMap>(
438
this: HTMLElement & EnhancedHTMLElement,
539
type: K,
6-
callback: (event: WindowEventMap[K]) => void,
40+
callback: (event: WindowWithRailsUJSEventMap[K]) => void,
741
options?: AddEventListenerOptions
842
) => () => void
9-
onDelegate: <K extends keyof WindowEventMap>(
43+
onDelegate: <K extends keyof WindowWithRailsUJSEventMap>(
1044
this: HTMLElement & EnhancedHTMLElement,
1145
childSelector: string,
1246
type: K,
13-
callback: (event: WindowEventMap[K]) => void,
47+
callback: (event: WindowWithRailsUJSEventMap[K]) => void,
1448
options?: AddEventListenerOptions
1549
) => () => void
1650
query: typeof query
@@ -31,7 +65,7 @@ const enhancedHTMLElementImpl: EnhancedHTMLElement = {
3165
on(type, callback, options) {
3266
const attachedCallback = (e: Event) => {
3367
// wrapped in a function to mimic the once configuration of the native option, which is not well supported (IE 11)
34-
callback.call(e.target, e as WindowEventMap[typeof type])
68+
callback.call(e.target, e as WindowWithRailsUJSEventMap[typeof type])
3569
if (options && options.once) {
3670
this.removeEventListener(type, attachedCallback)
3771
}
@@ -54,7 +88,7 @@ const enhancedHTMLElementImpl: EnhancedHTMLElement = {
5488

5589
if (target) {
5690
overrideEventCurrentTarget(e, target)
57-
callback.call(target, e as WindowEventMap[typeof type])
91+
callback.call(target, e as WindowWithRailsUJSEventMap[typeof type])
5892
}
5993
}
6094

@@ -73,17 +107,17 @@ const enhancedHTMLElementImpl: EnhancedHTMLElement = {
73107

74108
export interface EnhancedHTMLElementList {
75109
isEnhancedHTMLElementList: true
76-
on: <K extends keyof WindowEventMap>(
110+
on: <K extends keyof WindowWithRailsUJSEventMap>(
77111
this: (HTMLElement & EnhancedHTMLElement)[] & EnhancedHTMLElementList,
78112
type: K,
79-
callback: (event: WindowEventMap[K]) => void,
113+
callback: (event: WindowWithRailsUJSEventMap[K]) => void,
80114
options?: AddEventListenerOptions
81115
) => () => void
82-
onDelegate: <K extends keyof WindowEventMap>(
116+
onDelegate: <K extends keyof WindowWithRailsUJSEventMap>(
83117
this: (HTMLElement & EnhancedHTMLElement)[] & EnhancedHTMLElementList,
84118
childSelector: string,
85119
type: K,
86-
callback: (event: WindowEventMap[K]) => void,
120+
callback: (event: WindowWithRailsUJSEventMap[K]) => void,
87121
options?: AddEventListenerOptions
88122
) => () => void
89123
}

0 commit comments

Comments
 (0)