From 69ed85a0e123d87e39a00f9672a578716b3a6bbe Mon Sep 17 00:00:00 2001 From: Ran Byron Date: Fri, 1 Nov 2019 08:36:28 +0200 Subject: [PATCH] Parameter input feedback - #6 Better value normalization --- client/app/components/ParameterValueInput.jsx | 10 ++++------ client/app/services/parameters/NumberParameter.js | 6 +++--- client/app/services/parameters/TextParameter.js | 9 ++++++++- .../services/parameters/tests/NumberParameter.test.js | 5 ----- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/client/app/components/ParameterValueInput.jsx b/client/app/components/ParameterValueInput.jsx index 7f28ad33a8..88aef8c41e 100644 --- a/client/app/components/ParameterValueInput.jsx +++ b/client/app/components/ParameterValueInput.jsx @@ -5,7 +5,7 @@ import Input from 'antd/lib/input'; import InputNumber from 'antd/lib/input-number'; import DateParameter from '@/components/dynamic-parameters/DateParameter'; import DateRangeParameter from '@/components/dynamic-parameters/DateRangeParameter'; -import { isEqual } from 'lodash'; +import { isEqual, trim } from 'lodash'; import { QueryBasedParameterInput } from './QueryBasedParameterInput'; import './ParameterValueInput.less'; @@ -59,7 +59,7 @@ class ParameterValueInput extends React.Component { } onSelect = (value) => { - const isDirty = !isEqual(value, this.props.value); + const isDirty = !isEqual(trim(value), trim(this.props.value)); this.setState({ value, isDirty }); this.props.onSelect(value, isDirty); } @@ -140,13 +140,11 @@ class ParameterValueInput extends React.Component { const { className } = this.props; const { value } = this.state; - const normalize = val => (isNaN(val) ? undefined : val); - return ( this.onSelect(normalize(val))} + value={value} + onChange={val => this.onSelect(val)} /> ); } diff --git a/client/app/services/parameters/NumberParameter.js b/client/app/services/parameters/NumberParameter.js index 997cdb4b29..17abe0cc65 100644 --- a/client/app/services/parameters/NumberParameter.js +++ b/client/app/services/parameters/NumberParameter.js @@ -1,4 +1,4 @@ -import { toNumber, isNull } from 'lodash'; +import { toNumber, trim } from 'lodash'; import { Parameter } from '.'; class NumberParameter extends Parameter { @@ -9,11 +9,11 @@ class NumberParameter extends Parameter { // eslint-disable-next-line class-methods-use-this normalizeValue(value) { - if (isNull(value)) { + if (!trim(value)) { return null; } const normalizedValue = toNumber(value); - return !isNaN(normalizedValue) ? normalizedValue : null; + return !isNaN(normalizedValue) ? normalizedValue : value; } } diff --git a/client/app/services/parameters/TextParameter.js b/client/app/services/parameters/TextParameter.js index 645adbc8e3..0488392e88 100644 --- a/client/app/services/parameters/TextParameter.js +++ b/client/app/services/parameters/TextParameter.js @@ -1,4 +1,4 @@ -import { toString, isEmpty } from 'lodash'; +import { toString, isEmpty, trim } from 'lodash'; import { Parameter } from '.'; class TextParameter extends Parameter { @@ -15,6 +15,13 @@ class TextParameter extends Parameter { } return normalizedValue; } + + getExecutionValue() { + if (!trim(this.value)) { + return null; + } + return this.value; + } } export default TextParameter; diff --git a/client/app/services/parameters/tests/NumberParameter.test.js b/client/app/services/parameters/tests/NumberParameter.test.js index 2a292ea880..9cce92ef28 100644 --- a/client/app/services/parameters/tests/NumberParameter.test.js +++ b/client/app/services/parameters/tests/NumberParameter.test.js @@ -17,10 +17,5 @@ describe('NumberParameter', () => { const normalizedValue = param.normalizeValue(42); expect(normalizedValue).toBe(42); }); - - test('returns null when not possible to convert to number', () => { - const normalizedValue = param.normalizeValue('notanumber'); - expect(normalizedValue).toBeNull(); - }); }); });