-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathKeyboardShortcutsModal.js
121 lines (109 loc) · 4.84 KB
/
KeyboardShortcutsModal.js
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import React from 'react';
import PropTypes from 'prop-types';
import {View, ScrollView} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import _ from 'underscore';
import HeaderWithCloseButton from './HeaderWithCloseButton';
import Text from './Text';
import Modal from './Modal';
import CONST from '../CONST';
import styles from '../styles/styles';
import * as StyleUtils from '../styles/StyleUtils';
import withWindowDimensions, {windowDimensionsPropTypes} from './withWindowDimensions';
import withLocalize, {withLocalizePropTypes} from './withLocalize';
import compose from '../libs/compose';
import KeyboardShortcut from '../libs/KeyboardShortcut';
import * as KeyboardShortcutsActions from '../libs/actions/KeyboardShortcuts';
import * as ModalActions from '../libs/actions/Modal';
import ONYXKEYS from '../ONYXKEYS';
const propTypes = {
/** prop to set shortcuts modal visibility */
isShortcutsModalOpen: PropTypes.bool,
/** prop to fetch screen width */
...windowDimensionsPropTypes,
/** props to fetch translation functions */
...withLocalizePropTypes,
};
const defaultProps = {
isShortcutsModalOpen: false,
};
class KeyboardShortcutsModal extends React.Component {
componentDidMount() {
const openShortcutModalConfig = CONST.KEYBOARD_SHORTCUTS.SHORTCUT_MODAL;
this.unsubscribeShortcutModal = KeyboardShortcut.subscribe(openShortcutModalConfig.shortcutKey, () => {
ModalActions.close();
KeyboardShortcutsActions.showKeyboardShortcutModal();
}, openShortcutModalConfig.descriptionKey, openShortcutModalConfig.modifiers, true);
const closeShortcutModalConfig = CONST.KEYBOARD_SHORTCUTS.ESCAPE;
this.unsubscribeEscapeModal = KeyboardShortcut.subscribe(closeShortcutModalConfig.shortcutKey, () => {
ModalActions.close();
KeyboardShortcutsActions.hideKeyboardShortcutModal();
}, closeShortcutModalConfig.descriptionKey, closeShortcutModalConfig.modifiers, true, true);
}
componentWillUnmount() {
if (this.unsubscribeShortcutModal) {
this.unsubscribeShortcutModal();
}
if (this.unsubscribeEscapeModal) {
this.unsubscribeEscapeModal();
}
}
/**
* Render single row for the Keyboard shortcuts with description
* @param {Object} shortcut
* @param {Boolean} isFirstRow
* @returns {*}
*/
renderRow(shortcut, isFirstRow) {
return (
<View
style={[
styles.keyboardShortcutTableRow,
isFirstRow && styles.keyboardShortcutTableFirstRow,
]}
key={shortcut.displayName}
>
<View style={[styles.dFlex, styles.p2, styles.keyboardShortcutTablePrefix]}>
<Text>{shortcut.displayName}</Text>
</View>
<View style={[styles.flex1, styles.p2, styles.alignSelfStretch]}>
<Text>{this.props.translate(`keyboardShortcutModal.shortcuts.${shortcut.descriptionKey}`)}</Text>
</View>
</View>
);
}
render() {
const shortcuts = KeyboardShortcut.getDocumentedShortcuts();
const modalType = this.props.isSmallScreenWidth ? CONST.MODAL.MODAL_TYPE.BOTTOM_DOCKED : CONST.MODAL.MODAL_TYPE.CENTERED_UNSWIPEABLE;
return (
<Modal
isVisible={this.props.isShortcutsModalOpen}
type={modalType}
innerContainerStyle={{...styles.keyboardShortcutModalContainer, ...StyleUtils.getKeyboardShortcutsModalWidth(this.props.isSmallScreenWidth)}}
onClose={KeyboardShortcutsActions.hideKeyboardShortcutModal}
>
<HeaderWithCloseButton title={this.props.translate('keyboardShortcutModal.title')} onCloseButtonPress={KeyboardShortcutsActions.hideKeyboardShortcutModal} />
<ScrollView style={[styles.p5, styles.pt0]}>
<Text style={styles.mb5}>{this.props.translate('keyboardShortcutModal.subtitle')}</Text>
<View style={[styles.keyboardShortcutTableWrapper]}>
<View style={[styles.alignItemsCenter, styles.keyboardShortcutTableContainer]}>
{_.map(shortcuts, (shortcut, index) => {
const isFirstRow = index === 0;
return this.renderRow(shortcut, isFirstRow);
})}
</View>
</View>
</ScrollView>
</Modal>
);
}
}
KeyboardShortcutsModal.propTypes = propTypes;
KeyboardShortcutsModal.defaultProps = defaultProps;
export default compose(
withWindowDimensions,
withLocalize,
withOnyx({
isShortcutsModalOpen: {key: ONYXKEYS.IS_SHORTCUTS_MODAL_OPEN},
}),
)(KeyboardShortcutsModal);