Skip to content

Commit

Permalink
dont allow empty values in any condition for slider
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasneirynck committed Mar 13, 2019
1 parent ab10914 commit ba5e18e
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 18 deletions.
22 changes: 12 additions & 10 deletions src/legacy/ui/public/validated_range/is_range_valid.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ import { i18n } from '@kbn/i18n';
const LOWER_VALUE_INDEX = 0;
const UPPER_VALUE_INDEX = 1;

export function isRangeValid(value, min, max) {
export function isRangeValid(value, min, max, allowEmptyRange) {
allowEmptyRange = typeof allowEmptyRange === 'boolean' ? allowEmptyRange : true;//cannot use default props since that uses falsy check
let lowerValue = isNaN(value[LOWER_VALUE_INDEX]) ? '' : value[LOWER_VALUE_INDEX];
let upperValue = isNaN(value[UPPER_VALUE_INDEX]) ? '' : value[UPPER_VALUE_INDEX];

const isLowerValueValid = lowerValue !== '';
const isUpperValueValid = upperValue !== '';
const isLowerValueValid = lowerValue.toString() !== '';
const isUpperValueValid = upperValue.toString() !== '';
if (isLowerValueValid) {
lowerValue = parseFloat(lowerValue);
}
Expand All @@ -37,22 +38,23 @@ export function isRangeValid(value, min, max) {
let isValid = true;
let errorMessage = '';

if ((!isLowerValueValid && isUpperValueValid) || (isLowerValueValid && !isUpperValueValid)) {
if (!allowEmptyRange && (!isLowerValueValid || !isUpperValueValid)) {
isValid = false;
errorMessage = i18n.translate('common.ui.dualRangeControl.bothMustNotBeEmptyWhenNotAllowed', {
defaultMessage: 'Neither lower and upper values can be empty'
});
} else if ((!isLowerValueValid && isUpperValueValid) || (isLowerValueValid && !isUpperValueValid)) {
isValid = false;
errorMessage = i18n.translate('common.ui.dualRangeControl.mustSetBothErrorMessage', {
defaultMessage: 'Both lower and upper values must be set'
});
}

if ((isLowerValueValid && lowerValue < min) || (isUpperValueValid && upperValue > max)) {
} else if ((isLowerValueValid && lowerValue < min) || (isUpperValueValid && upperValue > max)) {
isValid = false;
errorMessage = i18n.translate('common.ui.dualRangeControl.outsideOfRangeErrorMessage', {
defaultMessage: 'Values must be on or between {min} and {max}',
values: { min, max }
});
}

if (isLowerValueValid && isUpperValueValid && upperValue < lowerValue) {
} else if (isLowerValueValid && isUpperValueValid && upperValue < lowerValue) {
isValid = false;
errorMessage = i18n.translate('common.ui.dualRangeControl.upperValidErrorMessage', {
defaultMessage: 'Upper value must be greater or equal to lower value'
Expand Down
5 changes: 5 additions & 0 deletions src/legacy/ui/public/validated_range/is_range_valid.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ it('Should return true when lower and upper values are not set (empty range)', (
expect(isValid).toBe(true);
});

it('Should return false when lower and upper values are not set (empty range) and empty ranges are not allowed', () => {
const { isValid } = isRangeValid(['', ''], 1, 10, false);
expect(isValid).toBe(false);
});

it('Should return false when lower value is not set and upper value is set', () => {
const { isValid, errorMessage } = isRangeValid(['', 3], 1, 10);
expect(isValid).toBe(false);
Expand Down
22 changes: 17 additions & 5 deletions src/legacy/ui/public/validated_range/validated_dual_range.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { isRangeValid } from './is_range_valid';

import {
Expand All @@ -35,20 +36,22 @@ export class ValidatedDualRange extends Component {
const { isValid, errorMessage } = isRangeValid(
nextProps.value,
nextProps.min,
nextProps.max);
nextProps.max,
nextProps.allowEmptyRange
);
return {
value: nextProps.value,
prevValue: nextProps.value,
isValid,
errorMessage,
errorMessage
};
}

return null;
}

onChange = (value) => {
const { isValid, errorMessage } = isRangeValid(value, this.props.min, this.props.max);
_onChange = (value) => {
const { isValid, errorMessage } = isRangeValid(value, this.props.min, this.props.max, this.props.allowEmptyRange);

this.setState({
value,
Expand All @@ -68,17 +71,26 @@ export class ValidatedDualRange extends Component {
...rest

This comment has been minimized.

Copy link
@nreese

nreese Mar 13, 2019

Contributor

Need to pull 'allowEmptyRange' out so it is not included in rest

} = this.props;


return (
<EuiFormRow
isInvalid={!this.state.isValid}
error={this.state.errorMessage ? [this.state.errorMessage] : []}
>
<EuiDualRange
value={this.state.value}
onChange={this.onChange}
onChange={this._onChange}
{...rest}
/>
</EuiFormRow>
);
}
}

ValidatedDualRange.propTypes = {
allowEmptyRange: PropTypes.bool,
};

ValidatedDualRange.defaultProps = {
allowEmptyRange: true
};
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ export function SettingsPanel(props) {
props.updateLabel(props.layerId, label);
};

const onZoomChange = (event) => {
const minZoom = Math.max(MIN_ZOOM, parseInt(event[0], 10));
const onZoomChange = (value) => {
const minZoom = Math.max(MIN_ZOOM, parseInt(value[0], 10));
props.updateMinZoom(props.layerId, minZoom);
const maxZoom = Math.min(MAX_ZOOM, parseInt(event[1], 10));
const maxZoom = Math.min(MAX_ZOOM, parseInt(value[1], 10));
props.updateMaxZoom(props.layerId, maxZoom);
};

Expand Down Expand Up @@ -95,6 +95,7 @@ export function SettingsPanel(props) {
showInput
showRange
onChange={onZoomChange}
allowEmptyRange={false}
/>
</EuiFormRow>
</EuiFlexItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export function SizeRangeSelector({ minSize, maxSize, onChange }) {
showInput
showRange
onChange={onSizeChange}
allowEmptyRange={false}
/>
</EuiFormRow>
</EuiFlexItem>
Expand Down

0 comments on commit ba5e18e

Please sign in to comment.