Skip to content

Commit

Permalink
React/input currency pass event to callback (#498)
Browse files Browse the repository at this point in the history
* pass event to InputCurrency callback function

* prevent NaN value using onKeyDown for InputCurrency

* add changelog

* reformat changelog to md

* retain sythentic events

* update changelog

* Update react/src/components/atoms/forms/InputCurrency/index.js
  • Loading branch information
clairesunstudio committed Mar 5, 2019
1 parent 65a558e commit 1103bc4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
4 changes: 4 additions & 0 deletions changelogs/DP-12890.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
___DESCRIPTION___
Fixed
Minor
- (React) [InputCurrency] DP-12890: Pass event type to callback and fix `NaN` value when defaultValue is null using up/down buttons. #495
21 changes: 13 additions & 8 deletions react/src/components/atoms/forms/InputCurrency/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const Currency = (props) => (
return number;
};
const handleChange = (e) => {
const { type } = e;
let stringValue;
if (typeof e.target.value !== 'string') {
stringValue = String(e.target.value);
Expand Down Expand Up @@ -70,11 +71,12 @@ const Currency = (props) => (
}
context.updateState(update, () => {
if (typeof props.onChange === 'function') {
props.onChange(numberValue, props.id);
props.onChange(numberValue, props.id, type);
}
});
};
const handleAdjust = (e, direction) => {
const { type } = e;
let stringValue;
if (typeof context.value !== 'string') {
stringValue = String(context.value);
Expand All @@ -94,7 +96,7 @@ const Currency = (props) => (
const { showError, errorMsg } = validNumber(newValue, props.min, props.max);
context.updateState({ showError, errorMsg, value: toCurrency(newValue, 2) }, () => {
if (typeof props.onChange === 'function') {
props.onChange(newValue, props.id);
props.onChange(newValue, props.id, type, direction);
}
});
}
Expand Down Expand Up @@ -144,29 +146,32 @@ const Currency = (props) => (
}
},
onKeyDown: (e) => {
const { type, key } = e;
let stringValue;
if (typeof context.value !== 'string') {
stringValue = String(context.value);
} else {
stringValue = context.value;
}
const numberValue = numbro.unformat(stringValue);
// default to 0 if defaultValue is NaN
const baseValue = numberValue || 0;
if (!Number.isNaN(numberValue) && stringValue.length > 0) {
let newValue;
if (e.key === 'ArrowDown') {
newValue = Number(Number.parseFloat(numberValue - props.step).toFixed(2));
if (key === 'ArrowDown') {
newValue = Number(Number.parseFloat(baseValue - props.step).toFixed(2));
const { showError, errorMsg } = validNumber(newValue, props.min, props.max);
context.updateState({ showError, errorMsg, value: toCurrency(newValue, 2) }, () => {
if (typeof props.onChange === 'function') {
props.onChange(newValue, props.id);
props.onChange(newValue, props.id, type, key);
}
});
} else if (e.key === 'ArrowUp') {
newValue = Number(Number.parseFloat(numberValue + props.step).toFixed(2));
} else if (key === 'ArrowUp') {
newValue = Number(Number.parseFloat(baseValue + props.step).toFixed(2));
const { showError, errorMsg } = validNumber(newValue, props.min, props.max);
context.updateState({ showError, errorMsg, value: toCurrency(newValue, 2) }, () => {
if (typeof props.onChange === 'function') {
props.onChange(newValue, props.id);
props.onChange(newValue, props.id, type, key);
}
});
}
Expand Down

0 comments on commit 1103bc4

Please sign in to comment.