Skip to content

Commit

Permalink
chore: memo of it (#407)
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ authored Apr 17, 2023
1 parent f489dd9 commit 1d91a86
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 15 deletions.
8 changes: 5 additions & 3 deletions examples/search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ const addressOptions = [
value: 'fuzhou',
children: [
{
label: '马尾',
label: '马尾-mw',
value: 'mawei',
},
],
},
{
label: '泉州',
label: '泉州-qz',
value: 'quanzhou',
},
],
Expand Down Expand Up @@ -68,7 +68,9 @@ const addressOptions = [

class Demo extends React.Component {
render() {
return <Cascader options={addressOptions} showSearch />;
return (
<Cascader options={addressOptions} showSearch style={{ width: 300 }} animation="slide-up" />
);
}
}

Expand Down
17 changes: 17 additions & 0 deletions src/OptionList/CacheContent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as React from 'react';

export interface CacheContentProps {
children?: React.ReactNode;
open?: boolean;
}

const CacheContent = React.memo(
({ children }: CacheContentProps) => children as React.ReactElement,
(_, next) => !next.open,
);

if (process.env.NODE_ENV !== 'production') {
CacheContent.displayName = 'CacheContent';
}

export default CacheContent;
23 changes: 13 additions & 10 deletions src/OptionList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ import {
toPathValueStr,
} from '../utils/commonUtil';
import { toPathOptions } from '../utils/treeUtil';
import CacheContent from './CacheContent';
import Column, { FIX_LABEL } from './Column';
import useActive from './useActive';
import useKeyboard from './useKeyboard';

const RefOptionList = React.forwardRef<RefOptionListProps>((props, ref) => {
const { prefixCls, multiple, searchValue, toggleOpen, notFoundContent, direction } =
const { prefixCls, multiple, searchValue, toggleOpen, notFoundContent, direction, open } =
useBaseProps();

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

// >>>>> Render
return (
<div
className={classNames(`${mergedPrefixCls}-menus`, {
[`${mergedPrefixCls}-menu-empty`]: isEmpty,
[`${mergedPrefixCls}-rtl`]: rtl,
})}
ref={containerRef}
>
{columnNodes}
</div>
<CacheContent open={open}>
<div
className={classNames(`${mergedPrefixCls}-menus`, {
[`${mergedPrefixCls}-menu-empty`]: isEmpty,
[`${mergedPrefixCls}-rtl`]: rtl,
})}
ref={containerRef}
>
{columnNodes}
</div>
</CacheContent>
);
});

Expand Down
24 changes: 22 additions & 2 deletions tests/search.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/* eslint-disable @typescript-eslint/consistent-type-imports */

import React from 'react';
import { fireEvent, render } from '@testing-library/react';
import KeyCode from 'rc-util/lib/KeyCode';
import { resetWarned } from 'rc-util/lib/warning';
import { mount, ReactWrapper } from './enzyme';
import React from 'react';
import Cascader from '../src';
import { optionsForActiveMenuItems } from './demoOptions';
import { mount, ReactWrapper } from './enzyme';

describe('Cascader.Search', () => {
function doSearch(wrapper: ReactWrapper, search: string) {
Expand Down Expand Up @@ -244,4 +245,23 @@ describe('Cascader.Search', () => {
wrapper.find('input').simulate('change', { target: { value: 'z' } });
expect(wrapper.render()).toMatchSnapshot();
});

// https://github.com/ant-design/ant-design/issues/41810
it('not back to options when selected', () => {
const { container } = render(<Cascader options={options} showSearch />);

// Search
fireEvent.change(container.querySelector('input'), {
target: {
value: 'bamboo',
},
});

// Click
fireEvent.click(document.querySelector('.rc-cascader-menu-item-content'));
expect(document.querySelector('.rc-cascader-dropdown-hidden')).toBeTruthy();
expect(document.querySelector('.rc-cascader-menu-item-content').textContent).toBe(
'Label Bamboo / Label Little / Toy Fish',
);
});
});

1 comment on commit 1d91a86

@vercel
Copy link

@vercel vercel bot commented on 1d91a86 Apr 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

cascader – ./

cascader-react-component.vercel.app
cascader-git-master-react-component.vercel.app

Please sign in to comment.