Skip to content

Commit 57a0cdb

Browse files
authored
fix: when click clear button, it will be focused (#812)
* fix: when click clear button, it will be focused * fix: remove duplicate code * fix: attachTo body can trigger event propagation
1 parent 57216a9 commit 57a0cdb

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/BaseSelect.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,8 @@ const BaseSelect = React.forwardRef((props: BaseSelectProps, ref: React.Ref<Base
694694
const onClearMouseDown: React.MouseEventHandler<HTMLSpanElement> = () => {
695695
onClear?.();
696696

697+
selectorRef.current?.focus();
698+
697699
onDisplayValuesChange([], {
698700
type: 'clear',
699701
values: displayValues,

tests/Select.test.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1949,4 +1949,29 @@ describe('Select.Basic', () => {
19491949
expect(wrapper.find('span.rc-select-selection-item').at(2).prop('title')).toEqual('+ 1 ...');
19501950
});
19511951
});
1952+
1953+
it('should be focused when click clear button', () => {
1954+
jest.useFakeTimers();
1955+
1956+
const mouseDownPreventDefault = jest.fn();
1957+
const wrapper = mount(
1958+
<Select allowClear value="1">
1959+
<Option value="1">1</Option>
1960+
<Option value="2">2</Option>
1961+
</Select>,
1962+
{
1963+
attachTo: document.body,
1964+
},
1965+
);
1966+
1967+
expect(wrapper.find('.rc-select-clear-icon').length).toBeTruthy();
1968+
1969+
wrapper
1970+
.find('.rc-select-clear-icon')
1971+
.simulate('mouseDown', { preventDefault: mouseDownPreventDefault });
1972+
jest.runAllTimers();
1973+
1974+
expect(wrapper.find('.rc-select').getDOMNode().className).toContain('-focused');
1975+
jest.useRealTimers();
1976+
});
19521977
});

0 commit comments

Comments
 (0)