Skip to content

Commit 8b2f678

Browse files
authored
fix: search with enter not trigger correct change params (#253)
* fix: search with enter should give correct value * test: update test case
1 parent ac0dcf6 commit 8b2f678

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

src/OptionList/useKeyboard.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import * as React from 'react';
22
import type { RefOptionListProps } from 'rc-select/lib/OptionList';
3+
import { useBaseProps } from 'rc-select';
34
import KeyCode from 'rc-util/lib/KeyCode';
45
import type { DefaultOptionType, InternalFieldNames, SingleValueType } from '../Cascader';
5-
import { useBaseProps } from 'rc-select';
6+
import { SEARCH_MARK } from '../hooks/useSearchOptions';
67

78
export default (
89
ref: React.Ref<RefOptionListProps>,
@@ -151,7 +152,18 @@ export default (
151152
// >>> Select
152153
case KeyCode.ENTER: {
153154
if (validActiveValueCells.length) {
154-
onKeyBoardSelect(validActiveValueCells, lastActiveOptions[lastActiveIndex]);
155+
const option = lastActiveOptions[lastActiveIndex];
156+
157+
// Search option should revert back of origin options
158+
const originOptions: DefaultOptionType[] = option?.[SEARCH_MARK] || [];
159+
if (originOptions.length) {
160+
onKeyBoardSelect(
161+
originOptions.map(opt => opt[fieldNames.value]),
162+
originOptions[originOptions.length - 1],
163+
);
164+
} else {
165+
onKeyBoardSelect(validActiveValueCells, lastActiveOptions[lastActiveIndex]);
166+
}
155167
}
156168
break;
157169
}

tests/keyboard.spec.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import { addressOptions } from './demoOptions';
77
describe('Cascader.Keyboard', () => {
88
let wrapper;
99
let selectedValue;
10+
let selectedOptions;
1011
let menus;
11-
const onChange = value => {
12+
const onChange = (value, options) => {
1213
selectedValue = value;
14+
selectedOptions = options;
1315
};
1416

1517
beforeEach(() => {
@@ -18,6 +20,7 @@ describe('Cascader.Keyboard', () => {
1820

1921
afterEach(() => {
2022
selectedValue = null;
23+
selectedOptions = null;
2124
menus = null;
2225
});
2326

@@ -73,6 +76,24 @@ describe('Cascader.Keyboard', () => {
7376
wrapper.find('input').simulate('keyDown', { which: KeyCode.ENTER });
7477
expect(wrapper.isOpen()).toBeFalsy();
7578
expect(selectedValue).toEqual(['zj', 'hangzhou', 'yuhang']);
79+
expect(selectedOptions).toEqual([
80+
addressOptions[1],
81+
addressOptions[1].children[0],
82+
addressOptions[1].children[0].children[0],
83+
]);
84+
});
85+
86+
it('enter on search', () => {
87+
wrapper.find('input').simulate('change', { target: { value: '余杭' } });
88+
wrapper.find('input').simulate('keyDown', { which: KeyCode.DOWN });
89+
wrapper.find('input').simulate('keyDown', { which: KeyCode.ENTER });
90+
91+
expect(selectedValue).toEqual(['zj', 'hangzhou', 'yuhang']);
92+
expect(selectedOptions).toEqual([
93+
addressOptions[1],
94+
addressOptions[1].children[0],
95+
addressOptions[1].children[0].children[0],
96+
]);
7697
});
7798

7899
it('rtl', () => {

0 commit comments

Comments
 (0)