Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from /issues/29
Add withNumericTargetValue Closes #29
- Loading branch information
Showing
6 changed files
with
96 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
### withNumericTargetValue | ||
|
||
Calls your provided event handler function with `event.target.value`, cast to a Number, as the first value (if it exists). Passes the `event` along as the second argument in case you need it. | ||
|
||
**Vanilla** | ||
|
||
```js | ||
import { withNumericTargetValue } from "browser-event-utils"; | ||
|
||
myInputNode.addEventListener( | ||
"change", | ||
withNumericTargetValue((value, event) => { | ||
// Note that you still get the event object as the 2nd arg, if you need it | ||
console.log(value); // 0 -> Always a Number, never NaN | ||
}) | ||
); | ||
``` | ||
|
||
**React** | ||
|
||
```jsx | ||
import React, { Component } from "react"; | ||
import { withNumericTargetValue } from "browser-event-utils"; | ||
|
||
const handleChange = withNumericTargetValue((value, event) => { | ||
// Note that you still get the event object as the 2nd arg, if you need it | ||
console.log(value); // 0 -> Always a Number, never NaN | ||
}); | ||
|
||
const MyInput = () => { | ||
return <input type="number" onChange={handleChange} /> | ||
}; | ||
``` | ||
|
||
<!-- | ||
[![Edit with-numeric-target-value (React)](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/competent-breeze-kmhf6?fontsize=14&hidenavigation=1) | ||
--> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import withNumericTargetValue from '../with-numeric-target-value'; | ||
import { EventType } from '../index.d'; | ||
|
||
|
||
describe('withNumericTargetValue', () => { | ||
it('will not fall over if bad values are passed', () => { | ||
expect(() => withNumericTargetValue()()).not.toThrow(); | ||
}); | ||
it('calls the hanlder with a correct numeric value', () => { | ||
const handler = jest.fn(); | ||
const mockEvent = { target: { value: '100' } } as EventType; | ||
withNumericTargetValue(handler)(mockEvent); | ||
expect(handler).toHaveBeenCalledWith(100, mockEvent); | ||
}); | ||
it('casts non-numeric nullish values properly', () => { | ||
const handler = jest.fn(); | ||
const mockEvent = { target: { value: null } } as EventType; | ||
withNumericTargetValue(handler)(mockEvent); | ||
expect(handler).toHaveBeenCalledWith(0, mockEvent); | ||
}); | ||
it('will never pass a NaN through as the numeric value', () => { | ||
const handler = jest.fn(); | ||
const mockEvent = { target: { } } as EventType; | ||
withNumericTargetValue(handler)(mockEvent); | ||
expect(handler).not.toHaveBeenCalledWith(NaN, mockEvent); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import withTarget from './helpers/with-target'; | ||
import noop from './helpers/noop'; | ||
import { | ||
EventType, | ||
EventHandler, | ||
} from './index.d'; | ||
|
||
|
||
export type TWithNumericTargetValueHandler = | ||
(value: number | null, event?: EventType) => void; | ||
|
||
/** | ||
* Accepts a function, who's first argument will be Number(event.target.value), | ||
* followed by the full event object. | ||
* | ||
* @param {Function} fn - Consumer's handler function | ||
* @return {Function} - event handler function | ||
*/ | ||
const withNumericTargetValue = (fn: TWithNumericTargetValueHandler = noop): EventHandler => { | ||
return withTarget((target?: HTMLInputElement, event?: EventType): void => { | ||
const numericValue = Number(target?.value); | ||
return fn(!Number.isNaN(numericValue) ? numericValue : 0, event); | ||
}); | ||
}; | ||
|
||
export default withNumericTargetValue; |