-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathShortcut.tsx
More file actions
124 lines (118 loc) · 3.55 KB
/
Shortcut.tsx
File metadata and controls
124 lines (118 loc) · 3.55 KB
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
122
123
124
import type { FunctionComponent } from 'react';
import { MouseIcon } from '@patternfly/react-icons';
import { Label } from '@patternfly/react-core';
import { createUseStyles } from 'react-jss';
import { css } from '@patternfly/react-styles';
export interface ShortcutProps {
/** Array of shortcut keys */
keys: string[];
/** Shortcut description */
description?: React.ReactNode;
/** Indicates whether symbols should be displayed for certain keys */
showSymbols?: boolean;
/** Show hover in the shortcut */
hover?: boolean;
/** Show click in the shortcut */
click?: boolean;
/** Show right click in the shortcut */
rightClick?: boolean;
/** Show drag in the shortcut */
drag?: boolean;
/** Show drag and drop in the shortcut */
dragAndDrop?: boolean;
/** Custom label for the "Hover" mouse action. Defaults to "Hover" */
hoverLabel?: string;
/** Custom label for the "Click" mouse action. Defaults to "Click" */
clickLabel?: string;
/** Custom label for the "Right click" mouse action. Defaults to "Right click" */
rightClickLabel?: string;
/** Custom label for the "Drag" mouse action. Defaults to "Drag" */
dragLabel?: string;
/** Custom label for the "Drag + Drop" mouse action. Defaults to "Drag + Drop" */
dragAndDropLabel?: string;
/** Shortcut className */
className?: string;
/** Custom OUIA ID */
ouiaId?: string | number;
}
const symbols = {
'shift': '⇧',
'opt': '⌥',
'cmd': '⌘',
'enter': '↵',
'ctrl': '^',
'caps lock': '⇪',
'tab': '↹',
'win': '⊞',
'backspace': '⌫'
}
const useStyles = createUseStyles({
shortcut: {
marginRight: 'var(--pf-t--global--spacer--400)'
}
})
const Shortcut: FunctionComponent<ShortcutProps> = ({
keys = [],
description = null,
showSymbols = true,
hover,
click,
drag,
rightClick,
dragAndDrop,
hoverLabel = 'Hover',
clickLabel = 'Click',
rightClickLabel = 'Right click',
dragLabel = 'Drag',
dragAndDropLabel = 'Drag + Drop',
className,
ouiaId = 'Shortcut',
...props
}: ShortcutProps) => {
const classes = useStyles();
const badges = [
...(hover ? [
<Label variant="outline" key="hover" data-test-id="hover">
<MouseIcon />{` ${hoverLabel}`}
</Label>
] : []),
...keys.map((key) => {
const trimmedKey = key.trim().toLowerCase();
return(
<Label variant="outline" key={key} data-test-id={`${key}-key`}>
{showSymbols && symbols[trimmedKey] ? `${symbols[trimmedKey]} ` : '' }
{key.length === 1 ? key.toUpperCase() : key[0].toUpperCase() + key.slice(1).toLowerCase()}
</Label>
)}),
...(click ? [
<Label variant="outline" key="click" data-test-id="click">
<MouseIcon />{` ${clickLabel}`}
</Label>
] : []),
...(rightClick ? [
<Label variant="outline" key="right-click" data-test-id="right-click">
<MouseIcon />{` ${rightClickLabel}`}
</Label>
] : []),
...(drag ? [
<Label variant="outline" key="drag" data-test-id="drag">
<MouseIcon />{` ${dragLabel}`}
</Label>
] : []),
...(dragAndDrop ? [
<Label variant="outline" key="drag-and-drop" data-test-id="drag-and-drop">
<MouseIcon />{` ${dragAndDropLabel}`}
</Label>
] : [])
]
return (
<>
<span className={css({ [classes.shortcut]: description, className })} data-ouia-component-id={ouiaId} {...props}>
{badges.length > 0 && badges.reduce((prev, curr, idx) => (
<span key={idx}>{[ prev, ' + ', curr ]}</span>
))}
</span>
{description}
</>
);}
export default Shortcut;