Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PoC: Rough example of adopting createContext #3751

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/components/hotkeys/HotkeyContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { createContext } from 'react';

// Default to null - should be overridden by a provider (HotkeyLayer)
const HotkeyContext = createContext({ hotkeyLayer: null});

export default HotkeyContext;
5 changes: 2 additions & 3 deletions src/components/hotkeys/HotkeyHelpModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import HotkeyFriendlyModal from './HotkeyFriendlyModal'; // eslint-disable-line
import messages from './messages';

import './HotkeyHelpModal.scss';
import HotkeyContext from './HotKeyContext';

const specialCharacters = {
backspace: '\u232b',
Expand All @@ -35,9 +36,7 @@ class HotkeyHelpModal extends Component {
onRequestClose: PropTypes.func.isRequired,
};

static contextTypes = {
hotkeyLayer: PropTypes.object,
};
static contextType = HotkeyContext;

constructor(props, context) {
super(props);
Expand Down
42 changes: 17 additions & 25 deletions src/components/hotkeys/HotkeyLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import HotkeyService from './HotkeyService';

import Hotkeys from './Hotkeys';
import HotkeyHelpModal from './HotkeyHelpModal'; // eslint-disable-line import/no-cycle
import HotkeyContext from './HotKeyContext';

import './HotkeyLayer.scss';

Expand All @@ -25,28 +26,17 @@ class HotkeyLayer extends Component {
enableHelpModal: false,
};

static contextTypes = {
hotkeyLayer: PropTypes.object,
};

static childContextTypes = {
hotkeyLayer: PropTypes.object,
};

constructor(props) {
super(props);

this.hotkeyService = new HotkeyService();
}

state = {
isHelpModalOpen: false,
};

getChildContext() {
return {
// need to move this inside constructor so that we can access this.hotkeyService after it's initialized
this.state = {
isHelpModalOpen: false,
hotkeyLayer: this.hotkeyService,
};
}
}

componentWillUnmount() {
Expand Down Expand Up @@ -85,16 +75,18 @@ class HotkeyLayer extends Component {
const { children, className = '', enableHelpModal } = this.props;

return (
<Hotkeys configs={this.getHotkeyConfigs()}>
{enableHelpModal ? (
<span className={`hotkey-layer ${className}`}>
<HotkeyHelpModal isOpen={this.state.isHelpModalOpen} onRequestClose={this.closeHelpModal} />
{children}
</span>
) : (
children
)}
</Hotkeys>
<HotkeyContext.Provider value={this.state}>
<Hotkeys configs={this.getHotkeyConfigs()}>
{enableHelpModal ? (
<span className={`hotkey-layer ${className}`}>
<HotkeyHelpModal isOpen={this.state.isHelpModalOpen} onRequestClose={this.closeHelpModal} />
{children}
</span>
) : (
children
)}
</Hotkeys>
</HotkeyContext.Provider>
);
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/components/hotkeys/Hotkeys.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Children, Component } from 'react';
import PropTypes from 'prop-types';

import { HotkeyPropType } from './HotkeyRecord';
import HotkeyContext from './HotKeyContext';

class Hotkeys extends Component {
/* eslint-disable no-underscore-dangle */
Expand All @@ -12,13 +13,15 @@ class Hotkeys extends Component {
configs: PropTypes.arrayOf(HotkeyPropType).isRequired,
};

static contextTypes = {
hotkeyLayer: PropTypes.object,
};
static contextType = HotkeyContext;

componentDidMount() {
const { configs } = this.props;

// should verify this isn't being mounted too many times after making changes
console.log('Hotkeys componentDidMount', this.context.hotkeyLayer);
console.log(this.context);

if (!this.context.hotkeyLayer) {
throw new Error('You must instantiate a HotkeyLayer before using Hotkeys');
}
Expand Down
Loading