@@ -30,8 +30,12 @@ function keyboardBindings(event: KeyboardEvent) {
30
30
switch ( event . key ) {
31
31
case 'Enter' :
32
32
case 'Tab' :
33
- commit ( input , list )
34
- event . preventDefault ( )
33
+ if ( commit ( input , list ) ) {
34
+ event . preventDefault ( )
35
+ }
36
+ break
37
+ case 'Escape' :
38
+ clearSelection ( list )
35
39
break
36
40
case 'ArrowDown' :
37
41
navigate ( input , list , 1 )
@@ -64,10 +68,11 @@ function commitWithElement(event: MouseEvent) {
64
68
event . preventDefault ( )
65
69
}
66
70
67
- function commit ( input : HTMLTextAreaElement | HTMLInputElement , list : HTMLElement ) : void {
71
+ function commit ( input : HTMLTextAreaElement | HTMLInputElement , list : HTMLElement ) : boolean {
68
72
const target = list . querySelector ( '[aria-selected="true"]' )
69
- if ( ! target ) return
73
+ if ( ! target ) return false
70
74
fireCommitEvent ( target )
75
+ return true
71
76
}
72
77
73
78
function fireCommitEvent ( target : Element ) : void {
@@ -104,14 +109,19 @@ export function navigate(
104
109
}
105
110
}
106
111
107
- function trackComposition ( event : Event ) {
112
+ function clearSelection ( list ) : void {
113
+ const target = list . querySelector ( '[aria-selected="true"]' )
114
+ if ( ! target ) return
115
+ target . setAttribute ( 'aria-selected' , 'false' )
116
+ }
117
+
118
+ function trackComposition ( event : Event ) : void {
108
119
const input = event . currentTarget
109
120
if ( ! ( input instanceof HTMLTextAreaElement || input instanceof HTMLInputElement ) ) return
110
121
compositionMap . set ( input , event . type === 'compositionstart' )
111
122
112
123
const list = document . getElementById ( input . getAttribute ( 'aria-owns' ) || '' )
113
124
if ( ! list ) return
114
- const target = list . querySelector ( '[aria-selected="true"]' )
115
- if ( ! target ) return
116
- target . setAttribute ( 'aria-selected' , 'false' )
125
+
126
+ clearSelection ( list )
117
127
}
0 commit comments