Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/SelectInput/Content/SingleContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ const SingleContent = React.forwardRef<HTMLInputElement, SharedContentProps>(
}, [combobox, activeValue]);

// ========================== Render ==========================
const showHasValueCls =
displayValue &&
displayValue.label !== null &&
displayValue.label !== undefined &&
String(displayValue.label).trim() !== '';

Comment on lines +66 to +71
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The logic to determine showHasValueCls can be simplified for better readability. The !! and extra parentheses are redundant, != null can be used to check for both null and undefined, and optional chaining (?.) can make the code more concise.

    const showHasValueCls =
      displayValue?.label != null && String(displayValue.label).trim() !== '';

// Render value
const renderValue = displayValue ? (
hasOptionStyle ? (
Expand All @@ -88,11 +94,7 @@ const SingleContent = React.forwardRef<HTMLInputElement, SharedContentProps>(
<div
className={clsx(
`${prefixCls}-content`,
displayValue &&
displayValue.label !== null &&
displayValue.label !== undefined &&
displayValue.label !== '' &&
`${prefixCls}-content-has-value`,
showHasValueCls && `${prefixCls}-content-has-value`,
mergedSearchValue && `${prefixCls}-content-has-search-value`,
hasOptionStyle && `${prefixCls}-content-has-option-style`,
classNames?.content,
Expand Down
11 changes: 10 additions & 1 deletion tests/Select.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,21 @@ describe('Select.Basic', () => {
const { container } = render(
<Select value={0}>
<Option value={0}>0</Option>
<Option value="1">1</Option>
<Option value={1}>1</Option>
</Select>,
);
expect(container.querySelector('.rc-select-content-has-value')).toBeTruthy();
});

it('should not add -content-has-value className when value is whitespace string', () => {
const { container } = render(
<Select value=" ">
<Option value="1">One</Option>
</Select>,
);
expect(container.querySelector('.rc-select-content-has-value')).toBeFalsy();
});

it('should default select the right option', () => {
const { container } = render(
<Select defaultValue="2">
Expand Down
Loading