Skip to content

Commit 810313b

Browse files
zombieJclaude
andcommitted
fix: use strict null/undefined check for display value in single select
Empty string values should be treated as valid values (having a display value), not as missing values. This fixes the content-has-value class not being applied when the value is an empty string. Co-Authored-By: Claude Opus 4.6 <[email protected]>
1 parent 87e9941 commit 810313b

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/SelectInput/Content/SingleContent.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const SingleContent = React.forwardRef<HTMLInputElement, SharedContentProps>(
1919

2020
const combobox = mode === 'combobox';
2121
const displayValue = displayValues[0];
22+
const hasDisplayValue = displayValue !== null && displayValue !== undefined;
2223

2324
// Implement the same logic as the old SingleSelector
2425
const mergedSearchValue = React.useMemo(() => {
@@ -34,7 +35,7 @@ const SingleContent = React.forwardRef<HTMLInputElement, SharedContentProps>(
3435
let style: React.CSSProperties | undefined;
3536
let titleValue: string | undefined;
3637

37-
if (displayValue && selectContext?.flattenOptions) {
38+
if (hasDisplayValue && selectContext?.flattenOptions) {
3839
const option = selectContext.flattenOptions.find((opt) => opt.value === displayValue.value);
3940
if (option?.data) {
4041
className = option.data.className;
@@ -43,7 +44,7 @@ const SingleContent = React.forwardRef<HTMLInputElement, SharedContentProps>(
4344
}
4445
}
4546

46-
if (displayValue && !titleValue) {
47+
if (hasDisplayValue && !titleValue) {
4748
titleValue = getTitle(displayValue);
4849
}
4950

@@ -64,7 +65,7 @@ const SingleContent = React.forwardRef<HTMLInputElement, SharedContentProps>(
6465

6566
// ========================== Render ==========================
6667
// Render value
67-
const renderValue = displayValue ? (
68+
const renderValue = hasDisplayValue ? (
6869
hasOptionStyle ? (
6970
<div
7071
className={clsx(`${prefixCls}-content-value`, optionClassName)}
@@ -88,7 +89,7 @@ const SingleContent = React.forwardRef<HTMLInputElement, SharedContentProps>(
8889
<div
8990
className={clsx(
9091
`${prefixCls}-content`,
91-
displayValue && `${prefixCls}-content-has-value`,
92+
hasDisplayValue && `${prefixCls}-content-has-value`,
9293
mergedSearchValue && `${prefixCls}-content-has-search-value`,
9394
hasOptionStyle && `${prefixCls}-content-has-option-style`,
9495
classNames?.content,

tests/placeholder.test.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,11 @@ describe('Select placeholder', () => {
4040
});
4141
expect(container.querySelector('.rc-select-placeholder').textContent).toBe('placeholder');
4242
});
43+
44+
it('should have content-has-value class when value is empty string', () => {
45+
const { container } = render(
46+
<Select value="" placeholder="placeholder" options={[{ value: '', label: '' }]} />,
47+
);
48+
expect(container.querySelector('.rc-select-content-has-value')).toBeTruthy();
49+
});
4350
});

0 commit comments

Comments
 (0)