Skip to content

Commit daaa285

Browse files
authored
fix: Disabled select should reset focus state (#671)
* fix: Disabled select should reset focus state * test: split to avoid test failed
1 parent 95aa5b9 commit daaa285

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

.eslintrc.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,5 @@ module.exports = {
99
'react/sort-comp': 0,
1010
'jsx-a11y/interactive-supports-focus': 0,
1111
'jsx-a11y/no-autofocus': 0,
12-
'import/no-extraneous-dependencies': ['error', { devDependencies: true }],
1312
},
1413
};

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Thumbs.db
2121
node_modules
2222
.cache
2323
dist
24+
debug.tsx
2425
assets/**/*.css
2526
build
2627
lib

src/generate.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -773,11 +773,15 @@ export default function generateSelector<
773773
setInnerSearchValue('');
774774
};
775775

776-
// Close dropdown when disabled change
776+
// Close dropdown & remove focus state when disabled change
777777
useEffect(() => {
778-
if (innerOpen && !!disabled) {
778+
if (innerOpen && disabled) {
779779
setInnerOpen(false);
780780
}
781+
782+
if (disabled) {
783+
setMockFocused(false);
784+
}
781785
}, [disabled]);
782786

783787
// Close will clean up single mode search text

tests/focus.test.tsx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { mount } from 'enzyme';
2+
import React from 'react';
3+
import { act } from 'react-dom/test-utils';
4+
import Select from '../src';
5+
6+
describe('Select.Focus', () => {
7+
it('disabled should reset focused', () => {
8+
jest.clearAllTimers();
9+
jest.useFakeTimers();
10+
11+
jest.clearAllTimers();
12+
13+
const wrapper = mount(<Select />);
14+
15+
// Focus
16+
wrapper.find('input').simulate('focus');
17+
act(() => {
18+
jest.runAllTimers();
19+
wrapper.update();
20+
});
21+
expect(wrapper.exists('.rc-select-focused')).toBeTruthy();
22+
23+
// Disabled
24+
wrapper.setProps({ disabled: true });
25+
act(() => {
26+
jest.runAllTimers();
27+
wrapper.update();
28+
});
29+
expect(wrapper.exists('.rc-select-focused')).toBeFalsy();
30+
31+
jest.useRealTimers();
32+
});
33+
});

0 commit comments

Comments
 (0)