-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug-Fix: Modified click trigger on form elements prevent default behaviour of clicked element #2771
base: dev
Are you sure you want to change the base?
Changes from all commits
6c53c61
e73831b
b71bc93
6dd555e
5f27c78
80c0856
24944a3
5f57fcd
1c773df
93e48ce
86a53ca
f010924
8817374
5389f07
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,15 +94,20 @@ describe('Core htmx internals Tests', function() { | |
htmx._('shouldCancel')({ type: 'click' }, anchorThatShouldNotCancel).should.equal(false) | ||
|
||
var form = make('<form></form>') | ||
htmx._('shouldCancel')({ type: 'submit' }, form).should.equal(true) | ||
htmx._('shouldCancel')({ type: 'submit', target: document.createElement('form') }, form).should.equal(true) | ||
|
||
htmx._('shouldCancel')({ type: 'click', target: document.createElement('form') }, form).should.equal(true) | ||
|
||
// function works also when target isn't an Element | ||
htmx._('shouldCancel')({ type: 'click', target: null }, form).should.equal(false) | ||
|
||
var form = make("<form><input id='i1' type='submit'></form>") | ||
var input = byId('i1') | ||
htmx._('shouldCancel')({ type: 'click' }, input).should.equal(true) | ||
htmx._('shouldCancel')({ type: 'click', target: document.createElement('input') }, input).should.equal(true) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't we use the Same goes for the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes you're right |
||
|
||
var form = make("<form><button id='b1' type='submit'></form>") | ||
var button = byId('b1') | ||
htmx._('shouldCancel')({ type: 'click' }, button).should.equal(true) | ||
htmx._('shouldCancel')({ type: 'click', target: document.createElement('button') }, button).should.equal(true) | ||
}) | ||
|
||
it('unset properly unsets a given attribute', function() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<title>Mocha Tests</title> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<link rel="stylesheet" href="../node_modules/mocha/mocha.css" /> | ||
<meta http-equiv="cache-control" content="no-cache, must-revalidate, post-check=0, pre-check=0" /> | ||
<meta http-equiv="cache-control" content="max-age=0" /> | ||
<meta http-equiv="expires" content="0" /> | ||
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" /> | ||
<meta http-equiv="pragma" content="no-cache" /> | ||
<meta name="htmx-config" content='{"historyEnabled":false,"defaultSettleDelay":0}'> | ||
</head> | ||
<body style="padding:20px;font-family: sans-serif"> | ||
|
||
<h1 style="margin-top: 40px">Working Checkbox and Datepicker</h1> | ||
<p>A modified click trigger on a form, doesn't prevent the default behaviour of other elements</p> | ||
<p>The checkbox can be checked, a click on the datepicker symbol opens the datepicker flyout</p> | ||
|
||
<script src="../src/htmx.js"></script> | ||
|
||
<input type="date"> | ||
<input type="checkbox"> | ||
<form hx-trigger="click from:body"> | ||
<input type="hidden" value="foo"> | ||
</form> | ||
</div> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -270,7 +270,6 @@ ol li { | |
float: right; | ||
} | ||
.nav { | ||
text-align: center; | ||
font-size: 1.2em; | ||
line-height: 1.3em; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering ; couldn't we simply check
if (asElement(evt.target) || elt).tagName === 'FORM'
in this case?So that, if defined,
evt.target
takes precedence, otherwise we keep the existing behavior. You would probably not even need to add thetarget
property to mock the events of the test suite in this case? Aselt
is the fallbackBtw, should we use the same technique for the other checks in this same function? Anchors for ex. Maybe we should simply change to
const elt = asElement(evt.target) || asElement(node)
at the start of the function and not need to modify anything else inside?Just some thoughts, completely untested, let me know!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Telroshan the problem is, the event target is an EventTarget and an EventTarget can be a Dom element but doesn't have to be. So the property tagname can be undefined. (It was my first approach but the type checker complaint). That's why I chose this way to ensure we only check properties which exist and don't run into any errors when trying to access undefined properties
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hence my suggestion @pfiadDi :
const elt = asElement(evt.target) || asElement(node)
Unless I'm mistaked (could totally be, didn't test this 😆 ), this will get the
evt.target
if it's a DOM element, but if it's not an element, will fallback tonode
.Then we have the already present
if
statement that returns early, in case neitherevt.target
nornode
were elements.I believe you shouldn't get any type checker complaint with that approach as we cast to
Element
thanks toasElement
?