Skip to content

Commit df90acd

Browse files
authored
fix: Select placeholder disappear when searchValue controlled (#464)
* fix: Placehodler logic * add test case
1 parent 5f3846a commit df90acd

File tree

4 files changed

+58
-137
lines changed

4 files changed

+58
-137
lines changed

src/Selector/MultipleSelector.tsx

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@ import React from 'react';
22
import classNames from 'classnames';
33
import CSSMotionList from 'rc-animate/lib/CSSMotionList';
44
import TransBtn from '../TransBtn';
5-
import {
6-
LabelValueType,
7-
RawValueType,
8-
CustomTagProps,
9-
} from '../interface/generator';
5+
import { LabelValueType, RawValueType, CustomTagProps } from '../interface/generator';
106
import { RenderNode } from '../interface';
117
import { InnerSelectorProps } from '.';
128
import Input from './Input';
@@ -21,9 +17,7 @@ interface SelectorProps extends InnerSelectorProps {
2117
// Tags
2218
maxTagCount?: number;
2319
maxTagTextLength?: number;
24-
maxTagPlaceholder?:
25-
| React.ReactNode
26-
| ((omittedValues: LabelValueType[]) => React.ReactNode);
20+
maxTagPlaceholder?: React.ReactNode | ((omittedValues: LabelValueType[]) => React.ReactNode);
2721
tokenSeparators?: string[];
2822
tagRender?: (props: CustomTagProps) => React.ReactElement;
2923

@@ -55,8 +49,7 @@ const SelectSelector: React.FC<SelectorProps> = ({
5549

5650
maxTagCount,
5751
maxTagTextLength,
58-
maxTagPlaceholder = (omittedValues: LabelValueType[]) =>
59-
`+ ${omittedValues.length} ...`,
52+
maxTagPlaceholder = (omittedValues: LabelValueType[]) => `+ ${omittedValues.length} ...`,
6053
tagRender,
6154

6255
onSelect,
@@ -74,12 +67,13 @@ const SelectSelector: React.FC<SelectorProps> = ({
7467
}, []);
7568

7669
// ===================== Search ======================
70+
const inputValue = open ? searchValue : '';
7771
const inputEditable: boolean = mode === 'tags' || (open && showSearch);
7872

7973
// We measure width and set to the input immediately
8074
useLayoutEffect(() => {
8175
setInputWidth(measureRef.current.scrollWidth);
82-
}, [searchValue]);
76+
}, [inputValue]);
8377

8478
// ==================== Selection ====================
8579
let displayValues: LabelValueType[] = values;
@@ -142,12 +136,7 @@ const SelectSelector: React.FC<SelectorProps> = ({
142136
};
143137

144138
return typeof tagRender === 'function' ? (
145-
<span
146-
key={mergedKey}
147-
onMouseDown={onMouseDown}
148-
className={className}
149-
style={style}
150-
>
139+
<span key={mergedKey} onMouseDown={onMouseDown} className={className} style={style}>
151140
{tagRender({
152141
label,
153142
value,
@@ -164,9 +153,7 @@ const SelectSelector: React.FC<SelectorProps> = ({
164153
})}
165154
style={style}
166155
>
167-
<span className={`${prefixCls}-selection-item-content`}>
168-
{label}
169-
</span>
156+
<span className={`${prefixCls}-selection-item-content`}>{label}</span>
170157
{closable && (
171158
<TransBtn
172159
className={`${prefixCls}-selection-item-remove`}
@@ -187,10 +174,7 @@ const SelectSelector: React.FC<SelectorProps> = ({
187174
<>
188175
{selectionNode}
189176

190-
<span
191-
className={`${prefixCls}-selection-search`}
192-
style={{ width: inputWidth }}
193-
>
177+
<span className={`${prefixCls}-selection-search`} style={{ width: inputWidth }}>
194178
<Input
195179
ref={inputRef}
196180
open={open}
@@ -201,27 +185,21 @@ const SelectSelector: React.FC<SelectorProps> = ({
201185
autoFocus={autoFocus}
202186
editable={inputEditable}
203187
accessibilityIndex={accessibilityIndex}
204-
value={searchValue}
188+
value={inputValue}
205189
onKeyDown={onInputKeyDown}
206190
onMouseDown={onInputMouseDown}
207191
onChange={onInputChange}
208192
tabIndex={tabIndex}
209193
/>
210194

211195
{/* Measure Node */}
212-
<span
213-
ref={measureRef}
214-
className={`${prefixCls}-selection-search-mirror`}
215-
aria-hidden
216-
>
217-
{searchValue}&nbsp;
196+
<span ref={measureRef} className={`${prefixCls}-selection-search-mirror`} aria-hidden>
197+
{inputValue}&nbsp;
218198
</span>
219199
</span>
220200

221-
{!values.length && !searchValue && (
222-
<span className={`${prefixCls}-selection-placeholder`}>
223-
{placeholder}
224-
</span>
201+
{!values.length && !inputValue && (
202+
<span className={`${prefixCls}-selection-placeholder`}>{placeholder}</span>
225203
)}
226204
</>
227205
);

src/Selector/SingleSelector.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ const SingleSelector: React.FC<SelectorProps> = ({
4040
inputValue = item ? getDisplayValue(item.value) : activeValue || searchValue;
4141
}
4242

43-
const hasTextInput = !!inputValue;
43+
// Not show text when closed expect combobox mode
44+
const hasTextInput = mode !== 'combobox' && !open ? false : !!inputValue;
4445

4546
return (
4647
<>

tests/Multiple.test.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,7 @@ describe('Select.Multiple', () => {
128128

129129
expect(handleChange).toHaveBeenCalledWith(
130130
[1, 2],
131-
[
132-
expect.objectContaining({ value: 1 }),
133-
expect.objectContaining({ value: 2, testprop: 2 }),
134-
],
131+
[expect.objectContaining({ value: 1 }), expect.objectContaining({ value: 2, testprop: 2 })],
135132
);
136133
});
137134

@@ -274,4 +271,11 @@ describe('Select.Multiple', () => {
274271

275272
jest.useRealTimers();
276273
});
274+
275+
it('show placeholder when searchValue is controlled', () => {
276+
const wrapper = mount(<Select mode="multiple" searchValue="light" placeholder="bamboo" />);
277+
expect(wrapper.find('.rc-select-selection-placeholder').length).toBeTruthy();
278+
toggleOpen(wrapper);
279+
expect(wrapper.find('.rc-select-selection-placeholder').length).toBeFalsy();
280+
});
277281
});

0 commit comments

Comments
 (0)