Skip to content

Commit

Permalink
Revamp EuiSuperDatePicker docs (#2641)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreadelrio authored Dec 13, 2019
1 parent fbe66d5 commit 0cd2dae
Show file tree
Hide file tree
Showing 6 changed files with 427 additions and 160 deletions.
3 changes: 3 additions & 0 deletions src-docs/src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ import { StepsExample } from './views/steps/steps_example';

import { SuggestExample } from './views/suggest/suggest_example';

import { SuperDatePickerExample } from './views/super_date_picker/super_date_picker_example';

import { TableExample } from './views/tables/tables_example';

import { TabsExample } from './views/tabs/tabs_example';
Expand Down Expand Up @@ -379,6 +381,7 @@ const navigation = [
SearchBarExample,
SelectableExample,
SuggestExample,
SuperDatePickerExample,
].map(example => createExample(example)),
},
{
Expand Down
83 changes: 0 additions & 83 deletions src-docs/src/views/date_picker/date_picker_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@ import { GuideSectionTypes } from '../../components';

import {
EuiCode,
EuiCodeBlock,
EuiLink,
EuiDatePicker,
EuiDatePickerRange,
EuiSuperDatePicker,
EuiSuperUpdateButton,
} from '../../../../src/components';

import DatePicker from './date_picker';
Expand Down Expand Up @@ -58,10 +55,6 @@ import Utc from './utc';
const utcSource = require('!!raw-loader!./utc');
const utcHtml = renderToHtml(Utc);

import SuperDatePicker from './super_date_picker';
const superDatePickerSource = require('!!raw-loader!./super_date_picker');
const superDatePickerHtml = renderToHtml(SuperDatePicker);

export const DatePickerExample = {
title: 'Date Picker',
sections: [
Expand Down Expand Up @@ -324,81 +317,5 @@ export const DatePickerExample = {
),
demo: <Classes />,
},
{
title: 'Super date picker',
source: [
{
type: GuideSectionTypes.JS,
code: superDatePickerSource,
},
{
type: GuideSectionTypes.HTML,
code: superDatePickerHtml,
},
],
text: (
<div>
<p>
<EuiCode>start</EuiCode> and <EuiCode>end</EuiCode> date times are
passed as strings in either datemath format (e.g.: now, now-15m,
now-15m/m) or as absolute date in the format{' '}
<EuiCode>YYYY-MM-DDTHH:mm:ss.SSSZ</EuiCode>. Use{' '}
<EuiLink href="https://github.com/elastic/datemath-js">
datemath
</EuiLink>{' '}
to convert start and end strings into moment objects.
</p>
<EuiCodeBlock paddingSize="none" isCopyable>
{`
import dateMath from '@elastic/datemath';
const startMoment = dateMath.parse(start);
// dateMath.parse is inconsistent with unparsable strings.
// Sometimes undefined is returned, other times an invalid moment is returned
if (!startMoment || !startMoment.isValid()) {
throw new Error('Unable to parse start string');
}
// Pass roundUp when parsing end string
const endMoment = dateMath.parse(end, { roundUp: true });
if (!endMoment || !endMoment.isValid()) {
throw new Error('Unable to parse end string');
}
`}
</EuiCodeBlock>
<p>
<EuiCode>onTimeChange</EuiCode> will be immediately invoked when{' '}
<EuiCode>start</EuiCode> and <EuiCode>end</EuiCode> change from
interactions with <strong> Quick select</strong>,{' '}
<strong>Commonly used</strong>, or{' '}
<strong>Recently used date ranges</strong> since these interactions
set both <EuiCode>start</EuiCode> and <EuiCode>end</EuiCode> in a
single event.
</p>
<p>
<EuiCode>onTimeChange</EuiCode> will <strong>not</strong> be invoked
when
<EuiCode>start</EuiCode> and <EuiCode>end</EuiCode> change from
interactions with <strong>Absolute</strong>,{' '}
<strong>Relative</strong>, and <strong>Now</strong> tabs.{' '}
<EuiCode>onTimeChange</EuiCode> will be invoked when the user clicks
the <strong>Update</strong> button. This gives users the ability to
set both <EuiCode>start</EuiCode> and <EuiCode>end</EuiCode> before
triggering <EuiCode>onTimeChange</EuiCode>. Set{' '}
<EuiCode>showUpdateButton</EuiCode> to <EuiCode>false</EuiCode> to
immediately invoke <EuiCode>onTimeChange</EuiCode> for all{' '}
<EuiCode>start</EuiCode> and <EuiCode>end</EuiCode> changes.
</p>
<p>
Set <EuiCode>isAutoRefreshOnly</EuiCode> to <EuiCode>true </EuiCode>{' '}
to limit the component to only display auto refresh content. This is
useful in cases where there is no time data but auto-refresh
configuration is still desired.
</p>
</div>
),
demo: <SuperDatePicker />,
props: { EuiSuperDatePicker, EuiSuperUpdateButton },
},
],
};
131 changes: 131 additions & 0 deletions src-docs/src/views/super_date_picker/super_date_picker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import React, { Component, Fragment } from 'react';

import {
EuiSuperDatePicker,
EuiSpacer,
EuiFormControlLayoutDelimited,
EuiFormLabel,
EuiPanel,
EuiText,
} from '../../../../src/components';

