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); diff --git a/one-event.test.ts b/one-event.test.ts index 7a491d9..7bcb048 100644 --- a/one-event.test.ts +++ b/one-event.test.ts @@ -23,3 +23,10 @@ test('should resolve with `undefined` if the signal has already aborted', async const event = await promise; expect(event).toBeUndefined(); }); + +test('should accept an array of selectors', async t => { + const promise = oneEvent(['a', 'b'], 'click'); + anchor.click(); + const event = await promise; + expect(event).toBeInstanceOf(MouseEvent); +}); diff --git a/one-event.ts b/one-event.ts index fb62414..239829e 100644 --- a/one-event.ts +++ b/one-event.ts @@ -13,7 +13,7 @@ async function oneEvent< TElement extends Element = ParseSelector, TEventType extends EventType = EventType, >( - selector: Selector, + selector: Selector | Selector[], type: TEventType, options?: DelegateOptions ): Promise>; @@ -22,7 +22,7 @@ async function oneEvent< TElement extends Element = HTMLElement, TEventType extends EventType = EventType, >( - selector: string, + selector: string | string[], type: TEventType, options?: DelegateOptions ): Promise>; @@ -32,7 +32,7 @@ async function oneEvent< TElement extends Element, TEventType extends EventType = EventType, >( - selector: string, + selector: string | string[], type: TEventType, options: DelegateOptions = {}, ): Promise | undefined> { diff --git a/package.json b/package.json index ad02927..7dbf544 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "delegate-it", - "version": "6.1.0", + "version": "6.2.0", "description": "Lightweight and modern event delegation in the browser", "keywords": [ "delegate",