Skip to content

Commit 3b00c09

Browse files
🎯 Add with-target-checked
1 parent f0e813a commit 3b00c09

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

docs/with-target-checked.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
### withTargetChecked
2+
3+
Calls your provided event handler function with `event.target.checked` as the first value (if it exists). Passes the `event` along as the second argument in case you need it.
4+
5+
**Vanilla**
6+
7+
```js
8+
import { withTargetChecked } from "browser-event-utils";
9+
10+
myInputNode.addEventListener(
11+
"change",
12+
withTargetChecked((checked, event) => {
13+
// 👈 Note that you still get the event object if you need it
14+
// ...handle input change event...
15+
})
16+
);
17+
```
18+
19+
**React**
20+
21+
```jsx
22+
import React, { Component } from "react";
23+
import { withTargetChecked } from "browser-event-utils";
24+
25+
const handleCheck = withTargetChecked((checked, event) => {
26+
console.log(`Checked! ${checked}`);
27+
});
28+
29+
function MyInput() {
30+
return <input type="checkbox" onChange={handleCheck} />;
31+
}
32+
```
33+
34+
[![Edit with-target-value (React)](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/with-stop-immediate-propagation-react-st3fz?fontsize=14)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* eslint-disable @typescript-eslint/explicit-function-return-type */
2+
import withTargetChecked from '../with-target-checked';
3+
4+
5+
describe('withTargetChecked', () => {
6+
it('will not fall over if bad values are passed', () => {
7+
expect(() => withTargetChecked()()).not.toThrow();
8+
});
9+
it('calls consumer function with undefined if event.target.checked does not exist', () => {
10+
const mockConsumer = jest.fn();
11+
const mockEvent = {};
12+
withTargetChecked(mockConsumer)(mockEvent);
13+
expect(mockConsumer).toHaveBeenCalledWith(undefined, mockEvent);
14+
});
15+
it('calls the consumer function with event.target.checked if it exists', () => {
16+
const mockConsumer = jest.fn();
17+
const mockIsChecked = true;
18+
const mockEvent = { target: { checked: mockIsChecked } };
19+
withTargetChecked(mockConsumer)(mockEvent);
20+
expect(mockConsumer).toHaveBeenCalledWith(mockIsChecked, mockEvent);
21+
});
22+
});

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export { default as withPreventDefault } from './with-prevent-default';
22
export { default as withStopPropagation } from './with-stop-propagation';
33
export { default as withStopImmediatePropagation } from './with-stop-immediate-propagation';
4+
export { default as withTargetChecked } from './with-target-checked';
45
export { default as withTargetValue } from './with-target-value';
56
export { default as withKeyPress } from './with-keypress';

src/with-target-checked.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import withTarget from './helpers/with-target';
2+
import noop from './helpers/noop';
3+
import { EventType, EventHandler } from './index.d';
4+
5+
export type TWithTargetCheckedHandler = (value: string | null, event: EventType) => void;
6+
7+
/**
8+
* Accepts a function, who's first argument is event.target.checked (if exists),
9+
* followed by the full event object.
10+
*
11+
* @param {Function} fn - Consumer's handler function
12+
* @return {Function} - event handler function
13+
*/
14+
const withTargetChecked = (fn: TWithTargetCheckedHandler = noop): EventHandler => withTarget((target, event): void => fn(target?.checked, event));
15+
16+
export default withTargetChecked;

0 commit comments

Comments
 (0)