export default class extends Component {
state = {
recentlyUsedRanges: [],
isLoading: false,
start: 'now-30m',
end: 'now',
};

onTimeChange = ({ start, end }) => {
this.setState(prevState => {
const recentlyUsedRanges = prevState.recentlyUsedRanges.filter(
recentlyUsedRange => {
const isDuplicate =
recentlyUsedRange.start === start && recentlyUsedRange.end === end;
return !isDuplicate;
}
);
recentlyUsedRanges.unshift({ start, end });
return {
start,
end,
recentlyUsedRanges:
recentlyUsedRanges.length > 10
? recentlyUsedRanges.slice(0, 9)
: recentlyUsedRanges,
isLoading: true,
};
}, this.startLoading);
};

onRefresh = ({ start, end, refreshInterval }) => {
return new Promise(resolve => {
setTimeout(resolve, 100);
}).then(() => {
console.log(start, end, refreshInterval);
});
};

onStartInputChange = e => {
this.setState({
start: e.target.value,
});
};

onEndInputChange = e => {
this.setState({
end: e.target.value,
});
};

startLoading = () => {
setTimeout(this.stopLoading, 1000);
};

stopLoading = () => {
this.setState({ isLoading: false });
};

onRefreshChange = ({ isPaused, refreshInterval }) => {
this.setState({
isPaused,
refreshInterval,
});
};

renderTimeRange = () => {
return (
<Fragment>
<EuiPanel paddingSize="m">
<EuiText size="s">
EuiSuperDatePicker should be resilient to invalid date values. You
can try to break it with unexpected values here.
</EuiText>
<EuiSpacer />
<EuiFormControlLayoutDelimited
prepend={<EuiFormLabel>Dates</EuiFormLabel>}
startControl={
<input
onChange={this.onStartInputChange}
type="text"
value={this.state.start}
placeholder="start"
className="euiFieldText"
/>
}
endControl={
<input
onChange={this.onEndInputChange}
type="text"
placeholder="end"
value={this.state.end}
className="euiFieldText"
/>
}
/>
</EuiPanel>
</Fragment>
);
};

render() {
return (
<Fragment>
<EuiSuperDatePicker
isLoading={this.state.isLoading}
start={this.state.start}
end={this.state.end}
onTimeChange={this.onTimeChange}
onRefresh={this.onRefresh}
isPaused={this.state.isPaused}
refreshInterval={this.state.refreshInterval}
onRefreshChange={this.onRefreshChange}
recentlyUsedRanges={this.state.recentlyUsedRanges}
/>
<EuiSpacer />
{this.renderTimeRange()}
</Fragment>
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,15 @@ import {
EuiSuperDatePicker,
EuiSwitch,
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: [],
isDisabled: false,
isLoading: false,
showUpdateButton: true,
isAutoRefreshOnly: false,
showCustomQuickSelectPanel: false,
start: 'now-30m',
end: 'now',
};
Expand Down Expand Up @@ -61,18 +47,6 @@ export default class extends Component {
});
};

onStartInputChange = e => {
this.setState({
start: e.target.value,
});
};

onEndInputChange = e => {
this.setState({
end: e.target.value,
});
};

startLoading = () => {
setTimeout(this.stopLoading, 1000);
};
Expand Down Expand Up @@ -106,53 +80,11 @@ export default class extends Component {
}));
};

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

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

return (
<Fragment>
<EuiFormRow
label="start"
helpText="EuiSuperDatePicker should be resilient to invalid start values. Try to break it with unexpected values">
<EuiFieldText
onChange={this.onStartInputChange}
value={this.state.start}
/>
</EuiFormRow>
<EuiFormRow
label="end"
helpText="EuiSuperDatePicker should be resilient to invalid end values. Try to break it with unexpected values">
<EuiFieldText
onChange={this.onEndInputChange}
value={this.state.end}
/>
</EuiFormRow>
</Fragment>
);
};

render() {
let customQuickSelectPanels;
if (this.state.showCustomQuickSelectPanel) {
customQuickSelectPanels = [
{
title: 'My custom panel',
content: <MyCustomQuickSelectPanel />,
},
];
}
return (
<Fragment>
<EuiSwitch
label="Show apply button"
label="Show update button"
onChange={this.toggleShowApplyButton}
checked={!this.state.isAutoRefreshOnly && this.state.showUpdateButton}
disabled={this.state.isAutoRefreshOnly}
Expand All @@ -164,12 +96,6 @@ export default class extends Component {
checked={this.state.isAutoRefreshOnly}
/>
&emsp;
<EuiSwitch
label="Show custom quick select panel"
onChange={this.toggleShowCustomQuickSelectPanel}
checked={this.state.showCustomQuickSelectPanel}
/>
&emsp;
<EuiSwitch
label="Is disabled"
onChange={this.toggleDisabled}
Expand All @@ -189,10 +115,8 @@ export default class extends Component {
recentlyUsedRanges={this.state.recentlyUsedRanges}
showUpdateButton={this.state.showUpdateButton}
isAutoRefreshOnly={this.state.isAutoRefreshOnly}
customQuickSelectPanels={customQuickSelectPanels}
/>
<EuiSpacer />
{this.renderTimeRange()}
</Fragment>
);
}
Expand Down
Loading

0 comments on commit 0cd2dae

Please sign in to comment.