|
1 | | -import React, { Component } from 'react' |
| 1 | +import React from 'react' |
2 | 2 | import PropTypes from 'prop-types' |
3 | 3 | import styles from './styles.module.css' |
4 | 4 |
|
@@ -102,42 +102,38 @@ const charCodeToSign = ({ keyCode, shiftKey }) => { |
102 | 102 | return String.fromCharCode(code) |
103 | 103 | } |
104 | 104 |
|
105 | | -class Hotkey extends Component { |
106 | | - onKeyDown(event) { |
| 105 | +function Hotkey({ hotkey, onChange }) { |
| 106 | + const onKeyDown = (event) => { |
107 | 107 | if (!event.ctrlKey && !event.altKey && !event.metaKey) { |
108 | 108 | // Do not allow to set global shorcut without modifier keys |
109 | 109 | // At least one of alt, cmd or ctrl is required |
110 | 110 | return |
111 | 111 | } |
112 | 112 | event.preventDefault() |
113 | 113 | event.stopPropagation() |
| 114 | + |
114 | 115 | const key = charCodeToSign(event) |
115 | | - if (!key) { |
116 | | - return |
117 | | - } |
| 116 | + if (!key) return |
118 | 117 | const keys = [] |
| 118 | + |
119 | 119 | if (event.ctrlKey) keys.push('Control') |
120 | 120 | if (event.altKey) keys.push('Alt') |
121 | 121 | if (event.shiftKey) keys.push('Shift') |
122 | 122 | if (event.metaKey) keys.push('Command') |
123 | 123 | keys.push(key) |
124 | | - this.props.onChange(keys.join('+')) |
125 | | - } |
126 | | - |
127 | | - render() { |
128 | | - const { hotkey } = this.props |
129 | | - const keys = hotkey.split('+').map(keyToSign).join(osKeyDelimiter) |
130 | | - return ( |
131 | | - <div> |
132 | | - <input |
133 | | - className={styles.input} |
134 | | - type="text" |
135 | | - value={keys} |
136 | | - onKeyDown={this.onKeyDown.bind(this)} |
137 | | - /> |
138 | | - </div> |
139 | | - ) |
| 124 | + onChange(keys.join('+')) |
140 | 125 | } |
| 126 | + const keys = hotkey.split('+').map(keyToSign).join(osKeyDelimiter) |
| 127 | + return ( |
| 128 | + <div> |
| 129 | + <input |
| 130 | + className={styles.input} |
| 131 | + type="text" |
| 132 | + value={keys} |
| 133 | + onKeyDown={onKeyDown} |
| 134 | + /> |
| 135 | + </div> |
| 136 | + ) |
141 | 137 | } |
142 | 138 |
|
143 | 139 | Hotkey.propTypes = { |
|
0 commit comments