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] add showRefreshOnly prop #1412

Merged
merged 5 commits into from
Jan 8, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
59 changes: 43 additions & 16 deletions src-docs/src/views/date_picker/super_date_picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default class extends Component {
recentlyUsedRanges: [],
isLoading: false,
showUpdateButton: true,
showRefreshOnly: false,
start: 'now-30m',
end: 'now',
}
Expand Down Expand Up @@ -60,6 +61,39 @@ export default class extends Component {
}));
}

toggleShowRefreshOnly = () => {
this.setState(prevState => ({
showRefreshOnly: !prevState.showRefreshOnly,
}));
}

renderTimeRange = () => {
if (this.state.showRefreshOnly) {
return null;
}

return (
<Fragment>
<EuiFormRow
label="start"
>
<EuiFieldText
readOnly
value={this.state.start}
/>
</EuiFormRow>
<EuiFormRow
label="end"
>
<EuiFieldText
readOnly
value={this.state.end}
/>
</EuiFormRow>
</Fragment>
);
}

render() {
return (
<Fragment>
Expand All @@ -68,6 +102,11 @@ export default class extends Component {
onChange={this.toggleShowApplyButton}
checked={this.state.showUpdateButton}
/>
<EuiSwitch
label="Show refresh only"
onChange={this.toggleShowRefreshOnly}
checked={this.state.showRefreshOnly}
/>
<EuiSpacer />

<EuiSuperDatePicker
Expand All @@ -80,24 +119,12 @@ export default class extends Component {
onRefreshChange={this.onRefreshChange}
recentlyUsedRanges={this.state.recentlyUsedRanges}
showUpdateButton={this.state.showUpdateButton}
showRefreshOnly={this.state.showRefreshOnly}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This prop name is confusing because I think that's it's referring to the Update/Refresh button. Can you rename to isAutoRefreshOnly?

/>

<EuiFormRow
label="start"
>
<EuiFieldText
readOnly
value={this.state.start}
/>
</EuiFormRow>
<EuiFormRow
label="end"
>
<EuiFieldText
readOnly
value={this.state.end}
/>
</EuiFormRow>
<EuiSpacer />

{this.renderTimeRange()}
</Fragment>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ exports[`EuiSuperDatePicker is rendered 1`] = `
isPaused={true}
recentlyUsedRanges={Array []}
refreshInterval={0}
showRefreshOnly={false}
start="now-15m"
/>
}
Expand Down Expand Up @@ -207,6 +208,7 @@ exports[`EuiSuperDatePicker isLoading 1`] = `
isPaused={true}
recentlyUsedRanges={Array []}
refreshInterval={0}
showRefreshOnly={false}
start="now-15m"
/>
}
Expand Down
27 changes: 27 additions & 0 deletions src/components/date_picker/super_date_picker/pretty_interval.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

const MS_IN_SECOND = 1000;
const MS_IN_MINUTE = 60 * MS_IN_SECOND;
const MS_IN_HOUR = 60 * MS_IN_MINUTE;
const MS_IN_DAY = 24 * MS_IN_HOUR;

export function prettyInterval(isPaused, intervalInMs) {
if (isPaused || intervalInMs === 0) {
return 'Off';
} else if (intervalInMs < MS_IN_MINUTE) {
const intervalInSeconds = Math.round(intervalInMs / MS_IN_SECOND);
const units = intervalInSeconds > 1 ? 'seconds' : 'second';
return `${intervalInSeconds} ${units}`;
} else if (intervalInMs < MS_IN_HOUR) {
const intervalInMinutes = Math.round(intervalInMs / MS_IN_MINUTE);
const units = intervalInMinutes > 1 ? 'minutes' : 'minute';
return `${intervalInMinutes} ${units}`;
} else if (intervalInMs < MS_IN_DAY) {
const intervalInHours = Math.round(intervalInMs / MS_IN_HOUR);
const units = intervalInHours > 1 ? 'hours' : 'hour';
return `${intervalInHours} ${units}`;
}

const intervalInDays = Math.round(intervalInMs / MS_IN_DAY);
const units = intervalInDays > 1 ? 'days' : 'day';
return `${intervalInDays} ${units}`;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

import { prettyInterval } from './pretty_interval';

const IS_NOT_PAUSED = false;
const IS_PAUSED = true;

test('Off', () => {
expect(prettyInterval(IS_NOT_PAUSED, 0)).toBe('Off');
expect(prettyInterval(IS_PAUSED, 1000)).toBe('Off');
});

test('seconds', () => {
expect(prettyInterval(IS_NOT_PAUSED, 1000)).toBe('1 second');
expect(prettyInterval(IS_NOT_PAUSED, 15000)).toBe('15 seconds');
});

test('minutes', () => {
expect(prettyInterval(IS_NOT_PAUSED, 60000)).toBe('1 minute');
expect(prettyInterval(IS_NOT_PAUSED, 1800000)).toBe('30 minutes');
});

test('hours', () => {
expect(prettyInterval(IS_NOT_PAUSED, 3600000)).toBe('1 hour');
expect(prettyInterval(IS_NOT_PAUSED, 43200000)).toBe('12 hours');
});

test('days', () => {
expect(prettyInterval(IS_NOT_PAUSED, 86400000)).toBe('1 day');
expect(prettyInterval(IS_NOT_PAUSED, 86400000 * 2)).toBe('2 days');
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`EuiSuperDatePicker is rendered 1`] = `
exports[`EuiQuickSelect is rendered 1`] = `
<React.Fragment>
<EuiFlexGroup
alignItems="center"
Expand Down Expand Up @@ -212,7 +212,7 @@ exports[`EuiSuperDatePicker is rendered 1`] = `
</React.Fragment>
`;

exports[`EuiSuperDatePicker prevQuickSelect 1`] = `
exports[`EuiQuickSelect prevQuickSelect 1`] = `
<React.Fragment>
<EuiFlexGroup
alignItems="center"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`EuiQuickSelectPopover is rendered 1`] = `
<EuiPopover
anchorPosition="downLeft"
button={
<EuiButtonEmpty
aria-label="Date quick select"
className="euiFormControlLayout__prepend"
color="primary"
data-test-subj="superDatePickerToggleQuickMenuButton"
iconSide="right"
iconType="arrowDown"
onClick={[Function]}
size="xs"
textProps={
Object {
"className": "euiQuickSelectPopover__buttonText",
}
}
type="button"
>
<EuiIcon
size="m"
type="calendar"
/>
</EuiButtonEmpty>
}
closePopover={[Function]}
hasArrow={true}
id="QuickSelectPopover"
isOpen={false}
ownFocus={true}
panelPaddingSize="m"
>
<div
className="euiQuickSelectPopover__content"
data-test-subj="superDatePickerQuickMenu"
>
<React.Fragment>
<EuiQuickSelect
applyTime={[Function]}
end="now"
prevQuickSelect={Object {}}
start="now-15m"
/>
<EuiCommonlyUsedTimeRanges
applyTime={[Function]}
commonlyUsedRanges={
Array [
Object {
"end": "now/d",
"label": "Today",
"start": "now/d",
},
]
}
/>
<EuiRecentlyUsed
applyTime={[Function]}
commonlyUsedRanges={
Array [
Object {
"end": "now/d",
"label": "Today",
"start": "now/d",
},
]
}
dateFormat="MMM D, YYYY @ HH:mm:ss.SSS"
recentlyUsedRanges={
Array [
Object {
"end": "now/d",
"label": "Today",
"start": "now/d",
},
]
}
/>
</React.Fragment>
<EuiRefreshInterval
applyRefreshInterval={[Function]}
isPaused={true}
refreshInterval={0}
/>
</div>
</EuiPopover>
`;

exports[`EuiQuickSelectPopover showRefreshOnly 1`] = `
<EuiPopover
anchorPosition="downLeft"
button={
<EuiButtonEmpty
aria-label="Date quick select"
className="euiFormControlLayout__prepend"
color="primary"
data-test-subj="superDatePickerToggleQuickMenuButton"
iconSide="right"
iconType="arrowDown"
onClick={[Function]}
size="xs"
textProps={
Object {
"className": "euiQuickSelectPopover__buttonText",
}
}
type="button"
>
<EuiIcon
size="m"
type="calendar"
/>
</EuiButtonEmpty>
}
closePopover={[Function]}
hasArrow={true}
id="QuickSelectPopover"
isOpen={false}
ownFocus={true}
panelPaddingSize="m"
>
<div
className="euiQuickSelectPopover__content"
data-test-subj="superDatePickerQuickMenu"
>
<EuiRefreshInterval
applyRefreshInterval={[Function]}
isPaused={true}
refreshInterval={0}
/>
</div>
</EuiPopover>
`;
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const defaultProps = {
start: 'now-15m',
end: 'now',
};
describe('EuiSuperDatePicker', () => {
describe('EuiQuickSelect', () => {
test('is rendered', () => {
const component = shallow(
<EuiQuickSelect
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

import PropTypes from 'prop-types';
import React, { Component } from 'react';
import React, { Component, Fragment } from 'react';
import { commonlyUsedRangeShape, recentlyUsedRangeShape } from '../types';

import {
Expand Down Expand Up @@ -49,6 +49,33 @@ export class EuiQuickSelectPopover extends Component {
}
}

renderDateTimeSections = () => {
if (this.props.showRefreshOnly) {
return null;
}

return (
<Fragment>
<EuiQuickSelect
applyTime={this.applyTime}
start={this.props.start}
end={this.props.end}
prevQuickSelect={this.state.prevQuickSelect}
/>
<EuiCommonlyUsedTimeRanges
applyTime={this.applyTime}
commonlyUsedRanges={this.props.commonlyUsedRanges}
/>
<EuiRecentlyUsed
applyTime={this.applyTime}
commonlyUsedRanges={this.props.commonlyUsedRanges}
dateFormat={this.props.dateFormat}
recentlyUsedRanges={this.props.recentlyUsedRanges}
/>
</Fragment>
);
}

render() {
const quickSelectButton = (
<EuiButtonEmpty
Expand Down Expand Up @@ -78,22 +105,7 @@ export class EuiQuickSelectPopover extends Component {
className="euiQuickSelectPopover__content"
data-test-subj="superDatePickerQuickMenu"
>
<EuiQuickSelect
applyTime={this.applyTime}
start={this.props.start}
end={this.props.end}
prevQuickSelect={this.state.prevQuickSelect}
/>
<EuiCommonlyUsedTimeRanges
applyTime={this.applyTime}
commonlyUsedRanges={this.props.commonlyUsedRanges}
/>
<EuiRecentlyUsed
applyTime={this.applyTime}
commonlyUsedRanges={this.props.commonlyUsedRanges}
dateFormat={this.props.dateFormat}
recentlyUsedRanges={this.props.recentlyUsedRanges}
/>
{this.renderDateTimeSections()}
<EuiRefreshInterval
applyRefreshInterval={this.props.applyRefreshInterval}
isPaused={this.props.isPaused}
Expand All @@ -115,4 +127,5 @@ EuiQuickSelectPopover.propTypes = {
commonlyUsedRanges: PropTypes.arrayOf(commonlyUsedRangeShape).isRequired,
dateFormat: PropTypes.string.isRequired,
recentlyUsedRanges: PropTypes.arrayOf(recentlyUsedRangeShape).isRequired,
showRefreshOnly: PropTypes.bool.isRequired,
};
Loading