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

fix: trigger onBlur does not require reset focused #980

Merged
merged 5 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
7 changes: 6 additions & 1 deletion src/BaseSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ const BaseSelect = React.forwardRef((props: BaseSelectProps, ref: React.Ref<Base
const triggerRef = React.useRef<RefTriggerProps>(null);
const selectorRef = React.useRef<RefSelectorProps>(null);
const listRef = React.useRef<RefOptionListProps>(null);
const blurRef = React.useRef<boolean>(false);

/** Used for component focused management */
const [mockFocused, setMockFocused, cancelSetMockFocused] = useDelayReset();
Expand Down Expand Up @@ -451,7 +452,8 @@ const BaseSelect = React.forwardRef((props: BaseSelectProps, ref: React.Ref<Base
setInnerOpen(false);
}

if (disabled) {
// After onBlur is triggered, the focused does not need to be reset
if (disabled && !blurRef.current) {
setMockFocused(false);
}
}, [disabled]);
Expand Down Expand Up @@ -561,8 +563,11 @@ const BaseSelect = React.forwardRef((props: BaseSelectProps, ref: React.Ref<Base
};

const onContainerBlur: React.FocusEventHandler<HTMLElement> = (...args) => {
blurRef.current = true;

setMockFocused(false, () => {
focusRef.current = false;
blurRef.current = false;
onToggleOpen(false);
});

Expand Down
44 changes: 42 additions & 2 deletions tests/focus.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { mount } from 'enzyme';
import React from 'react';
import { mount} from 'enzyme';
import React, { useState } from 'react';
MadCcc marked this conversation as resolved.
Show resolved Hide resolved
import { act } from 'react-dom/test-utils';
import Select from '../src';
import { fireEvent, render } from '@testing-library/react';

describe('Select.Focus', () => {
it('disabled should reset focused', () => {
Expand Down Expand Up @@ -31,6 +32,45 @@ describe('Select.Focus', () => {
jest.useRealTimers();
});

it('after onBlur is triggered the focused does not need to be reset', () => {
jest.useFakeTimers();

const onFocus = jest.fn();

const Demo: React.FC = () => {
const [disabled, setDisabled] = useState(false);
return (
<>
<Select disabled={disabled} onFocus={onFocus} onBlur={() => setDisabled(true)} />
<button onClick={() => setDisabled(false)} />
</>
);
};

const { container } = render(<Demo />);

fireEvent.focus(container.querySelector('input'));
jest.runAllTimers();

// trigger disabled
fireEvent.blur(container.querySelector('input'));
jest.runAllTimers();

// reset focused
onFocus.mockReset();

expect(container.querySelector('.rc-select-disabled')).toBeTruthy();

// reset disabled
fireEvent.click(container.querySelector('button'));
fireEvent.focus(container.querySelector('input'));
jest.runAllTimers();

expect(onFocus).toHaveBeenCalled();

jest.useRealTimers();
});

it('IE focus', () => {
jest.clearAllTimers();
jest.useFakeTimers();
Expand Down
Loading