Skip to content

Commit

Permalink
BUGFIX: Render dropdown contents even when dropdown is closed
Browse files Browse the repository at this point in the history
Until now, dropdown contents were `null`ed when the dropdown is closed.
This leads to unneccessary re-renders every time the dropdown visibility
is toggled.

This commit changes this logic. Dropdown contents are now always
rendered, but hidden when the dropdown is closed.
  • Loading branch information
grebaldi committed May 30, 2024
1 parent 9cf9b88 commit acbdd0a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`<ShallowDropDownContents/> should not render when having no children. 1`] = `
<ul
aria-hidden="true"
aria-label="dropdown"
className="baseDropDownContentsClassName"
data-ignore_click_outside={true}
onClick={[MockFunction]}
role="listbox"
style={Object {}}
>
Foo children
</ul>
`;
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('<ShallowDropDownContents/>', () => {
it('should not render when having no children.', () => {
const wrapper = shallow(<ShallowDropDownContents {...props}/>);

expect(toJson(wrapper)).toBeFalsy();
expect(toJson(wrapper)).toMatchSnapshot();
});

it('should allow the propagation of "className" with the "className" prop.', () => {
Expand Down
37 changes: 17 additions & 20 deletions packages/react-ui-components/src/DropDown/contents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -214,25 +214,22 @@ export default class ShallowDropDownContents extends PureComponent<ShallowDropDo
}
);

if (isOpen) {
const contents = (
<ul
className={finalClassName}
aria-hidden={isOpen ? 'false' : 'true'}
aria-label="dropdown"
role="listbox"
onClick={closeDropDown}
style={this.state.style}
data-ignore_click_outside={true}
>
{children}
</ul>
);

return scrollable
? ReactDOM.createPortal(contents, document.body)
: contents;
}
return null;
const contents = (
<ul
className={finalClassName}
aria-hidden={isOpen ? 'false' : 'true'}
aria-label="dropdown"
role="listbox"
onClick={closeDropDown}
style={this.state.style}
data-ignore_click_outside={true}
>
{children}
</ul>
);

return scrollable
? ReactDOM.createPortal(contents, document.body)
: contents;
}
}

0 comments on commit acbdd0a

Please sign in to comment.