Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Dropdown): visibility is controlled when undefined #202

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"dependencies": {
"@babel/runtime": "^7.10.1",
"classnames": "^2.2.6",
"rc-trigger": "^5.0.4",
"rc-trigger": "^5.2.18",
"rc-util": "^5.17.0"
}
}
2 changes: 1 addition & 1 deletion src/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function Dropdown(props: DropdownProps, ref) {
} = props;

const [triggerVisible, setTriggerVisible] = React.useState<boolean>();
const mergedVisible = 'visible' in props ? visible : triggerVisible;
const mergedVisible = visible !== undefined ? visible : triggerVisible;

const triggerRef = React.useRef(null);
React.useImperativeHandle(ref, () => triggerRef.current);
Expand Down
2 changes: 1 addition & 1 deletion tests/__snapshots__/basic.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Array [
<div>
<div
class="rc-dropdown"
style="opacity: 0; pointer-events: none;"
Copy link
Author

Choose a reason for hiding this comment

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

I believe this was caused by updating the rc-trigger minor version.

style="opacity: 0;"
>
<ul
class="rc-dropdown-menu rc-dropdown-menu-root rc-dropdown-menu-vertical"
Expand Down
49 changes: 48 additions & 1 deletion tests/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('dropdown', () => {
expect(dropdown.find('.my-button').hasClass('rc-dropdown-open')).toBe(true);
});

it('supports constrolled visible prop', () => {
it('supports controlled visible prop', () => {
const onVisibleChange = jest.fn();
const dropdown = mount(
<Dropdown
Expand Down Expand Up @@ -104,6 +104,53 @@ describe('dropdown', () => {
expect(getPopupDomNode(dropdown).classList.contains('rc-dropdown-hidden')).toBe(true);
});

it('controls visibility when visible is undefined', async () => {
// Make sure dropdown component is still ""
let clicked;
let overlayClicked;

function onClick({ key }) {
clicked = key;
}

function onOverlayClick({ key }) {
overlayClicked = key;
}

const menu = (
<Menu style={{ width: 140 }} onClick={onClick}>
<MenuItem key="1">
<span className="my-menuitem">one</span>
</MenuItem>
<Divider />
<MenuItem key="2">two</MenuItem>
</Menu>
);
const dropdown = mount(
<Dropdown
trigger={['click']}
overlay={menu}
onOverlayClick={onOverlayClick}
// If visible is undefined, let dropdown control the component's
// visible state, not the consumer
visible={undefined}
>
<button className="my-button">open</button>
</Dropdown>,
);
expect(dropdown.find('.my-button')).toBeTruthy();
expect(dropdown.find('.rc-dropdown')).toBeTruthy();

dropdown.find('.my-button').simulate('click');
expect(clicked).toBeUndefined();
expect(getPopupDomNode(dropdown).classList.contains('rc-dropdown-hidden')).toBe(false);

dropdown.find('.my-menuitem').simulate('click');
expect(clicked).toBe('1');
expect(overlayClicked).toBe('1');
expect(getPopupDomNode(dropdown).classList.contains('rc-dropdown-hidden')).toBe(true);
});

it('re-align works', async () => {
const buttonStyle = { width: 600, height: 20, marginLeft: 100 };
const menu = (
Expand Down