Skip to content

Commit 309f576

Browse files
committed
Merge branch 'master' of github.com:box/box-ui-elements into replace-form-legacy-context
2 parents 269d862 + 2b136e4 commit 309f576

23 files changed

+1045
-518
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
4+
export const HotkeyContext = React.createContext(null);
5+
6+
HotkeyContext.displayName = 'HotkeyContext';
7+
8+
export const HotkeyContextPropTypes = {
9+
hotkeyLayer: PropTypes.object,
10+
};

src/components/hotkeys/HotkeyHelpModal.js

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ import React, { Component } from 'react';
22
import PropTypes from 'prop-types';
33
import { FormattedMessage } from 'react-intl';
44

5-
import commonMessages from '../../common/messages';
65
import { ModalActions } from '../modal';
76
import Button from '../button';
87
import PlainButton from '../plain-button';
98
import DropdownMenu, { MenuToggle } from '../dropdown-menu';
109
import { Menu, MenuItem } from '../menu';
11-
10+
import { HotkeyContext } from './HotkeyContext';
1211
import HotkeyFriendlyModal from './HotkeyFriendlyModal'; // eslint-disable-line import/no-cycle
12+
13+
import commonMessages from '../../common/messages';
1314
import messages from './messages';
1415

1516
import './HotkeyHelpModal.scss';
@@ -35,31 +36,39 @@ class HotkeyHelpModal extends Component {
3536
onRequestClose: PropTypes.func.isRequired,
3637
};
3738

