Skip to content

Commit 65d91fc

Browse files
authored
fix: fix not hiding the previous level option when there is no sub option (#401)
* chore: update hover demo * test: add case * test: add possible legacy use cases * fix: fix not hiding the previous level option when there is no sub option * test: add case 增加测试覆盖率 * chore: update logic
1 parent f86b66b commit 65d91fc

File tree

5 files changed

+132
-3
lines changed

5 files changed

+132
-3
lines changed

examples/hover.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,30 @@ const addressOptions = [
5454
},
5555
],
5656
},
57+
{
58+
label: '台湾',
59+
value: 'tw',
60+
children: [
61+
{
62+
label: '台北',
63+
value: 'taipei',
64+
children: [
65+
{
66+
label: '中正区',
67+
value: 'zhongzheng',
68+
},
69+
],
70+
},
71+
{
72+
label: '高雄',
73+
value: 'gaoxiong',
74+
}
75+
]
76+
},
77+
{
78+
label: '香港',
79+
value: 'xg',
80+
},
5781
];
5882

5983
class Demo extends React.Component {

src/OptionList/Column.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,12 @@ export default function Column({
112112
}) => {
113113
// >>>>> Open
114114
const triggerOpenPath = () => {
115-
if (!disabled && (!hoverOpen || !isMergedLeaf)) {
116-
onActive(fullPath);
115+
if (!disabled) {
116+
const nextValueCells = [...fullPath];
117+
if (hoverOpen && isMergedLeaf) {
118+
nextValueCells.pop();
119+
}
120+
onActive(nextValueCells);
117121
}
118122
};
119123

tests/checkable.spec.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import React from 'react';
44
import { mount } from './enzyme';
55
import Cascader from '../src';
6+
import { addressOptions } from './demoOptions';
67

78
describe('Cascader.Checkable', () => {
89
const options = [
@@ -166,4 +167,15 @@ describe('Cascader.Checkable', () => {
166167
const menus = wrapper.find('.rc-cascader-menu');
167168
expect(menus.find('.rc-cascader-checkbox').length).toBe(0);
168169
});
170+
171+
it('should work with custom checkable', () => {
172+
const wrapper = mount(
173+
<Cascader
174+
checkable={<span className="my-custom-checkbox" >0</span>}
175+
open
176+
options={addressOptions}
177+
/>,
178+
);
179+
expect(wrapper.find('.my-custom-checkbox')).toHaveLength(3);
180+
});
169181
});

tests/demoOptions.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,32 @@ export const addressOptionsForFieldNames = [
150150
],
151151
},
152152
];
153+
154+
// Uneven
155+
export const addressOptionsForUneven = [
156+
...addressOptions,
157+
{
158+
label: '台湾',
159+
value: 'tw',
160+
children: [
161+
{
162+
label: '台北',
163+
value: 'taipei',
164+
children: [
165+
{
166+
label: '中正区',
167+
value: 'zhongzheng',
168+
},
169+
],
170+
},
171+
{
172+
label: '高雄',
173+
value: 'gaoxiong',
174+
}
175+
]
176+
},
177+
{
178+
label: '香港',
179+
value: 'xg',
180+
},
181+
]

tests/index.spec.tsx

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
44
import { resetWarned } from 'rc-util/lib/warning';
55
import React from 'react';
66
import Cascader from '../src';
7-
import { addressOptions, optionsForActiveMenuItems } from './demoOptions';
7+
import { addressOptions, addressOptionsForUneven, optionsForActiveMenuItems } from './demoOptions';
88
import { mount } from './enzyme';
99

1010
describe('Cascader.Basic', () => {
@@ -648,6 +648,44 @@ describe('Cascader.Basic', () => {
648648
expect(menus.render()).toMatchSnapshot();
649649
});
650650

651+
// https://github.com/ant-design/ant-design/issues/41134
652+
it('hover to no secondary menu should hide the previous secondary menu', () => {
653+
const wrapper = mount(
654+
<Cascader
655+
changeOnSelect
656+
expandTrigger="hover"
657+
options={addressOptionsForUneven}
658+
onChange={onChange}>
659+
<input readOnly />
660+
</Cascader>,
661+
);
662+
663+
wrapper.find('input').simulate('click');
664+
const menus = wrapper.find('.rc-cascader-menu');
665+
expect(menus.length).toBe(1);
666+
const menu1Items = menus.at(0).find('.rc-cascader-menu-item');
667+
expect(menu1Items.length).toBe(5);
668+
wrapper.clickOption(0, 3, 'mouseEnter');
669+
670+
const menus2 = wrapper.find('.rc-cascader-menu');
671+
expect(menus2.length).toBe(2);
672+
const menu2Items = menus2.at(1).find('.rc-cascader-menu-item');
673+
expect(menu2Items.length).toBe(2);
674+
wrapper.clickOption(1, 0, 'mouseEnter');
675+
676+
expect(wrapper.find('.rc-cascader-menu')).toHaveLength(3);
677+
wrapper.clickOption(1, 1, 'mouseEnter');
678+
expect(wrapper.find('.rc-cascader-menu')).toHaveLength(2); // should hide the previous secondary menu
679+
680+
wrapper.clickOption(0, 4, 'mouseEnter');
681+
expect(wrapper.find('.rc-cascader-menu')).toHaveLength(1); // should hide the previous secondary menu
682+
683+
jest.runAllTimers();
684+
wrapper.update();
685+
expect(selectedValue).toBeFalsy();
686+
expect(wrapper.isOpen()).toBeTruthy();
687+
})
688+
651689
describe('focus test', () => {
652690
let domSpy;
653691
let focusTimes = 0;
@@ -746,6 +784,28 @@ describe('Cascader.Basic', () => {
746784
expect(wrapper.find('li.rc-cascader-menu-item-active')).toHaveLength(1);
747785
expect(wrapper.find('li.rc-cascader-menu-item-active').first().text()).toEqual('Bamboo');
748786
});
787+
788+
describe('the defaultValue should be activated the first time it is opened', () => {
789+
(['click', 'hover'] as const).forEach(expandTrigger => {
790+
it(`expandTrigger: ${expandTrigger}`, () => {
791+
const wrapper = mount(
792+
<Cascader
793+
expandTrigger={expandTrigger}
794+
defaultValue={['tw', 'gaoxiong']}
795+
options={addressOptionsForUneven}
796+
>
797+
<input readOnly />
798+
</Cascader>,
799+
);
800+
801+
wrapper.find('input').simulate('click');
802+
const activeItems = wrapper.find('li.rc-cascader-menu-item-active');
803+
expect(activeItems).toHaveLength(2);
804+
expect(activeItems.last().text()).toEqual('高雄');
805+
});
806+
})
807+
});
808+
749809
});
750810

751811
it('defaultValue not exist', () => {

0 commit comments

Comments
 (0)