Skip to content

Commit

Permalink
Add displayInInput option
Browse files Browse the repository at this point in the history
  • Loading branch information
ajenkins-mparticle committed Nov 28, 2024
1 parent 751451f commit d40559e
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 2 deletions.
8 changes: 8 additions & 0 deletions docs/demo/external-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: external search
nav:
title: Demo
path: /demo
---

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

const addressOptions = [
{
label: '福建',
value: 'fj',
children: [
{
label: '福州',
value: 'fuzhou',
children: [
{
label: '马尾-mw',
value: 'mawei',
},
],
},
{
label: '泉州-qz',
value: 'quanzhou',
},
],
},
{
label: '浙江',
value: 'zj',
children: [
{
label: '杭州',
value: 'hangzhou',
children: [
{
label: '余杭',
value: 'yuhang',
},
{
label: '福州',
value: 'fuzhou',
children: [
{
label: '马尾',
value: 'mawei',
},
],
},
],
},
],
},
{
label: '北京',
value: 'bj',
children: [
{
label: '朝阳区',
value: 'chaoyang',
},
{
label: '海淀区',
value: 'haidian',
},
],
},
];

const Demo = () => {
const [searchValue, setSearchValue] = useState('');
return (
<>
<Cascader
options={addressOptions}
showSearch={{ displayInInput: false }}
searchValue={searchValue}
style={{ width: 300 }}
dropdownRender={menu => {
return (
<div>
<input
value={searchValue}
onChange={e => setSearchValue(e.target.value)}
placeholder="External Search"
/>
{menu}
</div>
);
}}
animation="slide-up"
notFoundContent="Empty Content!"
/>
</>
);
};

export default Demo;
1 change: 1 addition & 0 deletions src/Cascader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export interface ShowSearchType<
inputValue: string,
fieldNames: FieldNames<OptionType, ValueField>,
) => number;
displayInInput?: boolean;
matchInputWidth?: boolean;
limit?: number | false;
}
Expand Down
3 changes: 2 additions & 1 deletion src/hooks/useSearchConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default function useSearchConfig(showSearch?: CascaderProps['showSearch']
let searchConfig: ShowSearchType = {
matchInputWidth: true,
limit: 50,
displayInInput: true,
};

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

return [true, searchConfig];
return [!!searchConfig.displayInInput, searchConfig];
}, [showSearch]);
}
45 changes: 44 additions & 1 deletion tests/search.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { fireEvent, render } from '@testing-library/react';
import KeyCode from 'rc-util/lib/KeyCode';
import { resetWarned } from 'rc-util/lib/warning';
import React from 'react';
import React, { useState } from 'react';
import Cascader from '../src';
import { optionsForActiveMenuItems } from './demoOptions';
import type { ReactWrapper } from './enzyme';
Expand Down Expand Up @@ -73,6 +73,49 @@ describe('Cascader.Search', () => {
expect(onChange).toHaveBeenCalledWith(['bamboo', 'little', 'fish'], expect.anything());
});

it('externally controlled search',() => {
const onSearch = jest.fn();
const onChange = jest.fn();

function doExternalSearch(wrapper: ReactWrapper, search: string) {
wrapper.find('input[data-test="external-search"]').simulate('change', {
target: {
value: search,
},
});
}


const ExternallyControlledSearch = () => {
const [searchValue,setSearchValue] = useState('')
return (
<>
<input data-test="external-search" value={searchValue} onChange={e => setSearchValue(e.target.value)} />
<Cascader options={options} searchValue={searchValue} onChange={onChange} onSearch={onSearch} open showSearch={{displayInInput:false}} />,
</>
);
}
const wrapper = mount(<ExternallyControlledSearch/>)

// Leaf
doExternalSearch(wrapper, 'toy');
let itemList = wrapper.find('div.rc-cascader-menu-item-content');
expect(itemList).toHaveLength(2);
expect(itemList.at(0).text()).toEqual('Label Bamboo / Label Little / Toy Fish');
expect(itemList.at(1).text()).toEqual('Label Bamboo / Label Little / Toy Cards');

// Parent
doExternalSearch(wrapper, 'Label Little');
itemList = wrapper.find('div.rc-cascader-menu-item-content');
expect(itemList).toHaveLength(2);
expect(itemList.at(0).text()).toEqual('Label Bamboo / Label Little / Toy Fish');
expect(itemList.at(1).text()).toEqual('Label Bamboo / Label Little / Toy Cards');

// Change
wrapper.clickOption(0, 0);
expect(onChange).toHaveBeenCalledWith(['bamboo', 'little', 'fish'], expect.anything());
})

Check notice

Code scanning / CodeQL

Semicolon insertion Note test

Avoid automated semicolon insertion (94% of all statements in
the enclosing function
have an explicit semicolon).

it('changeOnSelect', () => {
const onChange = jest.fn();
const wrapper = mount(
Expand Down

0 comments on commit d40559e

Please sign in to comment.