Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React/input currency pass event to callback #498

Merged
merged 7 commits into from
Mar 5, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
props.onChange(newValue, props.id, type);
props.onChange(newValue, props.id, type, key);

clairesunstudio marked this conversation as resolved.
Show resolved Hide resolved
}
});
} 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