Skip to content

Commit 37ec868

Browse files
authored
fix: MultipleSelect should inputable when first focus (#571)
* fix: Default editable * fix: editable when focused * test: More test case
1 parent 8edd430 commit 37ec868

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

examples/multiple.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class Test extends React.Component {
7979

8080
<div style={{ width: 300 }}>
8181
<Select
82+
autoFocus
8283
value={value}
8384
animation={useAnim ? 'slide-up' : null}
8485
choiceTransitionName="rc-select-selection__choice-zoom"

src/Selector/MultipleSelector.tsx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as React from 'react';
2+
import { useState } from 'react';
23
import classNames from 'classnames';
34
import pickAttrs from 'rc-util/lib/pickAttrs';
45
import { CSSMotionList } from 'rc-motion';
@@ -64,9 +65,10 @@ const SelectSelector: React.FC<SelectorProps> = props => {
6465
onInputCompositionEnd,
6566
} = props;
6667

67-
const [motionAppear, setMotionAppear] = React.useState(false);
68+
const [motionAppear, setMotionAppear] = useState(false);
6869
const measureRef = React.useRef<HTMLSpanElement>(null);
69-
const [inputWidth, setInputWidth] = React.useState(0);
70+
const [inputWidth, setInputWidth] = useState(0);
71+
const [focused, setFocused] = useState(false);
7072

7173
// ===================== Motion ======================
7274
React.useEffect(() => {
@@ -75,7 +77,7 @@ const SelectSelector: React.FC<SelectorProps> = props => {
7577

7678
// ===================== Search ======================
7779
const inputValue = open || mode === 'tags' ? searchValue : '';
78-
const inputEditable: boolean = mode === 'tags' || (open && showSearch);
80+
const inputEditable: boolean = mode === 'tags' || (showSearch && (open || focused));
7981

8082
// We measure width and set to the input immediately
8183
useLayoutEffect(() => {
@@ -181,7 +183,16 @@ const SelectSelector: React.FC<SelectorProps> = props => {
181183
<>
182184
{selectionNode}
183185

184-
<span className={`${prefixCls}-selection-search`} style={{ width: inputWidth }}>
186+
<span
187+
className={`${prefixCls}-selection-search`}
188+
style={{ width: inputWidth }}
189+
onFocus={() => {
190+
setFocused(true);
191+
}}
192+
onBlur={() => {
193+
setFocused(false);
194+
}}
195+
>
185196
<Input
186197
ref={inputRef}
187198
open={open}

tests/Multiple.test.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,4 +429,18 @@ describe('Select.Multiple', () => {
429429
],
430430
);
431431
});
432+
433+
it('focus should enable showSearch input', () => {
434+
const wrapper = mount(
435+
<Select
436+
mode="multiple"
437+
options={[{ value: 'light', label: 'Light', option: 2333 }]}
438+
showSearch
439+
/>,
440+
);
441+
442+
wrapper.find('input').simulate('focus');
443+
444+
expect(wrapper.find('Input').prop('editable')).toBeTruthy();
445+
});
432446
});

0 commit comments

Comments
 (0)