From 653a660146abd0a7fffd3d219839b7d01310a280 Mon Sep 17 00:00:00 2001 From: Nathan Reese Date: Wed, 6 Feb 2019 15:52:37 -0700 Subject: [PATCH] EuiSuperDatePicker - safely handle negative relative times (#1537) * EuiSuperDatePicker - safetly handle negative relative times * changelog * protect against undefined returns for other calls to dateMath.parse --- CHANGELOG.md | 1 + .../date_picker/super_date_picker/date_modes.js | 6 +++++- .../super_date_picker/date_popover/absolute_tab.js | 3 ++- .../super_date_picker/date_popover/relative_tab.js | 11 ++++++++--- .../quick_select_popover/quick_select.js | 6 ++++-- .../super_date_picker/super_date_picker.js | 3 +++ 6 files changed, 23 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 330d53f14a3..9625e0def84 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ **Bug fixes** - Fixed `EuiSearchBar.Query` match_all query string must be `*` ([#1521](https://github.com/elastic/eui/pull/1521)) +- Fixed `EuiSuperDatePicker` crashing with negative relative value ([#1537](https://github.com/elastic/eui/pull/1537)) ## [`6.10.0`](https://github.com/elastic/eui/tree/v6.10.0) diff --git a/src/components/date_picker/super_date_picker/date_modes.js b/src/components/date_picker/super_date_picker/date_modes.js index 5ab95e6b22a..c3a6b2fc38d 100644 --- a/src/components/date_picker/super_date_picker/date_modes.js +++ b/src/components/date_picker/super_date_picker/date_modes.js @@ -21,7 +21,11 @@ export function getDateMode(value) { } export function toAbsoluteString(value, roundUp) { - return dateMath.parse(value, { roundUp }).toISOString(); + const valueAsMoment = dateMath.parse(value, { roundUp }); + if (!valueAsMoment) { + return value; + } + return valueAsMoment.toISOString(); } diff --git a/src/components/date_picker/super_date_picker/date_popover/absolute_tab.js b/src/components/date_picker/super_date_picker/date_popover/absolute_tab.js index 352ca015852..2d76e942812 100644 --- a/src/components/date_picker/super_date_picker/date_popover/absolute_tab.js +++ b/src/components/date_picker/super_date_picker/date_popover/absolute_tab.js @@ -15,7 +15,8 @@ export class EuiAbsoluteTab extends Component { constructor(props) { super(props); - const valueAsMoment = dateMath.parse(props.value, { roundUp: props.roundUp }); + const parsedValue = dateMath.parse(props.value, { roundUp: props.roundUp }); + const valueAsMoment = parsedValue ? parsedValue : moment(); this.state = { valueAsMoment, textInputValue: valueAsMoment.format(INPUT_DATE_FORMAT), diff --git a/src/components/date_picker/super_date_picker/date_popover/relative_tab.js b/src/components/date_picker/super_date_picker/date_popover/relative_tab.js index 253587dd041..240bc105e43 100644 --- a/src/components/date_picker/super_date_picker/date_popover/relative_tab.js +++ b/src/components/date_picker/super_date_picker/date_popover/relative_tab.js @@ -47,23 +47,28 @@ export class EuiRelativeTab extends Component { }; handleChange = () => { - if (this.state.count === '') { + if (this.state.count === '' || this.state.count < 0) { return; } this.props.onChange(toRelativeStringFromParts(this.state)); } render() { - const formatedValue = dateMath.parse(this.props.value).format(this.props.dateFormat); + const isInvalid = this.state.count < 0; + const formatedValue = isInvalid ? '' : dateMath.parse(this.props.value).format(this.props.dateFormat); return ( - + = 0' : null} + > diff --git a/src/components/date_picker/super_date_picker/quick_select_popover/quick_select.js b/src/components/date_picker/super_date_picker/quick_select_popover/quick_select.js index 95462ff963e..f86cd73f748 100644 --- a/src/components/date_picker/super_date_picker/quick_select_popover/quick_select.js +++ b/src/components/date_picker/super_date_picker/quick_select_popover/quick_select.js @@ -80,9 +80,11 @@ export class EuiQuickSelect extends Component { } getBounds = () => { + const startMoment = dateMath.parse(this.props.start); + const endMoment = dateMath.parse(this.props.end, { roundUp: true }); return { - min: dateMath.parse(this.props.start), - max: dateMath.parse(this.props.end, { roundUp: true }), + min: startMoment ? startMoment : moment().subtract(15, 'minute'), + max: endMoment ? endMoment : moment(), }; } diff --git a/src/components/date_picker/super_date_picker/super_date_picker.js b/src/components/date_picker/super_date_picker/super_date_picker.js index 49d1db34470..6b987d6153c 100644 --- a/src/components/date_picker/super_date_picker/super_date_picker.js +++ b/src/components/date_picker/super_date_picker/super_date_picker.js @@ -23,6 +23,9 @@ function isRangeInvalid(start, end) { const startMoment = dateMath.parse(start); const endMoment = dateMath.parse(end, { roundUp: true }); + if (!startMoment || !endMoment) { + return true; + } if (startMoment.isAfter(endMoment)) { return true; }