diff --git a/src/BaseSelect/index.tsx b/src/BaseSelect/index.tsx index c2dea8640..63b71236c 100644 --- a/src/BaseSelect/index.tsx +++ b/src/BaseSelect/index.tsx @@ -1,6 +1,12 @@ import type { AlignType, BuildInPlacements } from '@rc-component/trigger'; import { clsx } from 'clsx'; -import { type AllowClearConfig, getDOM, isNonNullable, useEvent } from '@rc-component/util'; +import { + type AllowClearConfig, + getDOM, + isNonNullable, + isReactRenderable, + useEvent, +} from '@rc-component/util'; import type { ScrollConfig, ScrollTo } from '@rc-component/virtual-list'; import * as React from 'react'; import { useAllowClear } from '../hooks/useAllowClear'; @@ -360,7 +366,7 @@ const BaseSelect = React.forwardRef((props, ref) // ============================== Open ============================== // Not trigger `open` when `notFoundContent` is empty - const emptyListContent = !notFoundContent && emptyOptions; + const emptyListContent = !isReactRenderable(notFoundContent) && emptyOptions; const [rawOpen, mergedOpen, triggerOpen, lockOptions] = useOpen( defaultOpen || false, diff --git a/src/OptionList.tsx b/src/OptionList.tsx index 8740a4e9a..a151c8658 100644 --- a/src/OptionList.tsx +++ b/src/OptionList.tsx @@ -1,5 +1,5 @@ import { clsx } from 'clsx'; -import { KeyCode, omit, pickAttrs, useMemo } from '@rc-component/util'; +import { isReactRenderable, KeyCode, omit, pickAttrs, useMemo } from '@rc-component/util'; import List, { type ListRef, type ScrollConfig } from '@rc-component/virtual-list'; import * as React from 'react'; import { useEffect } from 'react'; @@ -450,10 +450,12 @@ const OptionList: React.ForwardRefRenderFunction = (_, r const mergedLabel = getLabel(item); const iconVisible = - !menuItemSelectedIcon || typeof menuItemSelectedIcon === 'function' || selected; + !isReactRenderable(menuItemSelectedIcon) || + typeof menuItemSelectedIcon === 'function' || + selected; // https://github.com/ant-design/ant-design/issues/34145 - const content = typeof mergedLabel === 'number' ? mergedLabel : mergedLabel || value; + const content = isReactRenderable(mergedLabel) ? mergedLabel : value; // https://github.com/ant-design/ant-design/issues/26717 let optionTitle = isTitleType(content) ? content.toString() : undefined; if (title !== undefined) { diff --git a/src/Select.tsx b/src/Select.tsx index 9735154fe..167578e7b 100644 --- a/src/Select.tsx +++ b/src/Select.tsx @@ -369,10 +369,7 @@ const Select = React.forwardRef( // ========================== Render ========================== const showHasValueCls = - displayValue && - displayValue.label !== null && - displayValue.label !== undefined && - String(displayValue.label).trim() !== ''; + displayValue && isNonNullable(displayValue.label) && String(displayValue.label).trim() !== ''; // Render value // Only render value when not using custom input in combobox mode diff --git a/tests/renderability.test.tsx b/tests/renderability.test.tsx new file mode 100644 index 000000000..b01e7db43 --- /dev/null +++ b/tests/renderability.test.tsx @@ -0,0 +1,37 @@ +import React from 'react'; +import { render } from '@testing-library/react'; +import Select from '../src'; + +it('opens an empty dropdown containing zero', () => { + const { baseElement } = render(, + ); + expect(baseElement.querySelectorAll('.rc-select-item-option-state')).toHaveLength(1); + expect( + baseElement.querySelector('.rc-select-item-option-selected .rc-select-item-option-state') + .textContent, + ).toBe('0'); +}); + +it('keeps a zero option label in the dropdown and selected value', () => { + const { container, baseElement } = render( +