Skip to content

Commit

Permalink
EuiSuperDatePicker handle invalid start and end property values (#1544)
Browse files Browse the repository at this point in the history
* EuiSuperDatePicker handle invalid start and end property values

* change log
  • Loading branch information
nreese authored Feb 7, 2019
1 parent 60af5b9 commit 08e333c
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

- 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))
- Fixed `EuiSuperDatePicker` crashing with invalid start and end prop values ([#1544](https://github.com/elastic/eui/pull/1544))
- Make TSLint issues be warnings, not errors, when running `src-docs` ([#1537](https://github.com/elastic/eui/pull/1537))

## [`6.10.0`](https://github.com/elastic/eui/tree/v6.10.0)
Expand Down
18 changes: 16 additions & 2 deletions src-docs/src/views/date_picker/super_date_picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ export default class extends Component {
}, this.startLoading);
}

onStartInputChange = e => {
this.setState({
start: e.target.value,
});
};

onEndInputChange = e => {
this.setState({
end: e.target.value,
});
};

startLoading = () => {
setTimeout(
this.stopLoading,
Expand Down Expand Up @@ -74,17 +86,19 @@ export default class extends Component {
<Fragment>
<EuiFormRow
label="start"
helpText="EuiSuperDatePicker should be resilient to invalid start values. Try to break it with unexpected values"
>
<EuiFieldText
readOnly
onChange={this.onStartInputChange}
value={this.state.start}
/>
</EuiFormRow>
<EuiFormRow
label="end"
helpText="EuiSuperDatePicker should be resilient to invalid end values. Try to break it with unexpected values"
>
<EuiFieldText
readOnly
onChange={this.onEndInputChange}
value={this.state.end}
/>
</EuiFormRow>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class EuiAbsoluteTab extends Component {
super(props);

const parsedValue = dateMath.parse(props.value, { roundUp: props.roundUp });
const valueAsMoment = parsedValue ? parsedValue : moment();
const valueAsMoment = parsedValue && parsedValue.isValid() ? parsedValue : moment();
this.state = {
valueAsMoment,
textInputValue: valueAsMoment.format(INPUT_DATE_FORMAT),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ export class EuiRelativeTab extends Component {

render() {
const isInvalid = this.state.count < 0;
const formatedValue = isInvalid ? '' : dateMath.parse(this.props.value).format(this.props.dateFormat);
const parsedValue = dateMath.parse(this.props.value);
const formatedValue = isInvalid || !parsedValue || !parsedValue.isValid()
? ''
: parsedValue.format(this.props.dateFormat);
return (
<EuiForm className="euiDatePopoverContent__padded">
<EuiFlexGroup gutterSize="s" responsive={false}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ export class EuiQuickSelect extends Component {
const startMoment = dateMath.parse(this.props.start);
const endMoment = dateMath.parse(this.props.end, { roundUp: true });
return {
min: startMoment ? startMoment : moment().subtract(15, 'minute'),
max: endMoment ? endMoment : moment(),
min: startMoment && startMoment.isValid() ? startMoment : moment().subtract(15, 'minute'),
max: endMoment && endMoment.isValid() ? endMoment : moment(),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function isRangeInvalid(start, end) {

const startMoment = dateMath.parse(start);
const endMoment = dateMath.parse(end, { roundUp: true });
if (!startMoment || !endMoment) {
if (!startMoment || !endMoment || !startMoment.isValid() || !endMoment.isValid()) {
return true;
}
if (startMoment.isAfter(endMoment)) {
Expand Down

0 comments on commit 08e333c

Please sign in to comment.