Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,36 @@ describe('EuiSuperDatePicker', () => {
);
});

it('invokes onFocus callbacks on the date popover buttons', () => {
const focusMock = jest.fn();
const component = mount(
<EuiSuperDatePicker
onTimeChange={noop}
showUpdateButton={false}
onFocus={focusMock}
/>
);

component
.find('button[data-test-subj="superDatePickerShowDatesButton"]')
.simulate('focus');
expect(focusMock).toBeCalledTimes(1);

component
.find('button[data-test-subj="superDatePickerShowDatesButton"]')
.simulate('click');
Comment on lines +185 to +187
Copy link
Contributor

Choose a reason for hiding this comment

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

This button should also be triggering the focus handler, so we should simulate focus on it as well

Suggested change
component
.find('button[data-test-subj="superDatePickerShowDatesButton"]')
.simulate('click');
component
.find('button[data-test-subj="superDatePickerShowDatesButton"]')
.simulate('click')
.simulate('focus');
expect(focusMock).toBeCalledTimes(1);


component
.find('button[data-test-subj="superDatePickerstartDatePopoverButton"]')
.simulate('focus');
expect(focusMock).toBeCalledTimes(2);

component
.find('button[data-test-subj="superDatePickerstartDatePopoverButton"]')
.simulate('focus');
expect(focusMock).toBeCalledTimes(3);
});

describe('showUpdateButton', () => {
test('can be false', () => {
const component = shallowAndDive(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

import React, { Component, FunctionComponent } from 'react';
import React, { Component, FocusEventHandler, FunctionComponent } from 'react';
import classNames from 'classnames';
import moment, { LocaleSpecifier } from 'moment'; // eslint-disable-line import/named
import dateMath from '@elastic/datemath';
Expand Down Expand Up @@ -87,6 +87,11 @@ export type EuiSuperDatePickerProps = CommonProps & {
*/
locale?: LocaleSpecifier;

/**
* Triggered whenever the EuiSuperDatePicker's dates are focused
*/
onFocus?: FocusEventHandler;

/**
* Callback for when the refresh interval is fired.
* EuiSuperDatePicker will only manage a refresh interval timer when onRefresh callback is supplied
Expand Down Expand Up @@ -393,6 +398,7 @@ export class EuiSuperDatePickerInternal extends Component<
timeFormat,
utcOffset,
compressed,
onFocus,
} = this.props;

if (
Expand All @@ -415,6 +421,7 @@ export class EuiSuperDatePickerInternal extends Component<
data-test-subj="superDatePickerShowDatesButton"
disabled={isDisabled}
onClick={this.hidePrettyDuration}
onFocus={onFocus}
>
<PrettyDuration
timeFrom={start}
Expand Down Expand Up @@ -454,6 +461,7 @@ export class EuiSuperDatePickerInternal extends Component<
onPopoverToggle={this.onStartDatePopoverToggle}
onPopoverClose={this.onStartDatePopoverClose}
timeOptions={timeOptions}
buttonProps={{ onFocus }}
/>
}
endDateControl={
Expand All @@ -474,6 +482,7 @@ export class EuiSuperDatePickerInternal extends Component<
onPopoverToggle={this.onEndDatePopoverToggle}
onPopoverClose={this.onEndDatePopoverClose}
timeOptions={timeOptions}
buttonProps={{ onFocus }}
/>
}
/>
Expand Down
2 changes: 2 additions & 0 deletions upcoming_changelogs/6320.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Added `onFocus` prop callback to `EuiSuperDatePicker`