Skip to content

Commit c2cce2c

Browse files
authored
fix: Cache option for filter (#637)
* fix: Cache option for filter * chore: fix logic * chore: fix ts
1 parent 7ebd481 commit c2cce2c

File tree

4 files changed

+25
-34
lines changed

4 files changed

+25
-34
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"test": "rc-test"
4444
},
4545
"dependencies": {
46-
"@rc-component/select": "~1.5.0",
46+
"@rc-component/select": "~1.5.2",
4747
"@rc-component/tree": "~1.2.0",
4848
"@rc-component/util": "^1.4.0",
4949
"clsx": "^2.1.1"

src/OptionList/CacheContent.tsx

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/OptionList/List.tsx

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { clsx } from 'clsx';
33
import type { useBaseProps } from '@rc-component/select';
44
import type { RefOptionListProps } from '@rc-component/select/lib/OptionList';
55
import * as React from 'react';
6+
import useMemo from '@rc-component/util/lib/hooks/useMemo';
67
import type { DefaultOptionType, LegacyKey, SingleValueType } from '../Cascader';
78
import CascaderContext from '../context';
89
import {
@@ -14,7 +15,6 @@ import {
1415
toPathValueStr,
1516
} from '../utils/commonUtil';
1617
import { toPathOptions } from '../utils/treeUtil';
17-
import CacheContent from './CacheContent';
1818
import Column, { FIX_LABEL } from './Column';
1919
import useActive from './useActive';
2020
import useKeyboard from './useKeyboard';
@@ -29,7 +29,9 @@ export type RawOptionListProps = Pick<
2929
| 'direction'
3030
| 'open'
3131
| 'disabled'
32-
>;
32+
> & {
33+
lockOptions?: boolean;
34+
};
3335

3436
const RawOptionList = React.forwardRef<RefOptionListProps, RawOptionListProps>((props, ref) => {
3537
const {
@@ -41,6 +43,7 @@ const RawOptionList = React.forwardRef<RefOptionListProps, RawOptionListProps>((
4143
direction,
4244
open,
4345
disabled,
46+
lockOptions = false,
4447
} = props;
4548

4649
const containerRef = React.useRef<HTMLDivElement>(null);
@@ -135,14 +138,21 @@ const RawOptionList = React.forwardRef<RefOptionListProps, RawOptionListProps>((
135138
};
136139

137140
// ========================== Option ==========================
138-
const mergedOptions = React.useMemo(() => {
141+
const filteredOptions = React.useMemo(() => {
139142
if (searchValue) {
140143
return searchOptions;
141144
}
142145

143146
return options;
144147
}, [searchValue, searchOptions, options]);
145148

149+
// Update only when open or lockOptions
150+
const mergedOptions = useMemo(
151+
() => filteredOptions,
152+
[open, lockOptions],
153+
(prev, next) => !!next[0] && !next[1],
154+
);
155+
146156
// ========================== Column ==========================
147157
const optionColumns = React.useMemo(() => {
148158
const optionList = [{ options: mergedOptions }];
@@ -246,17 +256,15 @@ const RawOptionList = React.forwardRef<RefOptionListProps, RawOptionListProps>((
246256

247257
// >>>>> Render
248258
return (
249-
<CacheContent open={open}>
250-
<div
251-
className={clsx(`${mergedPrefixCls}-menus`, {
252-
[`${mergedPrefixCls}-menu-empty`]: isEmpty,
253-
[`${mergedPrefixCls}-rtl`]: rtl,
254-
})}
255-
ref={containerRef}
256-
>
257-
{columnNodes}
258-
</div>
259-
</CacheContent>
259+
<div
260+
className={clsx(`${mergedPrefixCls}-menus`, {
261+
[`${mergedPrefixCls}-menu-empty`]: isEmpty,
262+
[`${mergedPrefixCls}-rtl`]: rtl,
263+
})}
264+
ref={containerRef}
265+
>
266+
{columnNodes}
267+
</div>
260268
);
261269
});
262270

src/OptionList/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import * as React from 'react';
44
import RawOptionList from './List';
55

66
const RefOptionList = React.forwardRef<RefOptionListProps>((props, ref) => {
7-
const baseProps = useBaseProps();
7+
const { lockOptions, ...baseProps } = useBaseProps();
88

99
// >>>>> Render
10-
return <RawOptionList {...props} {...baseProps} ref={ref} />;
10+
return <RawOptionList {...props} {...baseProps} lockOptions={lockOptions} ref={ref} />;
1111
});
1212

1313
export default RefOptionList;

0 commit comments

Comments
 (0)