Skip to content

feat: optimize search #530

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 2, 2024
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
8 changes: 8 additions & 0 deletions docs/demo/multiple-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: multiple-search
nav:
title: Demo
path: /demo
---

<code src="../../examples/multiple-search.tsx"></code>
48 changes: 48 additions & 0 deletions examples/multiple-search.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import React from 'react';
import '../assets/index.less';
import Cascader from '../src';

const options = [
{
value: 'zhejiang',
label: 'Zhejiang',
children: [
{
value: 'hangzhou',
label: 'Hangzhou',
children: [
{
value: 'xihu',
label: 'West Lake',
},
{
value: 'xiasha',
label: 'Xia Sha',
},
],
},
],
},
{
value: 'jiangsu',
label: 'Jiangsu',
children: [
{
value: 'nanjing',
label: 'Nanjing',
children: [
{
value: 'zhonghuamen',
label: 'Zhong Hua men',
},
],
},
],
},
];

const Demo = () => {
return <Cascader checkable showSearch options={options} />;
};

export default Demo;
2 changes: 1 addition & 1 deletion src/Cascader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ const Cascader = React.forwardRef<CascaderRef, InternalCascaderProps>((props, re
mergedFieldNames,
dropdownPrefixCls || prefixCls,
searchConfig,
changeOnSelect,
changeOnSelect || multiple,
);

// =========================== Values ===========================
Expand Down
12 changes: 7 additions & 5 deletions src/hooks/useSearchOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ const defaultFilter: ShowSearchType['filter'] = (search, options, { label = '' }
const defaultRender: ShowSearchType['render'] = (inputValue, path, prefixCls, fieldNames) =>
path.map(opt => opt[fieldNames.label as string]).join(' / ');

export default (
const useSearchOptions = (
search: string,
options: DefaultOptionType[],
fieldNames: InternalFieldNames,
prefixCls: string,
config: ShowSearchType,
changeOnSelect?: boolean,
enableHalfPath?: boolean,
) => {
const { filter = defaultFilter, render = defaultRender, limit = 50, sort } = config;

Expand Down Expand Up @@ -46,8 +46,8 @@ export default (
// If is leaf option
!children ||
children.length === 0 ||
// If is changeOnSelect
changeOnSelect
// If is changeOnSelect or multiple
enableHalfPath
) {
if (filter(search, connectedPathOptions, { label: fieldNames.label })) {
filteredOptions.push({
Expand Down Expand Up @@ -87,5 +87,7 @@ export default (
return limit !== false && limit > 0
? filteredOptions.slice(0, limit as number)
: filteredOptions;
}, [search, options, fieldNames, prefixCls, render, changeOnSelect, filter, sort, limit]);
}, [search, options, fieldNames, prefixCls, render, enableHalfPath, filter, sort, limit]);
};

export default useSearchOptions;
8 changes: 8 additions & 0 deletions tests/search.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,14 @@ describe('Cascader.Search', () => {
wrapper.find('.rc-cascader-menu-item').first().simulate('click');
wrapper.find('.rc-cascader-menu-item').first().simulate('mousedown');
expect(onChange).toHaveBeenCalledWith([['bamboo', 'little', 'fish']], expect.anything());

doSearch(wrapper, 'light');
wrapper.find('.rc-cascader-menu-item').first().simulate('click');
wrapper.find('.rc-cascader-menu-item').first().simulate('mousedown');
expect(onChange).toHaveBeenCalledWith(
[['bamboo', 'little', 'fish'], ['light']],
expect.anything(),
);
});

it('should not crash when exist options with same value on different levels', () => {
Expand Down
Loading