Skip to content

Commit 1d91a86

Browse files
authored
chore: memo of it (#407)
1 parent f489dd9 commit 1d91a86

File tree

4 files changed

+57
-15
lines changed

4 files changed

+57
-15
lines changed

examples/search.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ const addressOptions = [
1313
value: 'fuzhou',
1414
children: [
1515
{
16-
label: '马尾',
16+
label: '马尾-mw',
1717
value: 'mawei',
1818
},
1919
],
2020
},
2121
{
22-
label: '泉州',
22+
label: '泉州-qz',
2323
value: 'quanzhou',
2424
},
2525
],
@@ -68,7 +68,9 @@ const addressOptions = [
6868

6969
class Demo extends React.Component {
7070
render() {
71-
return <Cascader options={addressOptions} showSearch />;
71+
return (
72+
<Cascader options={addressOptions} showSearch style={{ width: 300 }} animation="slide-up" />
73+
);
7274
}
7375
}
7476

src/OptionList/CacheContent.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import * as React from 'react';
2+
3+
export interface CacheContentProps {
4+
children?: React.ReactNode;
5+
open?: boolean;
6+
}
7+
8+
const CacheContent = React.memo(
9+
({ children }: CacheContentProps) => children as React.ReactElement,
10+
(_, next) => !next.open,
11+
);
12+
13+
if (process.env.NODE_ENV !== 'production') {
14+
CacheContent.displayName = 'CacheContent';
15+
}
16+
17+
export default CacheContent;

src/OptionList/index.tsx

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ import {
1414
toPathValueStr,
1515
} from '../utils/commonUtil';
1616
import { toPathOptions } from '../utils/treeUtil';
17+
import CacheContent from './CacheContent';
1718
import Column, { FIX_LABEL } from './Column';
1819
import useActive from './useActive';
1920
import useKeyboard from './useKeyboard';
2021

2122
const RefOptionList = React.forwardRef<RefOptionListProps>((props, ref) => {
22-
const { prefixCls, multiple, searchValue, toggleOpen, notFoundContent, direction } =
23+
const { prefixCls, multiple, searchValue, toggleOpen, notFoundContent, direction, open } =
2324
useBaseProps();
2425

2526
const containerRef = React.useRef<HTMLDivElement>();
@@ -213,15 +214,17 @@ const RefOptionList = React.forwardRef<RefOptionListProps>((props, ref) => {
213214

214215
// >>>>> Render
215216
return (
216-
<div
217-
className={classNames(`${mergedPrefixCls}-menus`, {
218-
[`${mergedPrefixCls}-menu-empty`]: isEmpty,
219-
[`${mergedPrefixCls}-rtl`]: rtl,
220-
})}
221-
ref={containerRef}
222-
>
223-
{columnNodes}
224-
</div>
217+
<CacheContent open={open}>
218+
<div
219+
className={classNames(`${mergedPrefixCls}-menus`, {
220+
[`${mergedPrefixCls}-menu-empty`]: isEmpty,
221+
[`${mergedPrefixCls}-rtl`]: rtl,
222+
})}
223+
ref={containerRef}
224+
>
225+
{columnNodes}
226+
</div>
227+
</CacheContent>
225228
);
226229
});
227230

tests/search.spec.tsx

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
/* eslint-disable @typescript-eslint/consistent-type-imports */
22

3-
import React from 'react';
3+
import { fireEvent, render } from '@testing-library/react';
44
import KeyCode from 'rc-util/lib/KeyCode';
55
import { resetWarned } from 'rc-util/lib/warning';
6-
import { mount, ReactWrapper } from './enzyme';
6+
import React from 'react';
77
import Cascader from '../src';
88
import { optionsForActiveMenuItems } from './demoOptions';
9+
import { mount, ReactWrapper } from './enzyme';
910

1011
describe('Cascader.Search', () => {
1112
function doSearch(wrapper: ReactWrapper, search: string) {
@@ -244,4 +245,23 @@ describe('Cascader.Search', () => {
244245
wrapper.find('input').simulate('change', { target: { value: 'z' } });
245246
expect(wrapper.render()).toMatchSnapshot();
246247
});
248+
249+
// https://github.com/ant-design/ant-design/issues/41810
250+
it('not back to options when selected', () => {
251+
const { container } = render(<Cascader options={options} showSearch />);
252+
253+
// Search
254+
fireEvent.change(container.querySelector('input'), {
255+
target: {
256+
value: 'bamboo',
257+
},
258+
});
259+
260+
// Click
261+
fireEvent.click(document.querySelector('.rc-cascader-menu-item-content'));
262+
expect(document.querySelector('.rc-cascader-dropdown-hidden')).toBeTruthy();
263+
expect(document.querySelector('.rc-cascader-menu-item-content').textContent).toBe(
264+
'Label Bamboo / Label Little / Toy Fish',
265+
);
266+
});
247267
});

0 commit comments

Comments
 (0)