-
Notifications
You must be signed in to change notification settings - Fork 180
/
shortcuts.ts
67 lines (57 loc) · 1.47 KB
/
shortcuts.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import {PureComponent} from 'react';
import PropTypes from 'prop-types';
import shortcuts, {ShortcutsMap, ShortcutsScopeOptions} from './core';
export interface ShortcutsProps {
map: ShortcutsMap
scope: string
options: ShortcutsScopeOptions
children?: React.ReactNode
disabled?: boolean | null | undefined
}
export default class Shortcuts extends PureComponent<ShortcutsProps> {
static propTypes = {
map: PropTypes.object.isRequired,
scope: PropTypes.string.isRequired,
options: PropTypes.object,
disabled: PropTypes.bool,
children: PropTypes.node
};
static defaultProps = {
options: {}
};
componentDidMount() {
if (!this.props.disabled) {
this.turnShorcutsOn();
}
}
componentDidUpdate(prevProps: ShortcutsProps) {
const {disabled, map} = this.props;
if (!prevProps.disabled && disabled) {
this.turnShorcutsOff();
}
if (prevProps.disabled && !disabled) {
this.turnShorcutsOn();
}
if (prevProps.map !== map) {
shortcuts.bindMap(map, this.props);
}
}
componentWillUnmount() {
if (!this.props.disabled) {
this.turnShorcutsOff();
}
}
turnShorcutsOn() {
const {map, scope, options} = this.props;
shortcuts.bindMap(map, this.props);
shortcuts.pushScope(scope, options);
}
turnShorcutsOff() {
const {scope} = this.props;
shortcuts.unbindScope(scope);
shortcuts.spliceScope(scope);
}
render() {
return this.props.children || null;
}
}