Skip to content

Commit 932b3d4

Browse files
🏹 Adds a withTarget helper
For dealing with getting event targets
1 parent 99ffd85 commit 932b3d4

File tree

6 files changed

+49
-9
lines changed

6 files changed

+49
-9
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* eslint-disable @typescript-eslint/explicit-function-return-type */
2+
import withTarget from '../with-target';
3+
import { EventType, $TSFixMe } from '../../index.d';
4+
5+
6+
describe('withTarget', () => {
7+
it('calls the provided method with event.target (if it exists)', () => {
8+
const mockEvent: EventType | $TSFixMe = {
9+
target: 'mockEvent',
10+
};
11+
const mock = jest.fn();
12+
withTarget(mock)(mockEvent);
13+
expect(mock).toHaveBeenCalledWith(mockEvent);
14+
});
15+
});

src/helpers/noop.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* No-operation
3+
*/
4+
const noop = (): void => {};
5+
6+
export default noop;

src/helpers/with-event-factory.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import callIfExists from './call-if-exists';
2+
import noop from './noop';
23
import { EventType, EventHandler, UserEventHandler } from '../index.d';
34

45
export type TWithEventFactory = (method: string) => UserEventHandler;
@@ -9,7 +10,7 @@ export type TWithEventFactory = (method: string) => UserEventHandler;
910
* @param {Function} method - the event method which you want to call
1011
* @return {Function} - function which accepts the consumer's event handler function and returns the final event handler function
1112
*/
12-
const withEventFactory: TWithEventFactory = (method: string = ''): UserEventHandler => (fn: EventHandler): EventHandler => (event: EventType): void => {
13+
const withEventFactory: TWithEventFactory = (method: string = ''): UserEventHandler => (fn: EventHandler = noop): EventHandler => (event: EventType = {} as any): void => {
1314
callIfExists(method)(event);
1415
return fn(event);
1516
};

src/helpers/with-target.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { EventType, EventHandler, $TSFixMe } from '../index.d';
2+
import noop from './noop';
3+
4+
5+
export type TEventTarget = EventTarget | HTMLInputElement | $TSFixMe;
6+
export type TWithTargetHandler = (target: TEventTarget, event: EventType) => void;
7+
export type TWithTarget = (fn: TWithTargetHandler) => EventHandler;
8+
9+
/**
10+
* Calls a fn with event.target (if it exists)
11+
*
12+
* @param fn Function which will be called with event.target (if it exists)
13+
*/
14+
const withTarget: TWithTarget = (fn: TWithTargetHandler = noop): EventHandler => (event: EventType = {} as $TSFixMe): void => {
15+
return fn(event?.target, event);
16+
};
17+
18+
export default withTarget;

src/with-keypress.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { EventType, EventHandler, UserEventHandler } from './index.d';
2+
import noop from './helpers/noop';
23

34
/**
45
* Generic withKeypress generator
56
*
67
* @param {string} key The key which must be pressed for the fn to be invoked
78
*/
8-
const withKeyPress = (key: string): UserEventHandler => (fn: EventHandler): EventHandler => (event: EventType): void => {
9+
const withKeyPress = (key: string): UserEventHandler => (fn: EventHandler = noop): EventHandler => (event: EventType = {} as any): void => {
910
if (event.key === key) {
1011
return fn(event);
1112
}

src/with-target-value.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
export type WithTargetValue = (value: string | undefined, event: KeyboardEvent | InputEvent | any) => void;
1+
import withTarget from './helpers/with-target';
2+
import noop from './helpers/noop';
3+
import { EventType, EventHandler } from './index.d';
24

35

6+
export type TWithTargetValueHandler = (value: string | null, event: EventType) => void;
7+
48
/**
59
* Accepts a function, who's first argument is event.target.value (if exists),
610
* followed by the full event object.
711
*
812
* @param {Function} fn - Consumer's handler function
913
* @return {Function} - event handler function
1014
*/
11-
const withTargetValue = (fn: WithTargetValue) => (event: InputEvent | any) => {
12-
if (!event || !event.target) {
13-
return fn(undefined, event);
14-
}
15-
return fn(event.target.value, event);
16-
};
15+
const withTargetValue = (fn: TWithTargetValueHandler = noop): EventHandler => withTarget((target, event): void => fn(target?.value, event));
1716

1817
export default withTargetValue;

0 commit comments

Comments
 (0)