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

EuiSuperDatePicker move EuiDatePopoverButton isOpen state into EuiSuperDatePicker #1494

Merged
merged 2 commits into from
Jan 29, 2019
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
**Bug fixes**

- Fixed popover & tooltip positioning to properly account for arrow buffer ([#1490](https://github.com/elastic/eui/pull/1490))
- Fixed `EuiSuperDatePicker` unexpectedly closing start and end date popovers ([#1494](https://github.com/elastic/eui/pull/1494))

## [`6.7.3`](https://github.com/elastic/eui/tree/v6.7.3)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,100 +1,88 @@
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import React from 'react';
import classNames from 'classnames';

import { EuiPopover } from '../../../popover';

import { formatTimeString } from '../pretty_duration';
import { EuiDatePopoverContent } from './date_popover_content';

export class EuiDatePopoverButton extends Component {
static propTypes = {
position: PropTypes.oneOf(['start', 'end']),
isInvalid: PropTypes.bool,
needsUpdating: PropTypes.bool,
value: PropTypes.string.isRequired,
onChange: PropTypes.func.isRequired,
dateFormat: PropTypes.string.isRequired,
roundUp: PropTypes.bool,
}

state = {
isOpen: false,
}
export function EuiDatePopoverButton(props) {
const {
position,
isInvalid,
needsUpdating,
value,
buttonProps,
roundUp,
onChange,
dateFormat,
isOpen,
onPopoverToggle,
onPopoverClose,
...rest
} = props;

togglePopover = () => {
this.setState((prevState) => {
return { isOpen: !prevState.isOpen };
});
}
const classes = classNames([
'euiDatePopoverButton',
`euiDatePopoverButton--${position}`,
{
'euiDatePopoverButton-isSelected': isOpen,
'euiDatePopoverButton-isInvalid': isInvalid,
'euiDatePopoverButton-needsUpdating': needsUpdating
}
]);

closePopover = () => {
this.setState({
isOpen: false,
});
let title = value;
if (isInvalid) {
title = `Invalid date: ${title}`;
} else if (needsUpdating) {
title = `Update needed: ${title}`;
}

render() {
const {
position,
isInvalid,
needsUpdating,
value,
buttonProps,
roundUp,
onChange,
dateFormat,
...rest
} = this.props;

const classes = classNames([
'euiDatePopoverButton',
`euiDatePopoverButton--${position}`,
{
'euiDatePopoverButton-isSelected': this.state.isOpen,
'euiDatePopoverButton-isInvalid': isInvalid,
'euiDatePopoverButton-needsUpdating': needsUpdating
}
]);
const button = (
<button
onClick={onPopoverToggle}
className={classes}
title={title}
data-test-subj={`superDatePicker${position}DatePopoverButton`}
{...buttonProps}
>
{formatTimeString(value, dateFormat, roundUp)}
</button>
);

let title = value;
if (isInvalid) {
title = `Invalid date: ${title}`;
} else if (needsUpdating) {
title = `Update needed: ${title}`;
}

const button = (
<button
onClick={this.togglePopover}
className={classes}
title={title}
data-test-subj={`superDatePicker${this.props.position}DatePopoverButton`}
{...buttonProps}
>
{formatTimeString(value, dateFormat, roundUp)}
</button>
);

return (
<EuiPopover
className="euiDatePopoverButton__popover"
button={button}
isOpen={this.state.isOpen}
closePopover={this.closePopover}
anchorPosition={this.props.position === 'start' ? 'downLeft' : 'downRight'}
anchorClassName="euiDatePopoverButton__popoverAnchor"
panelPaddingSize="none"
ownFocus
{...rest}
>
<EuiDatePopoverContent
value={value}
roundUp={roundUp}
onChange={onChange}
dateFormat={dateFormat}
/>
</EuiPopover>
);
}
return (
<EuiPopover
className="euiDatePopoverButton__popover"
button={button}
isOpen={isOpen}
closePopover={onPopoverClose}
anchorPosition={position === 'start' ? 'downLeft' : 'downRight'}
anchorClassName="euiDatePopoverButton__popoverAnchor"
panelPaddingSize="none"
ownFocus
{...rest}
>
<EuiDatePopoverContent
value={value}
roundUp={roundUp}
onChange={onChange}
dateFormat={dateFormat}
/>
</EuiPopover>
);
}

EuiDatePopoverButton.propTypes = {
position: PropTypes.oneOf(['start', 'end']),
isInvalid: PropTypes.bool,
needsUpdating: PropTypes.bool,
value: PropTypes.string.isRequired,
onChange: PropTypes.func.isRequired,
dateFormat: PropTypes.string.isRequired,
roundUp: PropTypes.bool,
isOpen: PropTypes.bool.isRequired,
onPopoverToggle: PropTypes.func.isRequired,
onPopoverClose: PropTypes.func.isRequired,
};
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ export class EuiSuperDatePicker extends Component {
isInvalid: isRangeInvalid(start, end),
hasChanged: false,
showPrettyDuration: showPrettyDuration(start, end, commonlyUsedRanges),
isStartDatePopoverOpen: false,
isEndDatePopoverOpen: false,
};
}

Expand Down Expand Up @@ -195,6 +197,26 @@ export class EuiSuperDatePicker extends Component {
this.setState({ showPrettyDuration: false });
}

onStartDatePopoverToggle = () => {
this.setState(prevState => {
return { isStartDatePopoverOpen: !prevState.isStartDatePopoverOpen };
});
}

onStartDatePopoverClose = () => {
this.setState({ isStartDatePopoverOpen: false });
}

onEndDatePopoverToggle = () => {
this.setState(prevState => {
return { isEndDatePopoverOpen: !prevState.isEndDatePopoverOpen };
});
}

onEndDatePopoverClose = () => {
this.setState({ isEndDatePopoverOpen: false });
}

renderDatePickerRange = () => {
const {
start,
Expand All @@ -220,7 +242,9 @@ export class EuiSuperDatePicker extends Component {
);
}

if (this.state.showPrettyDuration) {
if (this.state.showPrettyDuration
&& !this.state.isStartDatePopoverOpen
&& !this.state.isEndDatePopoverOpen) {
return (
<EuiDatePickerRange
className="euiDatePickerRange--inGroup"
Expand Down Expand Up @@ -254,6 +278,9 @@ export class EuiSuperDatePicker extends Component {
onChange={this.setStart}
value={start}
dateFormat={this.props.dateFormat}
isOpen={this.state.isStartDatePopoverOpen}
onPopoverToggle={this.onStartDatePopoverToggle}
onPopoverClose={this.onStartDatePopoverClose}
/>
}
endDateControl={
Expand All @@ -265,6 +292,9 @@ export class EuiSuperDatePicker extends Component {
value={end}
dateFormat={this.props.dateFormat}
roundUp
isOpen={this.state.isEndDatePopoverOpen}
onPopoverToggle={this.onEndDatePopoverToggle}
onPopoverClose={this.onEndDatePopoverClose}
/>
}
/>
Expand Down