mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 01:38:56 -04:00
[Lens] Handle invalid values gracefully for static value operation (#172198)
## Summary
Fixes #171959
I've extended valid static value check to 15 digits (which is the max
support by JS as implementing the [64-bit IEEE
574](https://en.wikipedia.org/wiki/Double-precision_floating-point_format)).
<img width="1224" alt="Screenshot 2023-11-30 at 10 23 13"
src="bf88c0c8
-9e51-4c8f-912d-abd82f292eda">
Note: an alternative approach would be to make it pass nonetheless and
trunc the numeric value at 15th digit.
### Checklist
Delete any items that are not applicable to this PR.
- [x] Any text added follows [EUI's writing
guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses
sentence case text and includes [i18n
support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)
- [ ] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
---------
Co-authored-by: Stratoula Kalafateli <efstratia.kalafateli@elastic.co>
This commit is contained in:
parent
5ad3eec222
commit
1f8c816901
1 changed files with 25 additions and 7 deletions
|
@ -66,8 +66,9 @@ export const staticValueOperation: OperationDefinition<
|
|||
},
|
||||
getErrorMessage(layer, columnId) {
|
||||
const column = layer.columns[columnId] as StaticValueIndexPatternColumn;
|
||||
const isValid = isValidNumber(column.params.value, false, undefined, undefined, 15);
|
||||
|
||||
return column.params.value != null && !isValidNumber(column.params.value)
|
||||
return column.params.value != null && !isValid
|
||||
? [
|
||||
i18n.translate('xpack.lens.indexPattern.staticValueError', {
|
||||
defaultMessage: 'The static value of {value} is not a valid number',
|
||||
|
@ -89,7 +90,8 @@ export const staticValueOperation: OperationDefinition<
|
|||
const params = currentColumn.params;
|
||||
// TODO: improve this logic
|
||||
const useDisplayLabel = currentColumn.label !== defaultLabel;
|
||||
const label = isValidNumber(params.value)
|
||||
const isValid = isValidNumber(params.value, false, undefined, undefined, 15);
|
||||
const label = isValid
|
||||
? useDisplayLabel
|
||||
? currentColumn.label
|
||||
: params?.value ?? defaultLabel
|
||||
|
@ -98,11 +100,11 @@ export const staticValueOperation: OperationDefinition<
|
|||
return [
|
||||
{
|
||||
type: 'function',
|
||||
function: isValidNumber(params.value) ? 'mathColumn' : 'mapColumn',
|
||||
function: isValid ? 'mathColumn' : 'mapColumn',
|
||||
arguments: {
|
||||
id: [columnId],
|
||||
name: [label || defaultLabel],
|
||||
expression: [String(isValidNumber(params.value) ? params.value! : defaultValue)],
|
||||
expression: [String(isValid ? params.value! : defaultValue)],
|
||||
},
|
||||
},
|
||||
];
|
||||
|
@ -163,7 +165,10 @@ export const staticValueOperation: OperationDefinition<
|
|||
const onChange = useCallback(
|
||||
(newValue) => {
|
||||
// even if debounced it's triggering for empty string with the previous valid value
|
||||
if (currentColumn.params.value === newValue) {
|
||||
if (
|
||||
currentColumn.params.value === newValue ||
|
||||
!isValidNumber(newValue, false, undefined, undefined, 15)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
// Because of upstream specific UX flows, we need fresh layer state here
|
||||
|
@ -209,13 +214,26 @@ export const staticValueOperation: OperationDefinition<
|
|||
const onChangeHandler = useCallback(
|
||||
(e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const value = e.currentTarget.value;
|
||||
handleInputChange(isValidNumber(value) ? value : undefined);
|
||||
handleInputChange(value);
|
||||
},
|
||||
[handleInputChange]
|
||||
);
|
||||
|
||||
const inputValueIsValid = isValidNumber(inputValue, false, undefined, undefined, 15);
|
||||
|
||||
return (
|
||||
<EuiFormRow label={paramEditorCustomProps?.labels?.[0] || defaultLabel} fullWidth>
|
||||
<EuiFormRow
|
||||
label={paramEditorCustomProps?.labels?.[0] || defaultLabel}
|
||||
fullWidth
|
||||
isInvalid={!inputValueIsValid}
|
||||
error={
|
||||
!inputValueIsValid &&
|
||||
i18n.translate('xpack.lens.indexPattern.staticValueError', {
|
||||
defaultMessage: 'The static value of {value} is not a valid number',
|
||||
values: { value: inputValue ?? "''" },
|
||||
})
|
||||
}
|
||||
>
|
||||
<EuiFieldNumber
|
||||
fullWidth
|
||||
data-test-subj="lns-indexPattern-static_value-input"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue