Skip to content

Commit

Permalink
EuiSuperDatePicker - add customQuickSelectPanels prop (#1549)
Browse files Browse the repository at this point in the history
* custom quick select panel

* change log
  • Loading branch information
nreese authored Feb 11, 2019
1 parent 7b7b626 commit ea94c0c
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Added pseudo-localization mode to docs ([#1541](https://github.com/elastic/eui/pull/1541))
- New docs page listing localization tokens ([#1541](https://github.com/elastic/eui/pull/1541))
- Added support for OR group clauses in `EuiQuery` and `EuiSearchBar` ([#1204](https://github.com/elastic/eui/pull/1204))
- Added `customQuickSelectPanels` prop to `EuiSuperDatePicker` ([#1549](https://github.com/elastic/eui/pull/1549))

**Bug fixes**

Expand Down
39 changes: 39 additions & 0 deletions src-docs/src/views/date_picker/super_date_picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,30 @@ import {
EuiSpacer,
EuiFormRow,
EuiFieldText,
EuiLink,
} from '../../../../src/components';

function MyCustomQuickSelectPanel({ applyTime }) {

function applyMyCustomTime() {
applyTime({ start: 'now-30d', end: 'now+7d' });
}

return (
<EuiLink onClick={applyMyCustomTime}>
entire dataset timerange
</EuiLink>
);
}

export default class extends Component {

state = {
recentlyUsedRanges: [],
isLoading: false,
showUpdateButton: true,
isAutoRefreshOnly: false,
showCustomQuickSelectPanel: false,
start: 'now-30m',
end: 'now',
}
Expand Down Expand Up @@ -77,6 +92,12 @@ export default class extends Component {
}));
}

toggleShowCustomQuickSelectPanel = () => {
this.setState(prevState => ({
showCustomQuickSelectPanel: !prevState.showCustomQuickSelectPanel,
}));
}

renderTimeRange = () => {
if (this.state.isAutoRefreshOnly) {
return null;
Expand Down Expand Up @@ -107,6 +128,15 @@ export default class extends Component {
}

render() {
let customQuickSelectPanels;
if (this.state.showCustomQuickSelectPanel) {
customQuickSelectPanels = [
{
title: 'My custom panel',
content: (<MyCustomQuickSelectPanel/>),
}
];
}
return (
<Fragment>
<EuiSwitch
Expand All @@ -123,6 +153,14 @@ export default class extends Component {
onChange={this.toggleShowRefreshOnly}
checked={this.state.isAutoRefreshOnly}
/>

&emsp;

<EuiSwitch
label="Show custom quick select panel"
onChange={this.toggleShowCustomQuickSelectPanel}
checked={this.state.showCustomQuickSelectPanel}
/>
<EuiSpacer />

<EuiSuperDatePicker
Expand All @@ -136,6 +174,7 @@ export default class extends Component {
recentlyUsedRanges={this.state.recentlyUsedRanges}
showUpdateButton={this.state.showUpdateButton}
isAutoRefreshOnly={this.state.isAutoRefreshOnly}
customQuickSelectPanels={customQuickSelectPanels}
/>

<EuiSpacer />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@

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

import {
EuiButtonEmpty,
} from '../../../button';

import {
EuiIcon,
} from '../../../icon';

import {
EuiPopover,
} from '../../../popover';
import { EuiButtonEmpty } from '../../../button';
import { EuiIcon } from '../../../icon';
import { EuiPopover } from '../../../popover';
import { EuiTitle } from '../../../title';
import { EuiSpacer } from '../../../spacer';
import { EuiHorizontalRule } from '../../../horizontal_rule';
import { EuiText } from '../../../text';

import { EuiQuickSelect } from './quick_select';
import { EuiCommonlyUsedTimeRanges } from './commonly_used_time_ranges';
Expand Down Expand Up @@ -72,10 +68,30 @@ export class EuiQuickSelectPopover extends Component {
dateFormat={this.props.dateFormat}
recentlyUsedRanges={this.props.recentlyUsedRanges}
/>
{this.renderCustomQuickSelectPanels()}
</Fragment>
);
}

renderCustomQuickSelectPanels = () => {
if (!this.props.customQuickSelectPanels) {
return null;
}

return this.props.customQuickSelectPanels.map(({ title, content }) => {
return (
<Fragment key={title}>
<EuiTitle size="xxxs"><span>{title}</span></EuiTitle>
<EuiSpacer size="s" />
<EuiText size="s" className="euiQuickSelectPopover__section">
{React.cloneElement(content, { applyTime: this.applyTime })}
</EuiText>
<EuiHorizontalRule margin="s" />
</Fragment>
);
});
}

render() {
const quickSelectButton = (
<EuiButtonEmpty
Expand Down Expand Up @@ -128,4 +144,6 @@ EuiQuickSelectPopover.propTypes = {
dateFormat: PropTypes.string.isRequired,
recentlyUsedRanges: PropTypes.arrayOf(recentlyUsedRangeShape).isRequired,
isAutoRefreshOnly: PropTypes.bool.isRequired,
isAutoRefreshOnly: PropTypes.bool.isRequired,
customQuickSelectPanels: PropTypes.arrayOf(quickSelectPanelShape),
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import classNames from 'classnames';
import { commonlyUsedRangeShape, recentlyUsedRangeShape } from './types';
import { commonlyUsedRangeShape, recentlyUsedRangeShape, quickSelectPanelShape } from './types';
import { prettyDuration, showPrettyDuration } from './pretty_duration';
import { prettyInterval } from './pretty_interval';

Expand Down Expand Up @@ -81,6 +81,7 @@ export class EuiSuperDatePicker extends Component {
* Set isAutoRefreshOnly to true to limit the component to only display auto refresh content.
*/
isAutoRefreshOnly: PropTypes.bool,
customQuickSelectPanels: PropTypes.arrayOf(quickSelectPanelShape),
}

static defaultProps = {
Expand Down Expand Up @@ -335,6 +336,7 @@ export class EuiSuperDatePicker extends Component {
dateFormat={this.props.dateFormat}
recentlyUsedRanges={this.props.recentlyUsedRanges}
isAutoRefreshOnly={this.props.isAutoRefreshOnly}
customQuickSelectPanels={this.props.customQuickSelectPanels}
/>
);

Expand Down
5 changes: 5 additions & 0 deletions src/components/date_picker/super_date_picker/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ export const recentlyUsedRangeShape = PropTypes.shape({
start: PropTypes.string.isRequired,
end: PropTypes.string.isRequired,
});

export const quickSelectPanelShape = PropTypes.shape({
title: PropTypes.string.isRequired,
content: PropTypes.node.isRequired,
});

0 comments on commit ea94c0c

Please sign in to comment.