-
Notifications
You must be signed in to change notification settings - Fork 180
/
shortcuts-hoc.tsx
43 lines (34 loc) · 1.16 KB
/
shortcuts-hoc.tsx
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
import React, {ComponentType} from 'react';
import PropTypes from 'prop-types';
import getUID from '../global/get-uid';
import Shortcuts from './shortcuts';
import {ShortcutsMap, ShortcutsScopeOptions} from './core';
export interface ShortcutsHOCOptions extends ShortcutsScopeOptions {
disabled?: boolean | null | undefined
}
export interface ShortcutsHOCProps {
rgShortcutsOptions: ShortcutsHOCOptions
rgShortcutsMap: ShortcutsMap
}
export default function shortcutsHOC<P extends {}>(ComposedComponent: ComponentType<P> | string) {
return class WithShortcuts extends React.Component<P & ShortcutsHOCProps> {
static propTypes = {
rgShortcutsOptions: PropTypes.object,
rgShortcutsMap: PropTypes.object
};
_shortcutsScopeUid = getUID('rg-shortcuts-');
render() {
const {rgShortcutsOptions, rgShortcutsMap, ...props} = this.props;
return (
<Shortcuts
scope={this._shortcutsScopeUid}
map={rgShortcutsMap}
options={rgShortcutsOptions}
disabled={rgShortcutsOptions.disabled}
>
<ComposedComponent {...props as unknown as P}/>
</Shortcuts>
);
}
};
}