From e51d7bca6bdd2fa73341f40e69ca5f93f2412d28 Mon Sep 17 00:00:00 2001 From: zombiej Date: Mon, 8 Nov 2021 14:17:18 +0800 Subject: [PATCH 1/2] fix: Disabled select should reset focus state --- .eslintrc.js | 1 - .gitignore | 1 + src/generate.tsx | 8 ++++++-- tests/Select.test.tsx | 25 +++++++++++++++++++++++++ 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 4b62cc7af..0833fbcbd 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -9,6 +9,5 @@ module.exports = { 'react/sort-comp': 0, 'jsx-a11y/interactive-supports-focus': 0, 'jsx-a11y/no-autofocus': 0, - 'import/no-extraneous-dependencies': ['error', { devDependencies: true }], }, }; diff --git a/.gitignore b/.gitignore index 6e8f606b5..9b0000159 100644 --- a/.gitignore +++ b/.gitignore @@ -21,6 +21,7 @@ Thumbs.db node_modules .cache dist +debug.tsx assets/**/*.css build lib diff --git a/src/generate.tsx b/src/generate.tsx index da61a0a38..ac429adfa 100644 --- a/src/generate.tsx +++ b/src/generate.tsx @@ -773,11 +773,15 @@ export default function generateSelector< setInnerSearchValue(''); }; - // Close dropdown when disabled change + // Close dropdown & remove focus state when disabled change useEffect(() => { - if (innerOpen && !!disabled) { + if (innerOpen && disabled) { setInnerOpen(false); } + + if (disabled) { + setMockFocused(false); + } }, [disabled]); // Close will clean up single mode search text diff --git a/tests/Select.test.tsx b/tests/Select.test.tsx index dd808cd85..65c709918 100644 --- a/tests/Select.test.tsx +++ b/tests/Select.test.tsx @@ -1697,4 +1697,29 @@ describe('Select.Basic', () => { expect(wrapper.find('Trigger').prop('popupPlacement')).toEqual('topRight'); }); }); + + it('disabled should reset focused', () => { + jest.useFakeTimers(); + jest.clearAllTimers(); + + const wrapper = mount(); - - // Focus - wrapper.find('input').simulate('focus'); - act(() => { - jest.runAllTimers(); - wrapper.update(); - }); - expect(wrapper.exists('.rc-select-focused')).toBeTruthy(); - - // Disabled - wrapper.setProps({ disabled: true }); - act(() => { - jest.runAllTimers(); - wrapper.update(); - }); - expect(wrapper.exists('.rc-select-focused')).toBeFalsy(); - - jest.useRealTimers(); - }); }); diff --git a/tests/focus.test.tsx b/tests/focus.test.tsx new file mode 100644 index 000000000..6aae2b528 --- /dev/null +++ b/tests/focus.test.tsx @@ -0,0 +1,33 @@ +import { mount } from 'enzyme'; +import React from 'react'; +import { act } from 'react-dom/test-utils'; +import Select from '../src'; + +describe('Select.Focus', () => { + it('disabled should reset focused', () => { + jest.clearAllTimers(); + jest.useFakeTimers(); + + jest.clearAllTimers(); + + const wrapper = mount(