Skip to content

Commit

Permalink
Parameter input feedback - #6 Better value normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
ranbena committed Nov 1, 2019
1 parent 1c92bb2 commit 69ed85a
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
10 changes: 4 additions & 6 deletions client/app/components/ParameterValueInput.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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 (
<InputNumber
className={className}
value={normalize(value)}
onChange={val => this.onSelect(normalize(val))}
value={value}
onChange={val => this.onSelect(val)}
/>
);
}
Expand Down
6 changes: 3 additions & 3 deletions client/app/services/parameters/NumberParameter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { toNumber, isNull } from 'lodash';
import { toNumber, trim } from 'lodash';
import { Parameter } from '.';

class NumberParameter extends Parameter {
Expand All @@ -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;
}
}

Expand Down
9 changes: 8 additions & 1 deletion client/app/services/parameters/TextParameter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { toString, isEmpty } from 'lodash';
import { toString, isEmpty, trim } from 'lodash';
import { Parameter } from '.';

class TextParameter extends Parameter {
Expand All @@ -15,6 +15,13 @@ class TextParameter extends Parameter {
}
return normalizedValue;
}

getExecutionValue() {
if (!trim(this.value)) {
return null;
}
return this.value;
}
}

export default TextParameter;
5 changes: 0 additions & 5 deletions client/app/services/parameters/tests/NumberParameter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});

0 comments on commit 69ed85a

Please sign in to comment.