-
Notifications
You must be signed in to change notification settings - Fork 842
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EuiSuperDatePicker move EuiDatePopoverButton isOpen state into EuiSup…
…erDatePicker (#1494) * move isOpen state to EuiSuperDatePicker * change log
- Loading branch information
Showing
3 changed files
with
106 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 74 additions & 86 deletions
160
src/components/date_picker/super_date_picker/date_popover/date_popover_button.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters