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
2 changes: 1 addition & 1 deletion src/SelectInput/Content/SingleContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ const SingleContent = React.forwardRef<HTMLInputElement, SharedContentProps>(
<div
className={clsx(
`${prefixCls}-content`,
displayValue && `${prefixCls}-content-has-value`,
displayValue && displayValue.label && `${prefixCls}-content-has-value`,
Copy link
Contributor

Choose a reason for hiding this comment

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

high

目前的条件 displayValue && displayValue.label 会错误地将 0 视为假值,这意味着当 Select 的值为 0 时,.rc-select-content-has-value 这个 class 不会被应用,可能导致样式不正确。

一个更完善的条件应该能确保 0 被视为有效标签,同时正确处理空字符串的情况。

Suggested change
displayValue && displayValue.label && `${prefixCls}-content-has-value`,
displayValue && (displayValue.label || displayValue.label === 0) && `${prefixCls}-content-has-value`,

mergedSearchValue && `${prefixCls}-content-has-search-value`,
hasOptionStyle && `${prefixCls}-content-has-option-style`,
classNames?.content,
Expand Down
9 changes: 9 additions & 0 deletions tests/Select.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,15 @@ describe('Select.Basic', () => {
expect(container.querySelector('.rc-select-content-has-option-style')).toBeTruthy();
});

it('should not add -content-has-value className when value is empty string', () => {
const { container } = render(
<Select value="">
<Option value="1">One</Option>
</Select>,
);
expect(container.querySelector('.rc-select-content-has-value')).toBeFalsy();
});
Comment on lines +276 to +283
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

针对空字符串值的测试用例很好。为了防止回归并确保数值 0 被正确处理,建议也为 value={0} 添加一个测试用例。SingleContent.tsx 中当前的修复存在一个 bug,即 0 会被视为一个空值。在修复该问题后,添加如下测试用例会很有价值:

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


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