Skip to content

Commit

Permalink
Fixing inNavbar bug
Browse files Browse the repository at this point in the history
  • Loading branch information
IntellyCode committed Oct 15, 2023
1 parent aeaf180 commit f2362aa
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
10 changes: 9 additions & 1 deletion src/Dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,19 @@ class Dropdown extends React.Component {
const menu = this.getMenu();
const toggle = this.getToggle();

// Add a conditional check to avoid using toggle
//if there is no toggle component in the dropdown
if (!toggle) {
return;
}

const targetIsToggle = toggle.contains(e.target);

const clickIsInMenu = menu && menu.contains(e.target) && menu !== e.target;

let clickIsInInput = false;
if (container) {
// this is only for InputGroup with type dropdown
// This is only for InputGroup with type dropdown
clickIsInInput =
container.classList.contains('input-group') &&
container.classList.contains('dropdown') &&
Expand All @@ -120,6 +127,7 @@ class Dropdown extends React.Component {
this.toggle(e);
}


handleKeyDown(e) {
const isTargetMenuItem =
e.target.getAttribute('role') === 'menuitem' ||
Expand Down
45 changes: 23 additions & 22 deletions src/DropdownToggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,43 +99,44 @@ class DropdownToggle extends React.Component {
Tag = tag;
}

if (this.context.inNavbar) {
//extracted the rendering of the Tag component
const returnFunction = ({ ref }) => {
const handleRef = (tagRef) => {
ref(tagRef);
const { onToggleRef } = this.context;
if (onToggleRef) onToggleRef(tagRef);
};

return (
<Tag
{...props}
{...{ [typeof Tag === 'string' ? 'ref' : 'innerRef']: handleRef }}
className={classes}
onClick={this.onClick}
ref={this.context.onToggleRef}
aria-expanded={this.context.isOpen}
aria-haspopup={this.getRole()}
children={children}
/>
);
}

//No Reference component if the component is in Navbar
if (this.context.inNavbar) {
return (
<>
{returnFunction({ ref: this.context.onToggleRef })}
</>
);
}

//Normal rendering if component not in NavBar
return (
<Reference innerRef={innerRef}>
{({ ref }) => {
const handleRef = (tagRef) => {
ref(tagRef);
const { onToggleRef } = this.context;
if (onToggleRef) onToggleRef(tagRef);
};

return (
<Tag
{...props}
{...{ [typeof Tag === 'string' ? 'ref' : 'innerRef']: handleRef }}
className={classes}
onClick={this.onClick}
aria-expanded={this.context.isOpen}
aria-haspopup={this.getRole()}
children={children}
/>
);
}}

<Reference innerRef={innerRef} >
{returnFunction}
</Reference>
);

}
}

Expand Down

0 comments on commit f2362aa

Please sign in to comment.