38-
static contextTypes = {
39-
hotkeyLayer: PropTypes.object,
40-
};
41-
42-
constructor(props, context) {
39+
constructor(props) {
4340
super(props);
4441

45-
this.hotkeys = context.hotkeyLayer.getActiveHotkeys();
46-
this.types = context.hotkeyLayer.getActiveTypes();
42+
this.hotkeys = {};
43+
this.types = [];
4744
this.state = {
48-
currentType: this.types.length ? this.types[0] : null,
45+
currentType: null,
4946
};
5047
}
5148

49+
componentDidMount() {
50+
const hotkeyLayer = this.context;
51+
if (hotkeyLayer) {
52+
this.hotkeys = hotkeyLayer.getActiveHotkeys();
53+
this.types = hotkeyLayer.getActiveTypes();
54+
this.setState({
55+
currentType: this.types.length ? this.types[0] : null,
56+
});
57+
}
58+
}
59+
5260
componentDidUpdate({ isOpen: prevIsOpen }, { currentType: prevType }) {
5361
const { isOpen } = this.props;
62+
const hotkeyLayer = this.context;
5463

55-
if (!isOpen) {
64+
if (!isOpen || !hotkeyLayer) {
5665
return;
5766
}
5867

5968
// modal is being opened; refresh hotkeys
6069
if (!prevIsOpen && isOpen) {
61-
this.hotkeys = this.context.hotkeyLayer.getActiveHotkeys();
62-
this.types = this.context.hotkeyLayer.getActiveTypes();
70+
this.hotkeys = hotkeyLayer.getActiveHotkeys();
71+
this.types = hotkeyLayer.getActiveTypes();
6372
}
6473

6574
if (!prevType) {
@@ -189,4 +198,6 @@ class HotkeyHelpModal extends Component {
189198
}
190199
}
191200

201+
HotkeyHelpModal.contextType = HotkeyContext;
202+
192203
export default HotkeyHelpModal;

src/components/hotkeys/HotkeyLayer.js

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import PropTypes from 'prop-types';
33

44
import HotkeyRecord, { HotkeyPropType } from './HotkeyRecord';
55
import HotkeyService from './HotkeyService';
6+
import { HotkeyContext } from './HotkeyContext';
67

78
import Hotkeys from './Hotkeys';
89
import HotkeyHelpModal from './HotkeyHelpModal'; // eslint-disable-line import/no-cycle
@@ -25,14 +26,6 @@ class HotkeyLayer extends Component {
2526
enableHelpModal: false,
2627
};
2728

28-
static contextTypes = {
29-
hotkeyLayer: PropTypes.object,
30-
};
31-
32-
static childContextTypes = {
33-
hotkeyLayer: PropTypes.object,
34-
};
35-
3629
constructor(props) {
3730
super(props);
3831

@@ -43,12 +36,6 @@ class HotkeyLayer extends Component {
4336
isHelpModalOpen: false,
4437
};
4538

46-
getChildContext() {
47-
return {
48-
hotkeyLayer: this.hotkeyService,
49-
};
50-
}
51-
5239
componentWillUnmount() {
5340
this.hotkeyService.destroyLayer();
5441
}
@@ -85,16 +72,18 @@ class HotkeyLayer extends Component {
8572
const { children, className = '', enableHelpModal } = this.props;
8673

8774
return (
88-
<Hotkeys configs={this.getHotkeyConfigs()}>
89-
{enableHelpModal ? (
90-
<span className={`hotkey-layer ${className}`}>
91-
<HotkeyHelpModal isOpen={this.state.isHelpModalOpen} onRequestClose={this.closeHelpModal} />
92-
{children}
93-
</span>
94-
) : (
95-
children
96-
)}
97-
</Hotkeys>
75+
<HotkeyContext.Provider value={this.hotkeyService}>
76+
<Hotkeys configs={this.getHotkeyConfigs()}>
77+
{enableHelpModal ? (
78+
<span className={`hotkey-layer ${className}`}>
79+
<HotkeyHelpModal isOpen={this.state.isHelpModalOpen} onRequestClose={this.closeHelpModal} />
80+
{children}
81+
</span>
82+
) : (
83+
children
84+
)}
85+
</Hotkeys>
86+
</HotkeyContext.Provider>
9887
);
9988
}
10089
}

src/components/hotkeys/Hotkeys.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Children, Component } from 'react';
22
import PropTypes from 'prop-types';
33

44
import { HotkeyPropType } from './HotkeyRecord';
5+
import { HotkeyContext } from './HotkeyContext';
56

67
class Hotkeys extends Component {
78
/* eslint-disable no-underscore-dangle */
@@ -12,14 +13,11 @@ class Hotkeys extends Component {
1213
configs: PropTypes.arrayOf(HotkeyPropType).isRequired,
1314
};
1415

15-
static contextTypes = {
16-
hotkeyLayer: PropTypes.object,
17-
};
18-
1916
componentDidMount() {
2017
const { configs } = this.props;
18+
const hotkeyLayer = this.context;
2119

22-
if (!this.context.hotkeyLayer) {
20+
if (!hotkeyLayer) {
2321
throw new Error('You must instantiate a HotkeyLayer before using Hotkeys');
2422
}
2523

@@ -44,11 +42,13 @@ class Hotkeys extends Component {
4442
}
4543

4644
_addHotkeys(hotkeyConfigs) {
47-
hotkeyConfigs.forEach(hotkeyConfig => this.context.hotkeyLayer.registerHotkey(hotkeyConfig));
45+
hotkeyConfigs.forEach(hotkeyConfig => this.context.registerHotkey(hotkeyConfig));
4846
}
4947

5048
_removeHotkeys(hotkeyConfigs) {
51-
hotkeyConfigs.forEach(hotkeyConfig => this.context.hotkeyLayer.deregisterHotkey(hotkeyConfig));
49+
if (this.context) {
50+
hotkeyConfigs.forEach(hotkeyConfig => this.context.deregisterHotkey(hotkeyConfig));
51+
}
5252
}
5353

5454
render() {
@@ -59,4 +59,6 @@ class Hotkeys extends Component {
5959
}
6060
}
6161

62+
Hotkeys.contextType = HotkeyContext;
63+
6264
export default Hotkeys;

0 commit comments

Comments
 (0)