Skip to content

Commit

Permalink
fix(MenuOverlay): fire rerender if initially open
Browse files Browse the repository at this point in the history
  • Loading branch information
bfbiggs authored and pauljeter committed Sep 30, 2019
1 parent 54b2fe7 commit 829d5ad
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
16 changes: 12 additions & 4 deletions react/src/lib/MenuOverlay/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,21 @@ import MenuContext from '../MenuContext';

class MenuOverlay extends React.Component {
state = {
isOpen: false,
};
isOpen: this.props.isOpen || false
}

componentDidMount() {
this.props.isOpen && this.forceUpdate();
}

componentDidUpdate(prevProps, prevState) {
const { focusFirstQuery } = this.props;
const { isOpen } = this.state;

if(!prevState.isOpen && prevState !== isOpen && focusFirstQuery) {
const overlay = findDOMNode(this.menuOverlay);
const focusElement = overlay && overlay.querySelector(focusFirstQuery);

focusElement
&& focusElement.focus();
}
Expand Down Expand Up @@ -53,6 +57,7 @@ class MenuOverlay extends React.Component {
const { isOpen } = this.state;

const otherProps = omit({...props}, [
'isOpen',
'focusFirstQuery',
'onSelect'
]);
Expand Down Expand Up @@ -101,6 +106,8 @@ MenuOverlay.propTypes = {
className: PropTypes.string,
/** @prop Queries children to find matching item to have focus | '' */
focusFirstQuery: PropTypes.string,
/** @prop Prop to initalize state as open | false */
isOpen: PropTypes.bool,
/** @prop HTML element that provides control to MenuOverlay user */
menuTrigger: PropTypes.element.isRequired,
/** @prop Callback function invoked when user selects MenuOverlay | null */
Expand All @@ -113,6 +120,7 @@ MenuOverlay.defaultProps = {
children: null,
className: '',
focusFirstQuery: '',
isOpen: false,
onSelect: null,
showArrow: true,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ ShallowWrapper {
Symbol(enzyme.__unrendered__): <MenuOverlay
className=""
focusFirstQuery=""
isOpen={false}
menuTrigger={
<div>
Trigger
Expand Down

0 comments on commit 829d5ad

Please sign in to comment.