Skip to content

Commit b69ab40

Browse files
Add displayInInput option
1 parent 751451f commit b69ab40

File tree

5 files changed

+151
-2
lines changed

5 files changed

+151
-2
lines changed

docs/demo/external-search.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
title: search
3+
nav:
4+
title: Demo
5+
path: /demo
6+
---
7+
8+
<code src="../../examples/external-search.tsx"></code>

examples/external-search.tsx

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import React, { useState } from 'react';
2+
import '../assets/index.less';
3+
import Cascader from '../src';
4+
5+
const addressOptions = [
6+
{
7+
label: '福建',
8+
value: 'fj',
9+
children: [
10+
{
11+
label: '福州',
12+
value: 'fuzhou',
13+
children: [
14+
{
15+
label: '马尾-mw',
16+
value: 'mawei',
17+
},
18+
],
19+
},
20+
{
21+
label: '泉州-qz',
22+
value: 'quanzhou',
23+
},
24+
],
25+
},
26+
{
27+
label: '浙江',
28+
value: 'zj',
29+
children: [
30+
{
31+
label: '杭州',
32+
value: 'hangzhou',
33+
children: [
34+
{
35+
label: '余杭',
36+
value: 'yuhang',
37+
},
38+
{
39+
label: '福州',
40+
value: 'fuzhou',
41+
children: [
42+
{
43+
label: '马尾',
44+
value: 'mawei',
45+
},
46+
],
47+
},
48+
],
49+
},
50+
],
51+
},
52+
{
53+
label: '北京',
54+
value: 'bj',
55+
children: [
56+
{
57+
label: '朝阳区',
58+
value: 'chaoyang',
59+
},
60+
{
61+
label: '海淀区',
62+
value: 'haidian',
63+
},
64+
],
65+
},
66+
];
67+
68+
const Demo = () => {
69+
const [searchValue, setSearchValue] = useState('');
70+
return (
71+
<>
72+
<Cascader
73+
options={addressOptions}
74+
showSearch={{ displayInInput: false }}
75+
searchValue={searchValue}
76+
style={{ width: 300 }}
77+
dropdownRender={menu => {
78+
return (
79+
<div>
80+
<input
81+
value={searchValue}
82+
onChange={e => setSearchValue(e.target.value)}
83+
placeholder="External Search"
84+
/>
85+
{menu}
86+
</div>
87+
);
88+
}}
89+
animation="slide-up"
90+
notFoundContent="Empty Content!"
91+
/>
92+
</>
93+
);
94+
};
95+
96+
export default Demo;

src/Cascader.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export interface ShowSearchType<
5757
inputValue: string,
5858
fieldNames: FieldNames<OptionType, ValueField>,
5959
) => number;
60+
displayInInput?: boolean;
6061
matchInputWidth?: boolean;
6162
limit?: number | false;
6263
}

src/hooks/useSearchConfig.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export default function useSearchConfig(showSearch?: CascaderProps['showSearch']
1212
let searchConfig: ShowSearchType = {
1313
matchInputWidth: true,
1414
limit: 50,
15+
displayInInput: true,
1516
};
1617

1718
if (showSearch && typeof showSearch === 'object') {
@@ -29,6 +30,6 @@ export default function useSearchConfig(showSearch?: CascaderProps['showSearch']
2930
}
3031
}
3132

32-
return [true, searchConfig];
33+
return [!!searchConfig.displayInInput, searchConfig];
3334
}, [showSearch]);
3435
}

tests/search.spec.tsx

+44-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { fireEvent, render } from '@testing-library/react';
22
import KeyCode from 'rc-util/lib/KeyCode';
33
import { resetWarned } from 'rc-util/lib/warning';
4-
import React from 'react';
4+
import React, { useState } from 'react';
55
import Cascader from '../src';
66
import { optionsForActiveMenuItems } from './demoOptions';
77
import type { ReactWrapper } from './enzyme';
@@ -73,6 +73,49 @@ describe('Cascader.Search', () => {
7373
expect(onChange).toHaveBeenCalledWith(['bamboo', 'little', 'fish'], expect.anything());
7474
});
7575

76+
it('externally controlled search',() => {
77+
const onSearch = jest.fn();
78+
const onChange = jest.fn();
79+
80+
function doExternalSearch(wrapper: ReactWrapper, search: string) {
81+
wrapper.find('input[data-test="external-search"]').simulate('change', {
82+
target: {
83+
value: search,
84+
},
85+
});
86+
}
87+
88+
89+
const ExternallyControlledSearch = () => {
90+
const [searchValue,setSearchValue] = useState('')
91+
return (
92+
<>
93+
<input data-test="external-search" value={searchValue} onChange={e => setSearchValue(e.target.value)} />
94+
<Cascader options={options} searchValue={searchValue} onChange={onChange} onSearch={onSearch} open showSearch={{displayInInput:false}} />,
95+
</>
96+
);
97+
}
98+
const wrapper = mount(<ExternallyControlledSearch/>)
99+
100+
// Leaf
101+
doExternalSearch(wrapper, 'toy');
102+
let itemList = wrapper.find('div.rc-cascader-menu-item-content');
103+
expect(itemList).toHaveLength(2);
104+
expect(itemList.at(0).text()).toEqual('Label Bamboo / Label Little / Toy Fish');
105+
expect(itemList.at(1).text()).toEqual('Label Bamboo / Label Little / Toy Cards');
106+
107+
// Parent
108+
doExternalSearch(wrapper, 'Label Little');
109+
itemList = wrapper.find('div.rc-cascader-menu-item-content');
110+
expect(itemList).toHaveLength(2);
111+
expect(itemList.at(0).text()).toEqual('Label Bamboo / Label Little / Toy Fish');
112+
expect(itemList.at(1).text()).toEqual('Label Bamboo / Label Little / Toy Cards');
113+
114+
// Change
115+
wrapper.clickOption(0, 0);
116+
expect(onChange).toHaveBeenCalledWith(['bamboo', 'little', 'fish'], expect.anything());
117+
})
118+
76119
it('changeOnSelect', () => {
77120
const onChange = jest.fn();
78121
const wrapper = mount(

0 commit comments

Comments
 (0)