Skip to content

Commit ed52b4e

Browse files
committed
fix: handle whitespace-only values in content-has-value class
1 parent 3924823 commit ed52b4e

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

src/SelectInput/Content/SingleContent.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ const SingleContent = React.forwardRef<HTMLInputElement, SharedContentProps>(
6363
}, [combobox, activeValue]);
6464

6565
// ========================== Render ==========================
66+
const showHasValueCls = !!(
67+
displayValue &&
68+
displayValue.label !== null &&
69+
displayValue.label !== undefined &&
70+
String(displayValue.label).trim() !== ''
71+
);
72+
6673
// Render value
6774
const renderValue = displayValue ? (
6875
hasOptionStyle ? (
@@ -88,11 +95,7 @@ const SingleContent = React.forwardRef<HTMLInputElement, SharedContentProps>(
8895
<div
8996
className={clsx(
9097
`${prefixCls}-content`,
91-
displayValue &&
92-
displayValue.label !== null &&
93-
displayValue.label !== undefined &&
94-
displayValue.label !== '' &&
95-
`${prefixCls}-content-has-value`,
98+
showHasValueCls && `${prefixCls}-content-has-value`,
9699
mergedSearchValue && `${prefixCls}-content-has-search-value`,
97100
hasOptionStyle && `${prefixCls}-content-has-option-style`,
98101
classNames?.content,

tests/Select.test.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,12 +286,21 @@ describe('Select.Basic', () => {
286286
const { container } = render(
287287
<Select value={0}>
288288
<Option value={0}>0</Option>
289-
<Option value="1">1</Option>
289+
<Option value={1}>1</Option>
290290
</Select>,
291291
);
292292
expect(container.querySelector('.rc-select-content-has-value')).toBeTruthy();
293293
});
294294

295+
it('should not add -content-has-value className when value is whitespace string', () => {
296+
const { container } = render(
297+
<Select value=" ">
298+
<Option value="1">One</Option>
299+
</Select>,
300+
);
301+
expect(container.querySelector('.rc-select-content-has-value')).toBeFalsy();
302+
});
303+
295304
it('should default select the right option', () => {
296305
const { container } = render(
297306
<Select defaultValue="2">

0 commit comments

Comments
 (0)