Skip to content

Commit 06f12a6

Browse files
authoredMar 31, 2022
fix(pointer): change selection before dispatching focus (#895)
1 parent dff6d54 commit 06f12a6

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed
 

‎src/pointer/pointerPress.ts

+5-7
Original file line numberDiff line numberDiff 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

331329
function getTextRange(

‎tests/pointer/select.ts

+14
Original file line numberDiff line numberDiff 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+
})

‎tests/utility/type.ts

+1-1
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)