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 customQuickSelectRender prop #6382

Merged
merged 16 commits into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import React, { useState, Fragment } from 'react';

import {
EuiSuperDatePicker,
EuiSpacer,
EuiLink,
OnTimeChangeProps,
OnRefreshProps,
ApplyTime,
EuiSuperDatePickerProps,
} from '../../../../src';

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

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

export default () => {
const [recentlyUsedRanges, setRecentlyUsedRanges] = useState<
NonNullable<EuiSuperDatePickerProps['recentlyUsedRanges']>
>([]);
const [isLoading, setIsLoading] = useState(false);
const [start, setStart] = useState('now-30m');
const [end, setEnd] = useState('now');

const onTimeChange = ({ start, end }: OnTimeChangeProps) => {
const recentlyUsedRange = recentlyUsedRanges.filter((recentlyUsedRange) => {
const isDuplicate =
recentlyUsedRange.start === start && recentlyUsedRange.end === end;
return !isDuplicate;
});
recentlyUsedRange.unshift({ start, end });
setStart(start);
setEnd(end);
setRecentlyUsedRanges(
recentlyUsedRange.length > 10
? recentlyUsedRange.slice(0, 9)
: recentlyUsedRange
);
setIsLoading(true);
startLoading();
};

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

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

const stopLoading = () => {
setIsLoading(false);
};

const customQuickSelectPanels = [
{
title: 'My custom panel',
content: <MyCustomQuickSelectPanel />,
},
];

return (
<Fragment>
<EuiSuperDatePicker
isLoading={isLoading}
start={start}
end={end}
onTimeChange={onTimeChange}
onRefresh={onRefresh}
recentlyUsedRanges={recentlyUsedRanges}
customQuickSelectPanels={customQuickSelectPanels}
customQuickSelectRender={({
quickSelect,
commonlyUsedTimes,
recentlyUsedTimes,
customQuickSelectPanels,
}) => (
<>
{customQuickSelectPanels}
{quickSelect}
{commonlyUsedTimes}
{recentlyUsedTimes}
breehall marked this conversation as resolved.
Show resolved Hide resolved
</>
)}
/>
<EuiSpacer />
</Fragment>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
EuiLink,
EuiText,
EuiSuperDatePicker,
EuiBasicTable,
} from '../../../../src/components';

import { EuiSuperUpdateButtonProps } from './props';
Expand All @@ -27,6 +28,9 @@ const superDatePickerWidthSource = require('!!raw-loader!./super_date_picker_wid
import SuperDatePickerCustomQuickSelect from './super_date_picker_custom_quick_select';
const superDatePickerCustomQuickSelectSource = require('!!raw-loader!./super_date_picker_custom_quick_select');

import SuperDatePickerCustomQuickSelectCustomOrder from './super_date_picker_custom_quick_select_custom_order';
const superDatePickerCustomQuickSelectCustomOrderSource = require('!!raw-loader!./super_date_picker_custom_quick_select_custom_order');

import AutoRefresh from './auto_refresh';
const autoRefreshSource = require('!!raw-loader!./auto_refresh');
import AutoRefreshOnly from './auto_refresh_only';
Expand Down Expand Up @@ -57,6 +61,69 @@ const superDatePickerCustomQuickSelectSnippet = `<EuiSuperDatePicker
/>
`;

const superDatePickerCustomQuickSelectCustomOrderSnippet = `<EuiSuperDatePicker
onTimeChange={onTimeChange}
recentlyUsedRanges={[
end: ShortDate,
start: ShortDate,
label?: string,
]}
customQuickSelectPanels={[
{
title: string,
content: ReactElement,
},
]}
customQuickSelectRender={({
quickSelect,
commonlyUsedTimes,
recentlyUsedTimes,
customQuickSelectPanels,
}) => (
<>
{customQuickSelectPanels}
{quickSelect}
{commonlyUsedTimes}
{recentlyUsedTimes}
</>
)}
/>
`;

const quickSelectTableColumns = [
{
field: 'key',
name: 'Key',
},
{
field: 'controls',
name: 'Panel Controlled',
},
];

const quickSelectTableItems = [
{
key: 'quickSelect',
controls: 'Quick select',
},
{
key: 'commonlyUsedTimes',
controls: 'Commonly used',
},
{
key: 'recentlyUsedTimes',
controls: 'Recently used',
},
{
key: 'customQuickSelectPanels',
controls: 'Custom panels',
},
{
key: 'applyRefreshIntervalPanels',
controls: 'Refresh toggles',
},
];

export const SuperDatePickerExample = {
title: 'Super date picker',
intro: (
Expand Down Expand Up @@ -218,6 +285,32 @@ if (!endMoment || !endMoment.isValid()) {
snippet: superDatePickerCustomQuickSelectSnippet,
demo: <SuperDatePickerCustomQuickSelect />,
},
{
source: [
{
type: GuideSectionTypes.TSX,
code: superDatePickerCustomQuickSelectCustomOrderSource,
},
],
text: (
<>
<h3>Customing panel order</h3>
breehall marked this conversation as resolved.
Show resolved Hide resolved
<p>
You can optionally pass the{' '}
<EuiCode>customQuickSelectRender</EuiCode> prop that returns Quick
Select panels and allows you to customize the order of them.{' '}
</p>
<EuiBasicTable
tableCaption="Quick select panel keys"
items={quickSelectTableItems}
rowHeader="key"
columns={quickSelectTableColumns}
/>
breehall marked this conversation as resolved.
Show resolved Hide resolved
</>
),
snippet: superDatePickerCustomQuickSelectCustomOrderSnippet,
demo: <SuperDatePickerCustomQuickSelectCustomOrder />,
},
{
title: 'Sizing',
source: [
Expand Down
Loading