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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(date-picker): merge origin input and popup input focus #15423

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 20 additions & 2 deletions components/date-picker/createPicker.tsx
Expand Up @@ -21,6 +21,7 @@ export interface PickerProps {

export interface PickerState {
open: boolean;
hadFocus: boolean;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not use hasFocus?

value: moment.Moment | null;
showDate: moment.Moment | null;
}
Expand Down Expand Up @@ -68,6 +69,7 @@ export default function createPicker(TheCalendar: React.ComponentClass): any {
value,
showDate: value,
open: false,
hadFocus: false,
};
}

Expand Down Expand Up @@ -117,6 +119,22 @@ export default function createPicker(TheCalendar: React.ComponentClass): any {
}
};

onFocus = () => {
const { open, hadFocus } = this.state;
if (!open && !hadFocus) {
this.setState({ hadFocus: true });
this.props.onFocus()
}
};

onBlur = () => {
const { open, hadFocus } = this.state;
if (!open && hadFocus) {
this.setState({ hadFocus: false });
this.props.onBlur()
}
};

focus() {
this.input.focus();
}
Expand Down Expand Up @@ -245,8 +263,8 @@ export default function createPicker(TheCalendar: React.ComponentClass): any {
id={props.id}
className={classNames(props.className, props.pickerClass)}
style={{ ...pickerStyle, ...props.style }}
onFocus={props.onFocus}
onBlur={props.onBlur}
onFocus={this.onFocus}
onBlur={this.onBlur}
onMouseEnter={props.onMouseEnter}
onMouseLeave={props.onMouseLeave}
>
Expand Down
18 changes: 18 additions & 0 deletions tests/shared/focusTest.js
Expand Up @@ -29,6 +29,14 @@ export default function focusTest(Component) {
expect(handleFocus).toBeCalled();
});

it('focus() and onFocus trigger only once', () => {
const handleFocus = jest.fn();
const wrapper = mount(<Component onFocus={handleFocus} />, { attachTo: container });
wrapper.instance().focus();
jest.runAllTimers();
expect(handleFocus).toBeCalledTimes(1);
});

it('blur() and onBlur', () => {
const handleBlur = jest.fn();
const wrapper = mount(<Component onBlur={handleBlur} />, { attachTo: container });
Expand All @@ -39,6 +47,16 @@ export default function focusTest(Component) {
expect(handleBlur).toBeCalled();
});

it('blur() and onBlur only trigger once', () => {
const handleBlur = jest.fn();
const wrapper = mount(<Component onBlur={handleBlur} />, { attachTo: container });
wrapper.instance().focus();
jest.runAllTimers();
wrapper.instance().blur();
jest.runAllTimers();
expect(handleBlur).toBeCalledTimes(1);
});

it('autoFocus', () => {
const handleFocus = jest.fn();
mount(<Component autoFocus onFocus={handleFocus} />, { attachTo: container });
Expand Down