@@ -334,21 +334,30 @@ async function type(...args) {
334
334
335
335
async function typeImpl ( element , text , { allAtOnce = false , delay} = { } ) {
336
336
if ( element . disabled ) return
337
- const previousText = element . value
338
337
339
- const computedText =
340
- element . maxLength > 0
341
- ? text . slice ( 0 , Math . max ( element . maxLength - previousText . length , 0 ) )
338
+ element . focus ( )
339
+
340
+ // The focussed element could change between each event, so get the currently active element each time
341
+ const currentElement = ( ) => element . ownerDocument . activeElement
342
+ const currentValue = ( ) => element . ownerDocument . activeElement . value
343
+
344
+ const computeText = ( ) =>
345
+ currentElement ( ) . maxLength > 0
346
+ ? text . slice (
347
+ 0 ,
348
+ Math . max ( currentElement ( ) . maxLength - currentValue ( ) . length , 0 ) ,
349
+ )
342
350
: text
343
351
344
352
if ( allAtOnce ) {
345
353
if ( ! element . readOnly ) {
354
+ const previousText = element . value
355
+
346
356
fireEvent . input ( element , {
347
- target : { value : previousText + computedText } ,
357
+ target : { value : previousText + computeText ( ) } ,
348
358
} )
349
359
}
350
360
} else {
351
- let actuallyTyped = previousText
352
361
for ( let index = 0 ; index < text . length ; index ++ ) {
353
362
const char = text [ index ]
354
363
const key = char // TODO: check if this also valid for characters with diacritic markers e.g. úé etc
@@ -357,28 +366,28 @@ async function typeImpl(element, text, {allAtOnce = false, delay} = {}) {
357
366
// eslint-disable-next-line no-await-in-loop
358
367
if ( delay > 0 ) await wait ( delay )
359
368
360
- const downEvent = fireEvent . keyDown ( element , {
369
+ if ( currentElement ( ) . disabled ) return
370
+
371
+ const downEvent = fireEvent . keyDown ( currentElement ( ) , {
361
372
key,
362
373
keyCode,
363
374
which : keyCode ,
364
375
} )
365
376
366
377
if ( downEvent ) {
367
- const pressEvent = fireEvent . keyPress ( element , {
378
+ const pressEvent = fireEvent . keyPress ( currentElement ( ) , {
368
379
key,
369
380
keyCode,
370
381
charCode : keyCode ,
371
382
} )
372
383
373
- const isTextPastThreshold =
374
- ( actuallyTyped + key ) . length > ( previousText + computedText ) . length
384
+ const isTextPastThreshold = ! computeText ( ) . length
375
385
376
386
if ( pressEvent && ! isTextPastThreshold ) {
377
- actuallyTyped += key
378
387
if ( ! element . readOnly ) {
379
- fireEvent . input ( element , {
388
+ fireEvent . input ( currentElement ( ) , {
380
389
target : {
381
- value : actuallyTyped ,
390
+ value : currentValue ( ) + key ,
382
391
} ,
383
392
bubbles : true ,
384
393
cancelable : true ,
@@ -387,7 +396,7 @@ async function typeImpl(element, text, {allAtOnce = false, delay} = {}) {
387
396
}
388
397
}
389
398
390
- fireEvent . keyUp ( element , {
399
+ fireEvent . keyUp ( currentElement ( ) , {
391
400
key,
392
401
keyCode,
393
402
which : keyCode ,
0 commit comments