Skip to content

Commit 46bc44f

Browse files
committed
fix: hover trigger should not open final path
1 parent dc7eb82 commit 46bc44f

File tree

8 files changed

+78
-37
lines changed

8 files changed

+78
-37
lines changed

jest.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
module.exports = {
22
setupFiles: ['./tests/setup.js'],
33
snapshotSerializers: [require.resolve('enzyme-to-json/serializer')],
4-
}
4+
};

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"compile": "father build",
3737
"prepublishOnly": "npm run compile && np --no-cleanup --yolo --no-publish",
3838
"lint": "eslint src/ examples/ --ext .tsx,.ts,.jsx,.jsx",
39-
"test": "father test --no-cache",
39+
"test": "father test",
4040
"coverage": "father test --coverage",
4141
"now-build": "npm run build"
4242
},
@@ -55,7 +55,7 @@
5555
"father": "^2.13.2",
5656
"gh-pages": "^3.1.0",
5757
"glob": "^7.1.6",
58-
"jest": "^26.0.0",
58+
"jest": "^26.6.3",
5959
"np": "^6.0.0",
6060
"rc-form": "^2.4.0",
6161
"react": "^16.0.0",

src/OptionList/Column.tsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ export default function Column({
3939
const { changeOnSelect, expandTrigger, expandIcon, loadingIcon, dropdownMenuColumnStyle } =
4040
React.useContext(CascaderContext);
4141

42+
const hoverOpen = expandTrigger === 'hover';
43+
4244
// ============================ Render ============================
4345
return (
4446
<ul className={menuPrefixCls} role="menu">
@@ -53,7 +55,7 @@ export default function Column({
5355

5456
// >>>>> Open
5557
const triggerOpenPath = () => {
56-
if (!disabled) {
58+
if (!disabled && (!hoverOpen || !isMergedLeaf)) {
5759
onOpen(index, value);
5860
}
5961
};
@@ -98,7 +100,7 @@ export default function Column({
98100
}
99101
}}
100102
onMouseEnter={() => {
101-
if (expandTrigger) {
103+
if (hoverOpen) {
102104
triggerOpenPath();
103105
}
104106
}}
File renamed without changes.

tests/enzyme.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ type ReplaceReturnType<T extends (...a: any) => any, TNewReturn> = (
77

88
export interface ReactWrapper extends OriginReactWrapper {
99
isOpen: () => boolean;
10-
clickOption: (menuIndex: number, itemIndex: number, type?: 'click' | 'doubleClick') => ReactWrapper;
10+
clickOption: (
11+
menuIndex: number,
12+
itemIndex: number,
13+
type?: 'click' | 'doubleClick' | 'mouseEnter',
14+
) => ReactWrapper;
1115
}
1216

1317
type Mount = ReplaceReturnType<typeof originMount, ReactWrapper>;

tests/index.spec.js tests/index.spec.tsx

+60-28
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import React from 'react';
44
import { resetWarned } from 'rc-util/lib/warning';
55
import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
6-
import { mount } from 'enzyme';
7-
import Cascader from '..';
6+
import { mount } from './enzyme';
7+
import Cascader from '../src';
88
import { addressOptions, optionsForActiveMenuItems } from './demoOptions';
99

1010
describe('Cascader.Basic', () => {
@@ -368,6 +368,8 @@ describe('Cascader.Basic', () => {
368368
value: [],
369369
};
370370

371+
timeout = null;
372+
371373
componentDidMount() {
372374
this.timeout = setTimeout(() => {
373375
this.setState({
@@ -650,47 +652,77 @@ describe('Cascader.Basic', () => {
650652
});
651653

652654
it('focus', () => {
653-
const cascaderRef = React.createRef();
655+
const cascaderRef = React.createRef() as any;
654656
mount(<Cascader ref={cascaderRef} />);
655657

656658
cascaderRef.current.focus();
657659
expect(focusTimes === 1).toBeTruthy();
658660
});
659661

660662
it('blur', () => {
661-
const cascaderRef = React.createRef();
663+
const cascaderRef = React.createRef() as any;
662664
mount(<Cascader ref={cascaderRef} />);
663665

664666
cascaderRef.current.blur();
665667
expect(blurTimes === 1).toBeTruthy();
666668
});
667669
});
668670

669-
it('active className', () => {
670-
const wrapper = mount(
671-
<Cascader
672-
open
673-
expandIcon=""
674-
options={[
675-
{
676-
label: 'Bamboo',
677-
value: 'bamboo',
678-
children: [
679-
{
680-
label: 'Little',
681-
value: 'little',
682-
},
683-
],
684-
},
685-
]}
686-
/>,
687-
);
671+
describe('active className', () => {
672+
it('expandTrigger: click', () => {
673+
const wrapper = mount(
674+
<Cascader
675+
open
676+
expandIcon=""
677+
options={[
678+
{
679+
label: 'Bamboo',
680+
value: 'bamboo',
681+
children: [
682+
{
683+
label: 'Little',
684+
value: 'little',
685+
},
686+
],
687+
},
688+
]}
689+
/>,
690+
);
688691

689-
wrapper.clickOption(0, 0);
690-
wrapper.clickOption(1, 0);
692+
wrapper.clickOption(0, 0);
693+
wrapper.clickOption(1, 0);
694+
695+
expect(wrapper.find('li.rc-cascader-menu-item-active')).toHaveLength(2);
696+
expect(wrapper.find('li.rc-cascader-menu-item-active').first().text()).toEqual('Bamboo');
697+
expect(wrapper.find('li.rc-cascader-menu-item-active').last().text()).toEqual('Little');
698+
});
699+
700+
it('expandTrigger: hover', () => {
701+
const wrapper = mount(
702+
<Cascader
703+
open
704+
expandIcon=""
705+
expandTrigger="hover"
706+
options={[
707+
{
708+
label: 'Bamboo',
709+
value: 'bamboo',
710+
children: [
711+
{
712+
label: 'Little',
713+
value: 'little',
714+
},
715+
],
716+
},
717+
]}
718+
/>,
719+
);
691720

692-
expect(wrapper.find('li.rc-cascader-menu-item-active')).toHaveLength(2);
693-
expect(wrapper.find('li.rc-cascader-menu-item-active').first().text()).toEqual('Bamboo');
694-
expect(wrapper.find('li.rc-cascader-menu-item-active').last().text()).toEqual('Little');
721+
wrapper.clickOption(0, 0, 'mouseEnter');
722+
wrapper.clickOption(1, 0, 'mouseEnter');
723+
724+
expect(wrapper.find('li.rc-cascader-menu-item-active')).toHaveLength(1);
725+
expect(wrapper.find('li.rc-cascader-menu-item-active').first().text()).toEqual('Bamboo');
726+
});
695727
});
696728
});

tests/keyboard.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import { mount } from 'enzyme';
33
import KeyCode from 'rc-util/lib/KeyCode';
4-
import Cascader from '..';
4+
import Cascader from '../src';
55
import { addressOptions } from './demoOptions';
66

77
describe('Cascader.Keyboard', () => {

tests/setup.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@ Object.assign(Enzyme.ReactWrapper.prototype, {
1313
isOpen() {
1414
return !!this.find('Trigger').props().popupVisible;
1515
},
16-
clickOption(menuIndex, itemIndex, type = 'click') {
16+
findOption(menuIndex, itemIndex) {
1717
const menu = this.find('ul.rc-cascader-menu').at(menuIndex);
1818
const itemList = menu.find('li.rc-cascader-menu-item');
1919

20-
itemList.at(itemIndex).simulate(type);
20+
return itemList.at(itemIndex);
21+
},
22+
clickOption(menuIndex, itemIndex, type = 'click') {
23+
this.findOption(menuIndex, itemIndex).simulate(type);
2124

2225
return this;
2326
},

0 commit comments

Comments
 (0)