-
Notifications
You must be signed in to change notification settings - Fork 841
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] add showApplyButton prop #1399
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9a2bc96
[EuiSuperDatePicker] add showApplyButton prop
nreese 646801b
use getDerivedStateFromProps instead of key so component does not get…
nreese a611182
update changelog
nreese c2626aa
rename showApplyButton to showUpdateButton, move fields in example to…
nreese 4073859
update changelog
nreese 0f2ac96
merge with master
nreese 6dfd559
merge with master (again)
nreese File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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,15 +1,22 @@ | ||
|
||
import React, { Component } from 'react'; | ||
import React, { Component, Fragment } from 'react'; | ||
|
||
import { | ||
EuiSuperDatePicker, | ||
EuiSwitch, | ||
EuiSpacer, | ||
EuiFormRow, | ||
EuiFieldText, | ||
} from '../../../../src/components'; | ||
|
||
export default class extends Component { | ||
|
||
state = { | ||
recentlyUsedRanges: [], | ||
isLoading: false, | ||
showApplyButton: true, | ||
start: 'now-30m', | ||
end: 'now', | ||
} | ||
|
||
onTimeChange = ({ start, end }) => { | ||
|
@@ -47,18 +54,50 @@ export default class extends Component { | |
}); | ||
} | ||
|
||
toggleShowApplyButton = () => { | ||
this.setState(prevState => ({ | ||
showApplyButton: !prevState.showApplyButton, | ||
})); | ||
} | ||
|
||
render() { | ||
return ( | ||
<EuiSuperDatePicker | ||
isLoading={this.state.isLoading} | ||
start={this.state.start} | ||
end={this.state.end} | ||
onTimeChange={this.onTimeChange} | ||
isPaused={this.state.isPaused} | ||
refreshInterval={this.state.refreshInterval} | ||
onRefreshChange={this.onRefreshChange} | ||
recentlyUsedRanges={this.state.recentlyUsedRanges} | ||
/> | ||
<Fragment> | ||
<EuiFormRow | ||
label="start time" | ||
> | ||
<EuiFieldText | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you move these inputs to below the actual component and make them |
||
disabled | ||
value={this.state.start} | ||
/> | ||
</EuiFormRow> | ||
<EuiFormRow | ||
label="end time" | ||
> | ||
<EuiFieldText | ||
disabled | ||
value={this.state.end} | ||
/> | ||
</EuiFormRow> | ||
<EuiSwitch | ||
label="Show apply button" | ||
onChange={this.toggleShowApplyButton} | ||
checked={this.state.showApplyButton} | ||
/> | ||
<EuiSpacer /> | ||
|
||
<EuiSuperDatePicker | ||
isLoading={this.state.isLoading} | ||
start={this.state.start} | ||
end={this.state.end} | ||
onTimeChange={this.onTimeChange} | ||
isPaused={this.state.isPaused} | ||
refreshInterval={this.state.refreshInterval} | ||
onRefreshChange={this.onRefreshChange} | ||
recentlyUsedRanges={this.state.recentlyUsedRanges} | ||
showApplyButton={this.state.showApplyButton} | ||
/> | ||
</Fragment> | ||
); | ||
} | ||
} |
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,5 +1,3 @@ | ||
import { | ||
WrappedEuiSuperDatePicker, | ||
export { | ||
EuiSuperDatePicker, | ||
} from './super_date_picker'; | ||
|
||
export { WrappedEuiSuperDatePicker as EuiSuperDatePicker }; |
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 |
---|---|---|
|
@@ -15,79 +15,89 @@ import { EuiButton } from '../../button'; | |
import { EuiFlexGroup, EuiFlexItem } from '../../flex'; | ||
import { EuiToolTip } from '../../tool_tip'; | ||
|
||
// EuiSuperDatePicker has state that needs to be reset when start or end change. | ||
// Instead of using getDerivedStateFromProps, this wrapper adds a key to the component. | ||
// When a key changes, React will create a new component instance rather than update the current one | ||
// https://reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html#recommendation-fully-uncontrolled-component-with-a-key | ||
export function WrappedEuiSuperDatePicker(props) { | ||
return ( | ||
<EuiSuperDatePicker | ||
key={`${props.start}-${props.end}`} | ||
{...props} | ||
/> | ||
); | ||
} | ||
export class EuiSuperDatePicker extends Component { | ||
|
||
WrappedEuiSuperDatePicker.propTypes = { | ||
isLoading: PropTypes.bool, | ||
/** | ||
* String as either datemath (e.g.: now, now-15m, now-15m/m) or | ||
* absolute date in the format 'YYYY-MM-DDTHH:mm:ss.sssZ' | ||
*/ | ||
start: PropTypes.string, | ||
/** | ||
* String as either datemath (e.g.: now, now-15m, now-15m/m) or | ||
* absolute date in the format 'YYYY-MM-DDTHH:mm:ss.sssZ' | ||
*/ | ||
end: PropTypes.string, | ||
/** | ||
* Callback for when the time changes. Called with { start, end } | ||
*/ | ||
onTimeChange: PropTypes.func.isRequired, | ||
isPaused: PropTypes.bool, | ||
/** | ||
* Refresh interval in milliseconds | ||
*/ | ||
refreshInterval: PropTypes.number, | ||
/** | ||
* Callback for when the refresh interval changes. Called with { isPaused, refreshInterval } | ||
* Supply onRefreshChange to show refresh interval inputs in quick select popover | ||
*/ | ||
onRefreshChange: PropTypes.func, | ||
|
||
/** | ||
* 'start' and 'end' must be string as either datemath (e.g.: now, now-15m, now-15m/m) or | ||
* absolute date in the format 'YYYY-MM-DDTHH:mm:ss.sssZ' | ||
*/ | ||
commonlyUsedRanges: PropTypes.arrayOf(commonlyUsedRangeShape), | ||
dateFormat: PropTypes.string, | ||
/** | ||
* 'start' and 'end' must be string as either datemath (e.g.: now, now-15m, now-15m/m) or | ||
* absolute date in the format 'YYYY-MM-DDTHH:mm:ss.sssZ' | ||
*/ | ||
recentlyUsedRanges: PropTypes.arrayOf(recentlyUsedRangeShape), | ||
}; | ||
|
||
WrappedEuiSuperDatePicker.defaultProps = { | ||
start: 'now-15m', | ||
end: 'now', | ||
isPaused: true, | ||
refreshInterval: 0, | ||
commonlyUsedRanges: [ | ||
{ start: 'now/d', end: 'now/d', label: 'Today' }, | ||
{ start: 'now-1d/d', end: 'now-1d/d', label: 'Yesterday' }, | ||
{ start: 'now/w', end: 'now/w', label: 'This week' }, | ||
{ start: 'now/w', end: 'now', label: 'Week to date' }, | ||
{ start: 'now/M', end: 'now/M', label: 'This month' }, | ||
{ start: 'now/M', end: 'now', label: 'Month to date' }, | ||
{ start: 'now/y', end: 'now/y', label: 'This year' }, | ||
{ start: 'now/y', end: 'now', label: 'Year to date' }, | ||
], | ||
dateFormat: 'MMM D, YYYY @ HH:mm:ss.SSS', | ||
recentlyUsedRanges: [], | ||
}; | ||
static propTypes = { | ||
isLoading: PropTypes.bool, | ||
/** | ||
* String as either datemath (e.g.: now, now-15m, now-15m/m) or | ||
* absolute date in the format 'YYYY-MM-DDTHH:mm:ss.sssZ' | ||
*/ | ||
start: PropTypes.string, | ||
/** | ||
* String as either datemath (e.g.: now, now-15m, now-15m/m) or | ||
* absolute date in the format 'YYYY-MM-DDTHH:mm:ss.sssZ' | ||
*/ | ||
end: PropTypes.string, | ||
/** | ||
* Callback for when the time changes. Called with { start, end } | ||
*/ | ||
onTimeChange: PropTypes.func.isRequired, | ||
isPaused: PropTypes.bool, | ||
/** | ||
* Refresh interval in milliseconds | ||
*/ | ||
refreshInterval: PropTypes.number, | ||
/** | ||
* Callback for when the refresh interval changes. Called with { isPaused, refreshInterval } | ||
* Supply onRefreshChange to show refresh interval inputs in quick select popover | ||
*/ | ||
onRefreshChange: PropTypes.func, | ||
|
||
/** | ||
* 'start' and 'end' must be string as either datemath (e.g.: now, now-15m, now-15m/m) or | ||
* absolute date in the format 'YYYY-MM-DDTHH:mm:ss.sssZ' | ||
*/ | ||
commonlyUsedRanges: PropTypes.arrayOf(commonlyUsedRangeShape), | ||
dateFormat: PropTypes.string, | ||
/** | ||
* 'start' and 'end' must be string as either datemath (e.g.: now, now-15m, now-15m/m) or | ||
* absolute date in the format 'YYYY-MM-DDTHH:mm:ss.sssZ' | ||
*/ | ||
recentlyUsedRanges: PropTypes.arrayOf(recentlyUsedRangeShape), | ||
/** | ||
* Set showApplyButton to false to immediately invoke onTimeChange for all start and end changes. | ||
*/ | ||
showApplyButton: PropTypes.bool, | ||
} | ||
|
||
export class EuiSuperDatePicker extends Component { | ||
static defaultProps = { | ||
start: 'now-15m', | ||
end: 'now', | ||
isPaused: true, | ||
refreshInterval: 0, | ||
commonlyUsedRanges: [ | ||
{ start: 'now/d', end: 'now/d', label: 'Today' }, | ||
{ start: 'now-1d/d', end: 'now-1d/d', label: 'Yesterday' }, | ||
{ start: 'now/w', end: 'now/w', label: 'This week' }, | ||
{ start: 'now/w', end: 'now', label: 'Week to date' }, | ||
{ start: 'now/M', end: 'now/M', label: 'This month' }, | ||
{ start: 'now/M', end: 'now', label: 'Month to date' }, | ||
{ start: 'now/y', end: 'now/y', label: 'This year' }, | ||
{ start: 'now/y', end: 'now', label: 'Year to date' }, | ||
], | ||
dateFormat: 'MMM D, YYYY @ HH:mm:ss.SSS', | ||
recentlyUsedRanges: [], | ||
showApplyButton: true, | ||
} | ||
|
||
static getDerivedStateFromProps(nextProps, prevState) { | ||
if (nextProps.start !== prevState.prevProps.start | ||
|| nextProps.end !== prevState.prevProps.end) { | ||
return { | ||
prevProps: { | ||
start: nextProps.start, | ||
end: nextProps.end, | ||
}, | ||
start: nextProps.start, | ||
end: nextProps.end, | ||
isInvalid: false, | ||
hasChanged: false, | ||
}; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
constructor(props) { | ||
super(props); | ||
|
@@ -99,6 +109,10 @@ export class EuiSuperDatePicker extends Component { | |
} = this.props; | ||
|
||
this.state = { | ||
prevProps: { | ||
start: props.start, | ||
end: props.end, | ||
}, | ||
start, | ||
end, | ||
isInvalid: false, | ||
|
@@ -118,13 +132,13 @@ export class EuiSuperDatePicker extends Component { | |
setTootipRef = node => (this.tooltip = node); | ||
|
||
showTooltip = () => { | ||
if (!this._isMounted) { | ||
if (!this._isMounted || !this.tooltip) { | ||
return; | ||
} | ||
this.tooltip.showToolTip(); | ||
} | ||
hideTooltip = () => { | ||
if (!this._isMounted) { | ||
if (!this._isMounted || !this.tooltip) { | ||
return; | ||
} | ||
this.tooltip.hideToolTip(); | ||
|
@@ -149,6 +163,11 @@ export class EuiSuperDatePicker extends Component { | |
}); | ||
|
||
if (!isInvalid) { | ||
if (!this.props.showApplyButton) { | ||
this.props.onTimeChange({ start, end }); | ||
return; | ||
} | ||
|
||
this.showTooltip(); | ||
this.tooltipTimeout = setTimeout(() => { | ||
this.hideTooltip(); | ||
|
@@ -169,6 +188,9 @@ export class EuiSuperDatePicker extends Component { | |
} | ||
|
||
applyQuickTime = ({ start, end }) => { | ||
this.setState({ | ||
showPrettyDuration: showPrettyDuration(start, end, this.props.commonlyUsedRanges), | ||
}); | ||
this.props.onTimeChange({ start, end }); | ||
} | ||
|
||
|
@@ -236,6 +258,10 @@ export class EuiSuperDatePicker extends Component { | |
} | ||
|
||
renderUpdateButton = () => { | ||
if (!this.props.showApplyButton) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
return; | ||
} | ||
|
||
let buttonText = 'Refresh'; | ||
if (this.state.hasChanged || this.props.isLoading) { | ||
buttonText = this.props.isLoading ? 'Updating' : 'Update'; | ||
|
@@ -305,7 +331,3 @@ export class EuiSuperDatePicker extends Component { | |
); | ||
} | ||
} | ||
|
||
EuiSuperDatePicker.propTypes = WrappedEuiSuperDatePicker.propTypes; | ||
EuiSuperDatePicker.defaultProps = WrappedEuiSuperDatePicker.defaultProps; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changelog needs updating too