1 | import {PureComponent} from 'react';
|
2 | import PropTypes from 'prop-types';
|
3 |
|
4 | import shortcuts from '../shortcuts/core';
|
5 |
|
6 |
|
7 | export default class Shortcuts extends PureComponent {
|
8 | static propTypes = {
|
9 | map: PropTypes.object.isRequired,
|
10 | scope: PropTypes.string.isRequired,
|
11 | options: PropTypes.object,
|
12 | disabled: PropTypes.bool,
|
13 | children: PropTypes.node
|
14 | };
|
15 |
|
16 | static defaultProps = {
|
17 | options: {}
|
18 | };
|
19 |
|
20 | componentWillMount() {
|
21 | if (!this.props.disabled) {
|
22 | this.turnShorcutsOn();
|
23 | }
|
24 | }
|
25 |
|
26 | componentWillReceiveProps(nextProps) {
|
27 | const {disabled} = this.props;
|
28 | if (!disabled && nextProps.disabled) {
|
29 | this.turnShorcutsOff();
|
30 | }
|
31 | if (disabled && !nextProps.disabled) {
|
32 | this.turnShorcutsOn();
|
33 | }
|
34 | }
|
35 |
|
36 | componentWillUnmount() {
|
37 | if (!this.props.disabled) {
|
38 | this.turnShorcutsOff();
|
39 | }
|
40 | }
|
41 |
|
42 | turnShorcutsOn() {
|
43 | const {map, scope, options} = this.props;
|
44 | shortcuts.bindMap(map, this.props);
|
45 | shortcuts.pushScope(scope, options);
|
46 | }
|
47 |
|
48 | turnShorcutsOff() {
|
49 | const {scope} = this.props;
|
50 | shortcuts.unbindScope(scope);
|
51 | shortcuts.spliceScope(scope);
|
52 | }
|
53 |
|
54 | render() {
|
55 | return this.props.children || null;
|
56 | }
|
57 | }
|