File tree Expand file tree Collapse file tree 3 files changed +20
-8
lines changed
Expand file tree Collapse file tree 3 files changed +20
-8
lines changed Original file line number Diff line number Diff line change @@ -266,13 +266,6 @@ function mousedownDefaultBehavior({
266266 node ?: Node
267267 offset ?: number
268268} ) {
269- // The closest focusable element is focused when a `mousedown` would have been fired.
270- // Even if there was no `mousedown` because the element was disabled.
271- // A `mousedown` that preventsDefault cancels this though.
272- focus ( target )
273-
274- // TODO: What happens if a focus event handler interfers?
275-
276269 // An unprevented mousedown moves the cursor to the closest character.
277270 // We try to approximate the behavior for a no-layout environment.
278271 if ( ! targetIsDisabled ) {
@@ -326,6 +319,11 @@ function mousedownDefaultBehavior({
326319 selection . addRange ( range . cloneRange ( ) )
327320 }
328321 }
322+
323+ // The closest focusable element is focused when a `mousedown` would have been fired.
324+ // Even if there was no `mousedown` because the element was disabled.
325+ // A `mousedown` that preventsDefault cancels this though.
326+ focus ( target )
329327}
330328
331329function getTextRange (
Original file line number Diff line number Diff line change @@ -368,3 +368,17 @@ describe('focus control when clicking label', () => {
368368 expect ( input ) . not . toHaveFocus ( )
369369 } )
370370} )
371+
372+ test ( 'focus event handler can override selection' , async ( ) => {
373+ const { element, user} = setup ( `<input value="hello"/>` , {
374+ focus : false ,
375+ } )
376+ element . addEventListener ( 'focus' , e =>
377+ ( e . target as HTMLInputElement ) . select ( ) ,
378+ )
379+
380+ await user . click ( element )
381+
382+ expect ( element ) . toHaveProperty ( 'selectionStart' , 0 )
383+ expect ( element ) . toHaveProperty ( 'selectionEnd' , 5 )
384+ } )
Original file line number Diff line number Diff line change @@ -19,9 +19,9 @@ test('type into input', async () => {
1919 input[value="foo"] - mousemove
2020 input[value="foo"] - pointerdown
2121 input[value="foo"] - mousedown: primary
22+ input[value="foo"] - select
2223 input[value="foo"] - focus
2324 input[value="foo"] - focusin
24- input[value="foo"] - select
2525 input[value="foo"] - pointerup
2626 input[value="foo"] - mouseup: primary
2727 input[value="foo"] - click: primary
You can’t perform that action at this time.
0 commit comments