Skip to content

Commit

Permalink
fix(pointer): honor click handler on <label/> (#810)
Browse files Browse the repository at this point in the history
* test: remove todo

* fix(pointer): honor click handler on `<label/>`
  • Loading branch information
ph-fritsche authored Dec 13, 2021
1 parent e1c4cad commit 2c5d9f1
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 15 deletions.
12 changes: 4 additions & 8 deletions src/pointer/pointerPress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,18 +220,14 @@ function up(

const canClick = pointerType !== 'mouse' || button === 'primary'
if (canClick && target === pressed.downTarget) {
fire('click')
const unpreventedClick = fire('click')
if (clickCount === 2) {
fire('dblclick')
}

// If the click happens inside a `label` with a control, the control (or closes focusable) is focused.
const label = target.closest('label')
if (label?.control) {
focus(
findClosest(label.control, isFocusable) ??
target.ownerDocument.body,
)
const control = target.closest('label')?.control
if (unpreventedClick && control && isFocusable(control)) {
focus(control)
}
}
}
Expand Down
8 changes: 1 addition & 7 deletions tests/pointer/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,10 +319,6 @@ describe('focus control when clicking label', () => {

expect(label).not.toHaveFocus()
expect(input).toHaveFocus()
expect(input).toHaveProperty('selectionStart', 0)

// TODO: click on label selects input value
// expect(input).toHaveProperty('selectionEnd', 3)
})

test('click handlers can prevent moving focus per label', async () => {
Expand All @@ -333,9 +329,7 @@ describe('focus control when clicking label', () => {

await userEvent.pointer({keys: '[MouseLeft]', target: label})

// TODO: honor click handler
// expect(input).not.toHaveFocus()
expect(input).toBeTruthy()
expect(input).not.toHaveFocus()
})

test('do not move focus to disabled control', async () => {
Expand Down

0 comments on commit 2c5d9f1

Please sign in to comment.