Skip to content

Commit 2981ece

Browse files
Merge pull request #22 from /issues/20
Add withTargetChecked
2 parents 410f0b4 + 96ecd4a commit 2981ece

18 files changed

+183
-30
lines changed

.babelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@
22
"presets": [
33
"@babel/preset-env",
44
"@babel/preset-typescript"
5+
],
6+
"plugins": [
7+
"@babel/plugin-proposal-optional-chaining"
58
]
69
}

docs/with-keypress.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,16 @@ const MyButton = () => {
3333
};
3434
```
3535

36+
Although this library doesn't export handlers for specific keys, you could use this method to build out your own suite of key event handlers:
37+
38+
```js
39+
// /utils/keyboard.js
40+
const withEscapeKeyPress = withKeyPress('Escape');
41+
const withEnterKeyPress = withKeyPress('Enter');
42+
const withTabKeyPress = withKeyPress('Tab');
43+
const withShiftKeyPress = withKeyPress('Shift');
44+
// ...etc
45+
```
46+
47+
3648
[![Edit with-keypress (React)](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/with-keypress-react-9344c?fontsize=14&hidenavigation=1&theme=dark)

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)

docs/with-target-value.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
### withTargetValue
22

3-
Calls your provided event handler function with `event.target.value` as the first value (if it exists).
3+
Calls your provided event handler function with `event.target.value` as the first value (if it exists). Passes the `event` along as the second argument in case you need it.
44

55
**Vanilla**
66

@@ -22,14 +22,12 @@ myInputNode.addEventListener(
2222
import React, { Component } from "react";
2323
import { withTargetValue } from "browser-event-utils";
2424

25-
class MyInput extends Component {
26-
handleChange = withTargetValue((value, event) => {
27-
console.log(`Value changed! ${value}`);
28-
});
25+
const handleChange = withTargetValue((value, event) => {
26+
console.log(`Value changed! ${value}`);
27+
});
2928

30-
render() {
31-
return <input onChange={this.handleChange} />;
32-
}
29+
function MyInput() {
30+
return <input type="text" onChange={handleChange} />;
3331
}
3432
```
3533

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
},
4040
"devDependencies": {
4141
"@babel/core": "^7.7.7",
42+
"@babel/plugin-proposal-optional-chaining": "^7.7.5",
4243
"@babel/preset-env": "^7.7.7",
4344
"@babel/preset-typescript": "^7.7.7",
4445
"@types/jest": "^24.0.25",

rollup.config.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ const pkg = require('./package.json');
1010
const plugins = [
1111
json(),
1212
typescript({
13-
useTsconfigDeclarationDir: true
13+
useTsconfigDeclarationDir: true,
14+
objectHashIgnoreUnknownHack: true,
15+
clean: true,
1416
}),
1517
commonjs(),
1618
resolve(),
@@ -25,7 +27,7 @@ export default [
2527
file: pkg.browser,
2628
name: 'browserEventUtils',
2729
format: 'umd',
28-
sourcemap: true
30+
sourcemap: true,
2931
},
3032
],
3133
plugins: [
@@ -39,12 +41,12 @@ export default [
3941
{
4042
file: pkg.module,
4143
format: 'es',
42-
sourcemap: true
44+
sourcemap: true,
4345
},
4446
{
4547
file: pkg.main,
4648
format: 'commonjs',
47-
sourcemap: true
49+
sourcemap: true,
4850
}
4951
],
5052
plugins,
@@ -59,4 +61,4 @@ export default [
5961
// watch: {
6062
// include: 'src/**',
6163
// },
62-
// };
64+
// };
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+
});
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.target, mockEvent);
14+
});
15+
});

src/helpers/call-if-exists.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
import { EventHandler } from '../index.d';
1+
import { EventType, EventHandler } from '../index.d';
22

3+
export type TCallIfExists = (method: string) => EventHandler;
34
/**
45
* Helper util which ensures that a given property actually exists, then calls it if it does
56
*
6-
* @param {String} method - the event method that you want to call
7+
* @param {string} method - the event method that you want to call
78
* @return {Function} event handler function
89
*/
9-
const callIfExists = (method: string): EventHandler => (event: KeyboardEvent | any) => {
10-
return !!(event && event[method]) && event[method]();
10+
const callIfExists: TCallIfExists = (method: string = ''): EventHandler => (event: EventType): void => {
11+
// TODO: replace with optional chaining (eg: `?.`)
12+
return !!(event && event[method]) && (event[method] as unknown as Function)();
1113
};
1214

1315
export default callIfExists;

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;

0 commit comments

Comments
 (0)