Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using events used by rails-ujs #415

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 44 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,50 @@
// Allow to use the Rails UJS events
// UJS code source: https://github.com/rails/rails/blob/main/actionview/app/assets/javascripts/rails-ujs.js
export type WindowWithRailsUJSEventMap = WindowEventMap & {
"rails:attachBindings": CustomEvent
"ujs:everythingStopped": CustomEvent
confirm: CustomEvent
"confirm:complete": CustomEvent<[boolean]>
"ajax:before": CustomEvent
"ajax:stopped": CustomEvent
"ajax:beforeSend": CustomEvent<
[
XMLHttpRequest,
{
url: string
type: string
data: string
dataType: string
accept: string
}
]
>
"ajax:send": CustomEvent<[XMLHttpRequest]>
// response has to be casted afterwards by the event handler
"ajax:success": CustomEvent<
[(response: unknown, textStatus: string, xhr: XMLHttpRequest) => void]
>
"ajax:error": CustomEvent<
[(response: unknown, textStatus: string, xhr: XMLHttpRequest) => void]
>
"ajax:complete": CustomEvent<
[(xhr: XMLHttpRequest, textStatus: string) => void]
>
}

export interface EnhancedHTMLElement {
isEnhancedHTMLElement: true
on: <K extends keyof WindowEventMap>(
on: <K extends keyof WindowWithRailsUJSEventMap>(
this: HTMLElement & EnhancedHTMLElement,
type: K,
callback: (event: WindowEventMap[K]) => void,
callback: (event: WindowWithRailsUJSEventMap[K]) => void,
options?: AddEventListenerOptions
) => () => void
onDelegate: <K extends keyof WindowEventMap>(
onDelegate: <K extends keyof WindowWithRailsUJSEventMap>(
this: HTMLElement & EnhancedHTMLElement,
childSelector: string,
type: K,
callback: (event: WindowEventMap[K]) => void,
callback: (event: WindowWithRailsUJSEventMap[K]) => void,
options?: AddEventListenerOptions
) => () => void
query: typeof query
Expand All @@ -31,7 +65,7 @@ const enhancedHTMLElementImpl: EnhancedHTMLElement = {
on(type, callback, options) {
const attachedCallback = (e: Event) => {
// wrapped in a function to mimic the once configuration of the native option, which is not well supported (IE 11)
callback.call(e.target, e as WindowEventMap[typeof type])
callback.call(e.target, e as WindowWithRailsUJSEventMap[typeof type])
if (options && options.once) {
this.removeEventListener(type, attachedCallback)
}
Expand All @@ -54,7 +88,7 @@ const enhancedHTMLElementImpl: EnhancedHTMLElement = {

if (target) {
overrideEventCurrentTarget(e, target)
callback.call(target, e as WindowEventMap[typeof type])
callback.call(target, e as WindowWithRailsUJSEventMap[typeof type])
}
}

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

export interface EnhancedHTMLElementList {
isEnhancedHTMLElementList: true
on: <K extends keyof WindowEventMap>(
on: <K extends keyof WindowWithRailsUJSEventMap>(
this: (HTMLElement & EnhancedHTMLElement)[] & EnhancedHTMLElementList,
type: K,
callback: (event: WindowEventMap[K]) => void,
callback: (event: WindowWithRailsUJSEventMap[K]) => void,
options?: AddEventListenerOptions
) => () => void
onDelegate: <K extends keyof WindowEventMap>(
onDelegate: <K extends keyof WindowWithRailsUJSEventMap>(
this: (HTMLElement & EnhancedHTMLElement)[] & EnhancedHTMLElementList,
childSelector: string,
type: K,
callback: (event: WindowEventMap[K]) => void,
callback: (event: WindowWithRailsUJSEventMap[K]) => void,
options?: AddEventListenerOptions
) => () => void
}
Expand Down