Skip to content

Commit

Permalink
[EuiSuperDatePicker] Support onFocus (#4924) (#6320)
Browse files Browse the repository at this point in the history
* [EuiSuperDatePicker] Support onFocus (#4924)

* callback invoking multiple times.

* [EuiSuperDatePicker] Updated the onFocus call on buttonProps and prettyDuration (#4924)

* Apply suggestions from code review

[EuiSuperDatePicker] Added code suggestions for onFocus (#4924)

Co-authored-by: Constance <constancecchen@users.noreply.github.com>

* [EuiSuperDatePicker] Updated super_date_picker's tests and onFocus on prettyFormat  (#4924)

* [EuiSuperDatePicker] added test case for focus on superDatePickerShowDatesButton (#4924)

* [EuiSuperDatePicker] prettier error resolved (#4924)

Co-authored-by: Constance <constancecchen@users.noreply.github.com>
  • Loading branch information
UzairNoman and Constance authored Nov 7, 2022
1 parent 9e04b8d commit 787a47c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
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');

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`

0 comments on commit 787a47c

Please sign in to comment.