Skip to content

Commit

Permalink
Fix EuiContextMenu bug in which the menu items were continuously re-r…
Browse files Browse the repository at this point in the history
…endered causing their refs to be cleared.
  • Loading branch information
cjcenizal committed Feb 21, 2018
1 parent f5fca8f commit 5a841e8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# [`master`](https://github.com/elastic/eui/tree/master)

No public interface changes since `0.0.22`.
- Fixed `EuiContextMenu` bug when using the keyboard to navigate up, which was caused by unnecessarily re-rendering the items, thus losing references to them ([#431](https://github.com/elastic/eui/pull/431))

# [`0.0.22`](https://github.com/elastic/eui/tree/v0.0.22)

- Added `EuiDelayHide` component. [#412](https://github.com/elastic/eui/pull/412)
- Added `EuiDelayHide` component. ([#412](https://github.com/elastic/eui/pull/412))
- Decreased overall size of checkbox, radio, and switches as well as better styles for the different states. ([#407](https://github.com/elastic/eui/pull/407))
- Added `EuiFilePicker` component for `input type="file"` needs. ([#402](https://github.com/elastic/eui/pedull/402))
- Add `isLoading` prop to `EuiButton` ([#427](https://github.com/elastic/eui/pull/427))
Expand Down
13 changes: 12 additions & 1 deletion src/components/context_menu/context_menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export class EuiContextMenu extends Component {
this.idToPanelMap = {};
this.idToPreviousPanelIdMap = {};
this.idAndItemIndexToPanelIdMap = {};
this.idToRenderedItemsMap = {};

this.state = {
height: undefined,
Expand Down Expand Up @@ -153,6 +154,7 @@ export class EuiContextMenu extends Component {
this.idToPanelMap = mapIdsToPanels(panels);
this.idToPreviousPanelIdMap = mapIdsToPreviousPanels(panels);
this.idAndItemIndexToPanelIdMap = mapPanelItemsToPanels(panels);
this.mapIdsToRenderedItems(panels);
}

componentWillMount() {
Expand All @@ -165,6 +167,15 @@ export class EuiContextMenu extends Component {
}
}

mapIdsToRenderedItems = panels => {
this.idToRenderedItemsMap = {};

// Pre-rendering the items lets us check reference equality inside of EuiContextMenuPanel.
panels.forEach(panel => {
this.idToRenderedItemsMap[panel.id] = this.renderItems(panel.items);
});
};

renderItems(items = []) {
return items.map((item, index) => {
const {
Expand Down Expand Up @@ -227,7 +238,7 @@ export class EuiContextMenu extends Component {
transitionType={this.state.isOutgoingPanelVisible ? transitionType : undefined}
transitionDirection={this.state.isOutgoingPanelVisible ? this.state.transitionDirection : undefined}
hasFocus={transitionType === 'in'}
items={this.renderItems(panel.items)}
items={this.idToRenderedItemsMap[panelId]}
initialFocusedItemIndex={this.state.isUsingKeyboardToNavigate ? this.state.focusedItemIndex : undefined}
onUseKeyboardToNavigate={this.onUseKeyboardToNavigate}
showNextPanel={this.showNextPanel}
Expand Down

0 comments on commit 5a841e8

Please sign in to comment.