From 99ea1f04be650a0e8f1258a2b95dc3289d8c60f3 Mon Sep 17 00:00:00 2001 From: Federico Brigante Date: Tue, 20 Aug 2024 17:04:04 +0700 Subject: [PATCH 1/3] Accept array of selectors (#53) --- delegate.test.ts | 7 +++++++ delegate.ts | 8 ++++---- 2 files changed, 11 insertions(+), 4 deletions(-) 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); From 4157b539604cd10ca8fed5f6166757d9b0037ddd Mon Sep 17 00:00:00 2001 From: Federico Date: Tue, 20 Aug 2024 17:05:57 +0700 Subject: [PATCH 2/3] one-event: Accept array of selectors Followup to https://github.com/fregante/delegate-it/pull/53 --- one-event.test.ts | 7 +++++++ one-event.ts | 6 +++--- 2 files changed, 10 insertions(+), 3 deletions(-) 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> { From 5a73bdeaff2db09e5fc7997a70c6f7115e407d53 Mon Sep 17 00:00:00 2001 From: Federico Date: Tue, 20 Aug 2024 17:06:05 +0700 Subject: [PATCH 3/3] 6.2.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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",