Skip to content

Commit c9e2bea

Browse files
Fix Modified click trigger on form elements prevent default behaviour (#3336)
Fix Modified click trigger on form elements prevent default behaviour of clicked element
1 parent 7388d0c commit c9e2bea

3 files changed

Lines changed: 35 additions & 1 deletion

File tree

src/htmx.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2425,6 +2425,8 @@ var htmx = (function() {
24252425
*/
24262426
function shouldCancel(evt, elt) {
24272427
if (evt.type === 'submit' || evt.type === 'click') {
2428+
// use elt from event that was submitted/clicked where possible to determining if default form/link behavior should be canceled
2429+
elt = asElement(evt.target) || elt
24282430
if (elt.tagName === 'FORM') {
24292431
return true
24302432
}

test/core/internals.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,19 @@ describe('Core htmx internals Tests', function() {
9393
var anchorThatShouldNotCancel = make("<a href='#foo'></a>")
9494
htmx._('shouldCancel')({ type: 'click' }, anchorThatShouldNotCancel).should.equal(false)
9595

96+
var divThatShouldNotCancel = make('<div></div>')
97+
htmx._('shouldCancel')({ type: 'click' }, divThatShouldNotCancel).should.equal(false)
98+
9699
var form = make('<form></form>')
97-
htmx._('shouldCancel')({ type: 'submit' }, form).should.equal(true)
100+
htmx._('shouldCancel')({ type: 'submit', target: form }, form).should.equal(true)
101+
htmx._('shouldCancel')({ type: 'click', target: form }, form).should.equal(true)
102+
103+
// falls back to check elt tag when target is not an element
104+
htmx._('shouldCancel')({ type: 'click', target: null }, form).should.equal(true)
105+
106+
// check that events targeting elements that shouldn't cancel don't cancel
107+
htmx._('shouldCancel')({ type: 'submit', target: anchorThatShouldNotCancel }, form).should.equal(false)
108+
htmx._('shouldCancel')({ type: 'click', target: divThatShouldNotCancel }, form).should.equal(false)
98109

99110
form = make('<form id="f1">' +
100111
'<input id="insideInput" type="submit">' +

test/core/regressions.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,4 +270,25 @@ describe('Core htmx Regression Tests', function() {
270270
done()
271271
}, 50)
272272
})
273+
274+
it('a modified click trigger on a form does not prevent the default behaviour of other elements - https://github.com/bigskysoftware/htmx/issues/2755', function(done) {
275+
var defaultPrevented = 'unset'
276+
make('<input type="date" id="datefield">')
277+
make('<form hx-trigger="click from:body"></form>')
278+
279+
htmx.on('#datefield', 'click', function(evt) {
280+
// we need to wait so the state of the evt is finalized
281+
setTimeout(() => {
282+
defaultPrevented = evt.defaultPrevented
283+
try {
284+
defaultPrevented.should.equal(false)
285+
done()
286+
} catch (err) {
287+
done(err)
288+
}
289+
}, 0)
290+
})
291+
292+
byId('datefield').click()
293+
})
273294
})

0 commit comments

Comments
 (0)