diff --git a/delegate.test.ts b/delegate.test.ts index aa68b09..340cad8 100644 --- a/delegate.test.ts +++ b/delegate.test.ts @@ -26,6 +26,13 @@ test('should remove an event listener', () => { expect(spy).toHaveBeenCalledTimes(0); }); +test('should handle multiple selectors', () => { + const spy = vi.fn(); + delegate(['a', 'b'], 'click', spy); + anchor.click(); + expect(spy).toHaveBeenCalledTimes(1); +}); + test('should not add an event listener of the controller has already aborted', () => { const spy = vi.fn(); delegate('a', 'click', spy, {signal: AbortSignal.abort()}); diff --git a/delegate.ts b/delegate.ts index 34659f0..c3cc363 100644 --- a/delegate.ts +++ b/delegate.ts @@ -73,7 +73,7 @@ function delegate< TElement extends Element = ParseSelector, TEventType extends EventType = EventType, >( - selector: Selector, + selector: Selector | Selector[], type: TEventType, callback: DelegateEventHandler, options?: DelegateOptions @@ -83,7 +83,7 @@ function delegate< TElement extends Element = HTMLElement, TEventType extends EventType = EventType, >( - selector: string, + selector: string | string[], type: TEventType, callback: DelegateEventHandler, options?: DelegateOptions @@ -94,7 +94,7 @@ function delegate< TElement extends Element, TEventType extends EventType = EventType, >( - selector: string, + selector: string | string[], type: TEventType, callback: DelegateEventHandler, options: DelegateOptions = {}, @@ -114,7 +114,7 @@ function delegate< // Handle the regular Element usage const capture = Boolean(typeof options === 'object' ? options.capture : options); const listenerFunction = (event: Event): void => { - const delegateTarget = safeClosest(event, selector); + const delegateTarget = safeClosest(event, String(selector)); if (delegateTarget) { const delegateEvent = Object.assign(event, {delegateTarget}); callback.call(baseElement, delegateEvent as DelegateEvent);