Skip to content

Commit 2219319

Browse files
authored
fix: optionRender usage for empty search results (#636)
* fix: optionRender usage for empty search results * fix: type for empty options array in search test
1 parent bbbb2bf commit 2219319

2 files changed

Lines changed: 66 additions & 2 deletions

File tree

src/OptionList/Column.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ export default function Column<OptionType extends DefaultOptionType = DefaultOpt
223223
/>
224224
)}
225225
<div className={`${menuItemPrefixCls}-content`}>
226-
{optionRender ? optionRender(option) : label}
226+
{optionRender && value !== '__EMPTY__' ? optionRender(option) : label}
227227
</div>
228228
{!isLoading && expandIcon && !isMergedLeaf && (
229229
<div className={`${menuItemPrefixCls}-expand-icon`}>{expandIcon}</div>

tests/search.spec.tsx

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { fireEvent, render } from '@testing-library/react';
22
import KeyCode from '@rc-component/util/lib/KeyCode';
33
import { resetWarned } from '@rc-component/util/lib/warning';
44
import React from 'react';
5-
import Cascader from '../src';
5+
import Cascader, { type DefaultOptionType } from '../src';
66
import { optionsForActiveMenuItems } from './demoOptions';
77
import { expectOpen, doSearch, keyDown } from './util';
88

@@ -444,4 +444,68 @@ describe('Cascader.Search', () => {
444444
fireEvent.click(document.querySelector('.rc-cascader-checkbox') as HTMLElement);
445445
expect(inputNode).toHaveValue('little');
446446
});
447+
448+
it('should display notFoundContent when search has no results with optionRender', () => {
449+
const { container } = render(
450+
<Cascader
451+
options={options}
452+
open
453+
showSearch
454+
optionRender={option => `${option.label} - custom render`}
455+
notFoundContent="未找到相关内容"
456+
/>,
457+
);
458+
459+
// Search for something that doesn't exist
460+
doSearch(container, 'nonexistent');
461+
const itemList = container.querySelectorAll('.rc-cascader-menu-item');
462+
expect(itemList).toHaveLength(1);
463+
// Should display notFoundContent, not use optionRender
464+
expect(itemList[0].textContent).toEqual('未找到相关内容');
465+
expect(itemList[0].querySelector('.rc-cascader-menu-item-content')?.textContent).toEqual(
466+
'未找到相关内容',
467+
);
468+
});
469+
470+
it('should use optionRender for search results when both optionRender and notFoundContent are provided', () => {
471+
const { container } = render(
472+
<Cascader
473+
options={options}
474+
open
475+
showSearch
476+
optionRender={option => `${option.label} - custom render`}
477+
notFoundContent="未找到相关内容"
478+
/>,
479+
);
480+
481+
// Search for something that exists
482+
doSearch(container, 'toy');
483+
const itemList = container.querySelectorAll('.rc-cascader-menu-item');
484+
expect(itemList.length).toBeGreaterThan(0);
485+
486+
// Should use optionRender for actual options
487+
const firstItemContent = itemList[0].querySelector('.rc-cascader-menu-item-content');
488+
expect(firstItemContent?.textContent).toContain('custom render');
489+
expect(firstItemContent?.textContent).not.toEqual('未找到相关内容');
490+
});
491+
492+
it('should display notFoundContent when options array is empty with optionRender', () => {
493+
const { container } = render(
494+
<Cascader
495+
options={[] as DefaultOptionType[]}
496+
open
497+
showSearch
498+
optionRender={option => `${option.label} - custom render`}
499+
notFoundContent="空列表"
500+
/>,
501+
);
502+
503+
const itemList = container.querySelectorAll('.rc-cascader-menu-item');
504+
expect(itemList).toHaveLength(1);
505+
// Should display notFoundContent, not use optionRender
506+
expect(itemList[0].textContent).toEqual('空列表');
507+
expect(itemList[0].querySelector('.rc-cascader-menu-item-content')?.textContent).toEqual(
508+
'空列表',
509+
);
510+
});
447511
});

0 commit comments

Comments
 (0)