Skip to content

Commit

Permalink
[EuiSuperDatePicker][EuiAutoRefresh][EuiRefreshInterval] Allow contro…
Browse files Browse the repository at this point in the history
…lling the rendered unit (#7501)
  • Loading branch information
cee-chen authored Feb 6, 2024
1 parent c7f4974 commit 86a47eb
Show file tree
Hide file tree
Showing 15 changed files with 311 additions and 43 deletions.
3 changes: 3 additions & 0 deletions changelogs/upcoming/7501.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- Updated `EuiSuperDatePicker` with a new `refreshIntervalUnits` prop. Passing this prop allows controlling and overriding the default unit rounding behavior.
- Updated `EuiAutoRefresh` and `EuiRefreshInterval` with a new `intervalUnits` prop. Passing this prop allows controlling and overriding the default unit rounding behavior.
- Updated `onRefreshChange` to pass back a new `intervalUnits` key that contains the current interval unit format (seconds, minutes, or hours).
7 changes: 7 additions & 0 deletions src-docs/src/views/auto_refresh/playground.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
propUtilityForPlayground,
dummyFunction,
simulateFunction,
createOptionalEnum,
} from '../../services/playground';

export const autoRefreshConfig = () => {
Expand All @@ -21,6 +22,8 @@ export const autoRefreshConfig = () => {
true
);

propsToUse.intervalUnits = createOptionalEnum(propsToUse.intervalUnits);

propsToUse.append = {
...propsToUse.append,
type: PropTypes.String,
Expand Down Expand Up @@ -56,6 +59,8 @@ export const autoRefreshButtonConfig = () => {
true
);

propsToUse.intervalUnits = createOptionalEnum(propsToUse.intervalUnits);

return {
config: {
componentName: 'EuiAutoRefreshButton',
Expand Down Expand Up @@ -86,6 +91,8 @@ export const refreshIntervalConfig = () => {
true
);

propsToUse.intervalUnits = createOptionalEnum(propsToUse.intervalUnits);

return {
config: {
componentName: 'EuiRefreshInterval',
Expand Down
21 changes: 20 additions & 1 deletion src-docs/src/views/super_date_picker/auto_refresh.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ import {
OnRefreshChangeProps,
OnRefreshProps,
OnTimeChangeProps,
RefreshUnitsOptions,
EuiSwitch,
} from '../../../../src';

export default () => {
const [doNotRoundUnits, setDoNotRoundUnits] = useState(false);
const [intervalUnits, setIntervalUnits] = useState<RefreshUnitsOptions>();

const [refreshInterval, setRefreshInterval] = useState(1000);
const [isPaused, setIsPaused] = useState(true);
const [start, setStart] = useState('now-30m');
Expand All @@ -29,9 +34,11 @@ export default () => {
const onRefreshChange = ({
isPaused,
refreshInterval,
intervalUnits,
}: OnRefreshChangeProps) => {
setIsPaused(isPaused);
setRefreshInterval(refreshInterval);
setIntervalUnits(intervalUnits);
};

return (
Expand All @@ -41,8 +48,20 @@ export default () => {
onTimeChange={onTimeChange}
onRefresh={onRefresh}
isPaused={isPaused}
refreshInterval={refreshInterval}
onRefreshChange={onRefreshChange}
refreshInterval={refreshInterval}
refreshIntervalUnits={doNotRoundUnits ? intervalUnits : undefined}
customQuickSelectRender={({ refreshInterval }) => (
<>
<EuiSwitch
label="Round refresh interval up to the next largest unit"
checked={!doNotRoundUnits}
onChange={() => setDoNotRoundUnits(!doNotRoundUnits)}
compressed
/>
{refreshInterval}
</>
)}
/>
);
};
5 changes: 5 additions & 0 deletions src-docs/src/views/super_date_picker/playground.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
propUtilityForPlayground,
dummyFunction,
simulateFunction,
createOptionalEnum,
} from '../../services/playground';

export const superDatePickerConfig = () => {
Expand All @@ -18,6 +19,10 @@ export const superDatePickerConfig = () => {
propsToUse.onTimeChange = simulateFunction(propsToUse.onTimeChange, true);
propsToUse.onRefreshChange = simulateFunction(propsToUse.onRefreshChange);

propsToUse.refreshIntervalUnits = createOptionalEnum(
propsToUse.refreshIntervalUnits
);

propsToUse.isPaused = {
...propsToUse.isPaused,
type: PropTypes.Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ if (!endMoment || !endMoment.isValid()) {
text: (
<>
<p>
Set <EuiCode>isAutoRefreshOnly</EuiCode> to <EuiCode>true </EuiCode>{' '}
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.
Expand Down
89 changes: 89 additions & 0 deletions src/components/date_picker/auto_refresh/auto_refresh.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import React from 'react';
import { fireEvent } from '@testing-library/react';
import { requiredProps } from '../../../test/required_props';
import { render } from '../../../test/rtl';

Expand Down Expand Up @@ -40,6 +41,50 @@ describe('EuiAutoRefresh', () => {

expect(container.firstChild).toMatchSnapshot();
});

test('intervalUnits forces rendering in the provided units', () => {
const { getByLabelText, getByRole, getByTestSubject } = render(
<EuiAutoRefresh
isPaused={false}
refreshInterval={200000}
intervalUnits="s"
onRefreshChange={() => {}}
/>
);

expect(getByLabelText('Auto refresh')).toHaveValue('200 seconds');

fireEvent.click(getByRole('button'));
expect(getByTestSubject('superDatePickerRefreshIntervalInput')).toHaveValue(
200
);
expect(
getByTestSubject('superDatePickerRefreshIntervalUnitsSelect')
).toHaveValue('s');
});

test('onRefreshChange passes back all expected values', () => {
const onRefreshChange = jest.fn();
const { getByLabelText, getByTestSubject } = render(
<EuiAutoRefresh
isPaused={false}
refreshInterval={1000}
onRefreshChange={onRefreshChange}
/>
);

fireEvent.click(getByLabelText('Auto refresh'));
fireEvent.change(
getByTestSubject('superDatePickerRefreshIntervalUnitsSelect'),
{ target: { value: 'h' } }
);

expect(onRefreshChange).toHaveBeenCalledWith({
refreshInterval: 3600000,
intervalUnits: 'h',
isPaused: false,
});
});
});

describe('EuiAutoRefreshButton', () => {
Expand Down Expand Up @@ -70,4 +115,48 @@ describe('EuiAutoRefreshButton', () => {

expect(container.firstChild).toMatchSnapshot();
});

test('intervalUnits forces rendering in the provided units', () => {
const { getByRole, getByTestSubject } = render(
<EuiAutoRefreshButton
isPaused={false}
refreshInterval={200000}
intervalUnits="s"
onRefreshChange={() => {}}
/>
);

expect(getByRole('button')).toHaveTextContent('200 s');

fireEvent.click(getByRole('button'));
expect(getByTestSubject('superDatePickerRefreshIntervalInput')).toHaveValue(
200
);
expect(
getByTestSubject('superDatePickerRefreshIntervalUnitsSelect')
).toHaveValue('s');
});

test('onRefreshChange passes back all expected values', () => {
const onRefreshChange = jest.fn();
const { getByRole, getByTestSubject } = render(
<EuiAutoRefreshButton
isPaused={false}
refreshInterval={1000}
onRefreshChange={onRefreshChange}
/>
);

fireEvent.click(getByRole('button'));
fireEvent.change(
getByTestSubject('superDatePickerRefreshIntervalUnitsSelect'),
{ target: { value: 'h' } }
);

expect(onRefreshChange).toHaveBeenCalledWith({
refreshInterval: 3600000,
intervalUnits: 'h',
isPaused: false,
});
});
});
19 changes: 16 additions & 3 deletions src/components/date_picker/auto_refresh/auto_refresh.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export type EuiAutoRefreshProps = EuiAutoRefreshSharedProps & {
export const EuiAutoRefresh: FunctionComponent<EuiAutoRefreshProps> = ({
className,
onRefreshChange,
intervalUnits,
isDisabled,
isPaused = true,
refreshInterval = 1000,
Expand Down Expand Up @@ -74,7 +75,9 @@ export const EuiAutoRefresh: FunctionComponent<EuiAutoRefreshProps> = ({
}
readOnly={readOnly}
disabled={isDisabled}
value={usePrettyInterval(Boolean(isPaused), refreshInterval)}
value={usePrettyInterval(Boolean(isPaused), refreshInterval, {
unit: intervalUnits,
})}
{...rest}
/>
}
Expand All @@ -87,6 +90,7 @@ export const EuiAutoRefresh: FunctionComponent<EuiAutoRefreshProps> = ({
onRefreshChange={onRefreshChange}
isPaused={isPaused}
refreshInterval={refreshInterval}
intervalUnits={intervalUnits}
/>
</EuiInputPopover>
);
Expand All @@ -107,6 +111,7 @@ export const EuiAutoRefreshButton: FunctionComponent<
> = ({
className,
onRefreshChange,
intervalUnits,
isDisabled,
isPaused = true,
refreshInterval = 1000,
Expand All @@ -125,7 +130,11 @@ export const EuiAutoRefreshButton: FunctionComponent<
const autoRefeshLabelOn = useEuiI18n(
'euiAutoRefresh.buttonLabelOn',
'Auto refresh is on and set to {prettyInterval}',
{ prettyInterval: usePrettyInterval(Boolean(isPaused), refreshInterval) }
{
prettyInterval: usePrettyInterval(Boolean(isPaused), refreshInterval, {
unit: intervalUnits,
}),
}
);

return (
Expand All @@ -141,7 +150,10 @@ export const EuiAutoRefreshButton: FunctionComponent<
isDisabled={isDisabled}
{...rest}
>
{usePrettyInterval(Boolean(isPaused), refreshInterval, shortHand)}
{usePrettyInterval(Boolean(isPaused), refreshInterval, {
shortHand,
unit: intervalUnits,
})}
</EuiButtonEmpty>
}
isOpen={isPopoverOpen}
Expand All @@ -156,6 +168,7 @@ export const EuiAutoRefreshButton: FunctionComponent<
onRefreshChange={onRefreshChange}
isPaused={isPaused}
refreshInterval={refreshInterval}
intervalUnits={intervalUnits}
/>
</EuiPopover>
);
Expand Down
Loading

0 comments on commit 86a47eb

Please sign in to comment.