Skip to content

Commit 798a4a0

Browse files
authored
fix: Cascader expandTrigger="hover" behavior when search (#406)
1 parent 8127ada commit 798a4a0

File tree

3 files changed

+61
-52
lines changed

3 files changed

+61
-52
lines changed

examples/change-on-select.tsx

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,63 @@ import React from 'react';
33
import '../assets/index.less';
44
import Cascader from '../src';
55

6-
const addressOptions = [
6+
const options = [
77
{
8-
label: '福建',
9-
value: 'fj',
8+
label: "Women Clothing",
9+
value: "Women Clothing",
1010
children: [
1111
{
12-
label: '福州',
13-
value: 'fuzhou',
12+
label: "Women Tops, Blouses & Tee",
13+
value: "Women Tops, Blouses & Tee",
1414
children: [
1515
{
16-
label: '马尾',
17-
value: 'mawei',
16+
label: "Women T-Shirts",
17+
value: "Women T-Shirts"
1818
},
19-
],
20-
},
21-
{
22-
label: '泉州',
23-
value: 'quanzhou',
19+
{
20+
label: "Women Tops",
21+
value: "Women Tops"
22+
},
23+
{
24+
label: "Women Tank Tops & Camis",
25+
value: "Women Tank Tops & Camis"
26+
},
27+
{
28+
label: "Women Blouses",
29+
value: "Women Blouses"
30+
}
31+
]
2432
},
25-
],
26-
},
27-
{
28-
label: '浙江',
29-
value: 'zj',
30-
children: [
3133
{
32-
label: '杭州',
33-
value: 'hangzhou',
34+
label: "Women Suits",
35+
value: "Women Suits",
3436
children: [
3537
{
36-
label: '余杭',
37-
value: 'yuhang',
38+
label: "Women Suit Pants",
39+
value: "Women Suit Pants"
3840
},
39-
],
40-
},
41-
],
42-
},
43-
{
44-
label: '北京',
45-
value: 'bj',
46-
children: [
47-
{
48-
label: '朝阳区',
49-
value: 'chaoyang',
41+
{
42+
label: "Women Suit Sets",
43+
value: "Women Suit Sets"
44+
},
45+
{
46+
label: "Women Blazers",
47+
value: "Women Blazers"
48+
}
49+
]
5050
},
5151
{
52-
label: '海淀区',
53-
value: 'haidian',
54-
},
55-
],
56-
},
52+
label: "Women Co-ords",
53+
value: "Women Co-ords",
54+
children: [
55+
{
56+
label: "Two-piece Outfits",
57+
value: "Two-piece Outfits"
58+
}
59+
]
60+
}
61+
]
62+
}
5763
];
5864

5965
class Demo extends React.Component {
@@ -71,14 +77,13 @@ class Demo extends React.Component {
7177
render() {
7278
return (
7379
<Cascader
74-
options={addressOptions}
80+
showSearch
81+
options={options}
7582
onChange={this.onChange}
7683
changeOnSelect
7784
expandTrigger="hover"
7885
loadData={() => console.log('loadData')}
79-
>
80-
<input placeholder="please select address" value={this.state.inputValue} readOnly />
81-
</Cascader>
86+
/>
8287
);
8388
}
8489
}

src/OptionList/Column.tsx

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import * as React from 'react';
21
import classNames from 'classnames';
3-
import { isLeaf, toPathKey } from '../utils/commonUtil';
4-
import CascaderContext from '../context';
5-
import Checkbox from './Checkbox';
2+
import * as React from 'react';
63
import type { DefaultOptionType, SingleValueType } from '../Cascader';
4+
import CascaderContext from '../context';
75
import { SEARCH_MARK } from '../hooks/useSearchOptions';
6+
import { isLeaf, toPathKey } from '../utils/commonUtil';
7+
import Checkbox from './Checkbox';
88

99
export const FIX_LABEL = '__cascader_fix_label__';
1010

@@ -23,6 +23,7 @@ export interface ColumnProps {
2323
halfCheckedSet: Set<React.Key>;
2424
loadingKeys: React.Key[];
2525
isSelectable: (option: DefaultOptionType) => boolean;
26+
searchValue?: string;
2627
}
2728

2829
export default function Column({
@@ -38,6 +39,7 @@ export default function Column({
3839
halfCheckedSet,
3940
loadingKeys,
4041
isSelectable,
42+
searchValue,
4143
}: ColumnProps) {
4244
const menuPrefixCls = `${prefixCls}-menu`;
4345
const menuItemPrefixCls = `${prefixCls}-menu-item`;
@@ -112,13 +114,14 @@ export default function Column({
112114
}) => {
113115
// >>>>> Open
114116
const triggerOpenPath = () => {
115-
if (!disabled) {
116-
const nextValueCells = [...fullPath];
117-
if (hoverOpen && isMergedLeaf) {
118-
nextValueCells.pop();
119-
}
120-
onActive(nextValueCells);
117+
if (disabled || searchValue) {
118+
return;
119+
}
120+
const nextValueCells = [...fullPath];
121+
if (hoverOpen && isMergedLeaf) {
122+
nextValueCells.pop();
121123
}
124+
onActive(nextValueCells);
122125
};
123126

124127
// >>>>> Selection

src/OptionList/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ const RefOptionList = React.forwardRef<RefOptionListProps>((props, ref) => {
204204
<Column
205205
key={index}
206206
{...columnProps}
207+
searchValue={searchValue}
207208
prefixCls={mergedPrefixCls}
208209
options={col.options}
209210
prevValuePath={prevValuePath}

0 commit comments

Comments
 (0)