From 75df12f92d2a8a2cdc792a1c9244e911be3843b2 Mon Sep 17 00:00:00 2001 From: Minghua Sun Date: Tue, 1 Sep 2020 19:08:47 -0400 Subject: [PATCH] Allow value 0 to be returned in onBlur callback (#1161) * allow value 0 to be returned in onBlur callback * add changelog * Update react/src/components/forms/InputCurrency/index.js Co-authored-by: Steven Murray * address comment Co-authored-by: Steven Murray --- changelogs/react-fix-inputCurrency-bug.yml | 5 +++++ react/src/components/forms/InputCurrency/index.js | 12 ++++++------ 2 files changed, 11 insertions(+), 6 deletions(-) create mode 100644 changelogs/react-fix-inputCurrency-bug.yml diff --git a/changelogs/react-fix-inputCurrency-bug.yml b/changelogs/react-fix-inputCurrency-bug.yml new file mode 100644 index 0000000000..2847466115 --- /dev/null +++ b/changelogs/react-fix-inputCurrency-bug.yml @@ -0,0 +1,5 @@ +Added: + - project: React + component: InputCurrency + description: Fix onBlur bug to allow value 0 to be returned in onBlur callback. (#1161) + impact: Patch diff --git a/react/src/components/forms/InputCurrency/index.js b/react/src/components/forms/InputCurrency/index.js index 247b657308..2f17767eeb 100644 --- a/react/src/components/forms/InputCurrency/index.js +++ b/react/src/components/forms/InputCurrency/index.js @@ -140,15 +140,15 @@ const Currency = (props) => { const handleBlur = (e) => { const { type } = e; const inputEl = ref.current; - const stringValue = inputEl.value; - // isNotNumber returns true if stringValue is null, undefined or 'NaN' + const value = inputEl && inputEl.value; + const numberValue = Number(numbro.unformat(value)); + // isNotNumber returns true if value is null, undefined or NaN vs Number.isNaN only checks if value is NaN /* eslint-disable-next-line no-restricted-globals */ - const isNotNumber = !stringValue || isNaN(Number(numbro.unformat(stringValue))); + const isNotNumber = isNaN(value); if (isNotNumber) { inputEl.setAttribute('placeholder', props.placeholder); - } - let newValue = isNotNumber ? '' : Number(numbro.unformat(stringValue)); - if (!is.empty(newValue)) { + } else { + let newValue = numberValue; if (hasNumberProperty(props, 'max') && newValue > props.max) { newValue = props.max; }