@@ -33,20 +33,20 @@ import {
3333import { AbstractPureComponent , Classes , DISPLAYNAME_PREFIX , type Props } from "../../common" ;
3434import { Icon } from "../icon/icon" ;
3535
36- import { normalizeKeyCombo } from "./hotkeyParser" ;
36+ import { isMac , normalizeKeyCombo } from "./hotkeyParser" ;
3737
38- const KEY_ICONS : Record < string , { icon : React . JSX . Element ; iconTitle : string } > = {
39- ArrowDown : { icon : < ArrowDown /> , iconTitle : "Down key" } ,
40- ArrowLeft : { icon : < ArrowLeft /> , iconTitle : "Left key" } ,
41- ArrowRight : { icon : < ArrowRight /> , iconTitle : "Right key" } ,
42- ArrowUp : { icon : < ArrowUp /> , iconTitle : "Up key" } ,
43- alt : { icon : < KeyOption /> , iconTitle : "Alt/Option key" } ,
44- cmd : { icon : < KeyCommand /> , iconTitle : "Command key" } ,
45- ctrl : { icon : < KeyControl /> , iconTitle : "Control key" } ,
38+ const KEY_ICONS : Record < string , { icon : React . JSX . Element ; iconTitle : string ; isMacOnly ?: boolean } > = {
39+ alt : { icon : < KeyOption /> , iconTitle : "Alt/Option key" , isMacOnly : true } ,
40+ arrowdown : { icon : < ArrowDown /> , iconTitle : "Down key" } ,
41+ arrowleft : { icon : < ArrowLeft /> , iconTitle : "Left key" } ,
42+ arrowright : { icon : < ArrowRight /> , iconTitle : "Right key" } ,
43+ arrowup : { icon : < ArrowUp /> , iconTitle : "Up key" } ,
44+ cmd : { icon : < KeyCommand /> , iconTitle : "Command key" , isMacOnly : true } ,
45+ ctrl : { icon : < KeyControl /> , iconTitle : "Control key" , isMacOnly : true } ,
4646 delete : { icon : < KeyDelete /> , iconTitle : "Delete key" } ,
4747 enter : { icon : < KeyEnter /> , iconTitle : "Enter key" } ,
48- meta : { icon : < KeyCommand /> , iconTitle : "Command key" } ,
49- shift : { icon : < KeyShift /> , iconTitle : "Shift key" } ,
48+ meta : { icon : < KeyCommand /> , iconTitle : "Command key" , isMacOnly : true } ,
49+ shift : { icon : < KeyShift /> , iconTitle : "Shift key" , isMacOnly : true } ,
5050} ;
5151
5252/** Reverse table of some CONFIG_ALIASES fields, for display by KeyComboTag */
@@ -71,20 +71,30 @@ export interface KeyComboTagProps extends Props {
7171 minimal ?: boolean ;
7272}
7373
74- export class KeyComboTag extends AbstractPureComponent < KeyComboTagProps > {
74+ interface KeyComboTagInternalProps extends KeyComboTagProps {
75+ /** Override the oeprating system rendering for internal testing purposes */
76+ platformOverride ?: string ;
77+ }
78+
79+ export class KeyComboTagInternal extends AbstractPureComponent < KeyComboTagInternalProps > {
7580 public static displayName = `${ DISPLAYNAME_PREFIX } .KeyComboTag` ;
7681
7782 public render ( ) {
78- const { className, combo, minimal } = this . props ;
79- const keys = normalizeKeyCombo ( combo )
83+ const { className, combo, minimal, platformOverride } = this . props ;
84+ const normalizedKeys = normalizeKeyCombo ( combo , platformOverride ) ;
85+ const keys = normalizedKeys
8086 . map ( key => ( key . length === 1 ? key . toUpperCase ( ) : key ) )
81- . map ( minimal ? this . renderMinimalKey : this . renderKey ) ;
82- return < span className = { classNames ( Classes . KEY_COMBO , className ) } > { keys } </ span > ;
87+ . map ( ( key , index ) =>
88+ minimal
89+ ? this . renderMinimalKey ( key , index , index === normalizedKeys . length - 1 )
90+ : this . renderKey ( key , index ) ,
91+ ) ;
92+ return < span className = { classNames ( Classes . KEY_COMBO , className , { [ Classes . MINIMAL ] : minimal } ) } > { keys } </ span > ;
8393 }
8494
8595 private renderKey = ( key : string , index : number ) => {
8696 const keyString = DISPLAY_ALIASES [ key ] ?? key ;
87- const icon = KEY_ICONS [ key ] ;
97+ const icon = this . getKeyIcon ( key ) ;
8898 const reactKey = `key-${ index } ` ;
8999 return (
90100 < kbd className = { classNames ( Classes . KEY , { [ Classes . MODIFIER_KEY ] : icon != null } ) } key = { reactKey } >
@@ -94,8 +104,22 @@ export class KeyComboTag extends AbstractPureComponent<KeyComboTagProps> {
94104 ) ;
95105 } ;
96106
97- private renderMinimalKey = ( key : string , index : number ) => {
98- const icon = KEY_ICONS [ key ] ;
99- return icon == null ? key : < Icon icon = { icon . icon } title = { icon . iconTitle } key = { `key-${ index } ` } /> ;
107+ private renderMinimalKey = ( key : string , index : number , isLastKey : boolean ) => {
108+ const icon = this . getKeyIcon ( key ) ;
109+ if ( icon == null ) {
110+ return isLastKey ? key : < React . Fragment key = { `key-${ index } ` } > { key } + </ React . Fragment > ;
111+ }
112+ return < Icon icon = { icon . icon } title = { icon . iconTitle } key = { `key-${ index } ` } /> ;
100113 } ;
114+
115+ private getKeyIcon ( key : string ) {
116+ const { platformOverride } = this . props ;
117+ const icon = KEY_ICONS [ key ] ;
118+ if ( icon ?. isMacOnly && ! isMac ( platformOverride ) ) {
119+ return undefined ;
120+ }
121+ return icon ;
122+ }
101123}
124+
125+ export const KeyComboTag : React . ComponentType < KeyComboTagProps > = KeyComboTagInternal ;
0 commit comments