Skip to content

Commit e429094

Browse files
authored
fix: prevent click event on non-focusable control (#1130)
1 parent f681f7b commit e429094

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Also, please watch the repo and respond to questions/bug reports/feature
4848
requests! Thanks!
4949
5050
<!-- prettier-ignore-start -->
51-
[egghead]: https://egghead.io/series/how-to-contribute-to-an-open-source-project-on-github
51+
[egghead]: https://egghead.io/courses/how-to-contribute-to-an-open-source-project-on-github
5252
[all-contributors]: https://github.com/all-contributors/all-contributors
5353
[issues]: https://github.com/testing-library/user-event/issues
5454
<!-- prettier-ignore-end -->

src/event/behavior/click.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ behavior.click = (event, target, instance) => {
99
return () => {
1010
if (isFocusable(control)) {
1111
focusElement(control)
12+
instance.dispatchEvent(control, cloneEvent(event))
1213
}
13-
instance.dispatchEvent(control, cloneEvent(event))
1414
}
1515
} else if (isElementType(target, 'input', {type: 'file'})) {
1616
return () => {

tests/pointer/click.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,24 @@ describe('label', () => {
235235

236236
expect(getEvents('click')).toHaveLength(2)
237237
})
238+
239+
test('do not click associated non-focusable control per label', async () => {
240+
const {element, getEvents, user} = setup(
241+
`<label for="in">foo</label><input disabled id="in"/>`,
242+
)
243+
244+
await user.pointer({keys: '[MouseLeft]', target: element})
245+
246+
expect(getEvents('click')).toHaveLength(1)
247+
})
248+
249+
test('do not click nested non-focusable control per label', async () => {
250+
const {element, getEvents, user} = setup(`<label><input disabled/></label>`)
251+
252+
await user.pointer({keys: '[MouseLeft]', target: element})
253+
254+
expect(getEvents('click')).toHaveLength(1)
255+
})
238256
})
239257

240258
describe('check/uncheck control per click', () => {
@@ -282,6 +300,19 @@ describe('check/uncheck control per click', () => {
282300

283301
expect(input).not.toBeChecked()
284302
})
303+
304+
test('clicking label does not change non-focusable checkable input', async () => {
305+
const {
306+
elements: [input, label],
307+
user,
308+
} = setup(`<input type="checkbox" disabled id="a"/><label for="a"></label>`)
309+
310+
expect(input).not.toBeChecked()
311+
312+
await user.pointer({keys: '[MouseLeft]', target: label})
313+
314+
expect(input).not.toBeChecked()
315+
})
285316
})
286317

287318
describe('submit form per click', () => {

0 commit comments

Comments
 (0)