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

Parameter feedback - #6 Better value normalization #4327

Merged
merged 1 commit into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Before:

After:

Copy link
Member

Choose a reason for hiding this comment

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

I wonder if there is a legitimate use case for multiple spaces in a parameter 😬

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)}
Copy link
Member

Choose a reason for hiding this comment

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

👍

/>
);
}
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;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Before:
Screen Shot 2019-11-01 at 9 07 51

After:
Screen Shot 2019-11-01 at 9 07 25

const normalizedValue = toNumber(value);
return !isNaN(normalizedValue) ? normalizedValue : null;
return !isNaN(normalizedValue) ? normalizedValue : value;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Before:
Screen Shot 2019-11-01 at 9 04 05

After:
Screen Shot 2019-11-01 at 9 03 55

}
}

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;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Before:
Screen Shot 2019-11-01 at 9 05 58

After:
Screen Shot 2019-11-01 at 9 06 10

Copy link
Member

Choose a reason for hiding this comment

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

I didn't see a reason why someone would use a parameter only containing whitespaces, so this looks good to me 👍.

}

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();
});
});
});