Bug 1848158 - Let keydown preventDefault work on <input type=date> r=reusable-components-reviewers,emilio,ayeddi,tgiles

Differential Revision: https://phabricator.services.mozilla.com/D208077
This commit is contained in:
Gregory Pappas 2024-04-26 20:24:33 +00:00
parent 1e07620528
commit 69b247d1f5
3 changed files with 39 additions and 14 deletions

View file

@ -73,6 +73,8 @@ support-files = ["file_double_submit.html"]
["test_input_datetime_input_change_events.html"]
["test_input_datetime_preventDefault.html"]
["test_input_datetime_readonly.html"]
["test_input_datetime_reset_default_value_input_change_event.html"]

View file

@ -0,0 +1,23 @@
<!doctype html>
<title>Test for bug 1848158</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<script src="/tests/SimpleTest/EventUtils.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<input id="input" type="date" value="1998-01-22">
<script>
SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(function() {
let value = input.value;
input.addEventListener("keydown", function(e) {
e.preventDefault();
});
isnot(value, "", "should have a value");
input.focus();
synthesizeKey("KEY_Backspace");
is(input.value, value, "Value shouldn't change");
SimpleTest.finish();
});
</script>

View file

@ -650,6 +650,10 @@ this.DateTimeBoxWidget = class {
onKeyDown(aEvent) {
this.log("onKeyDown key: " + aEvent.key);
if (aEvent.defaultPrevented) {
return;
}
switch (aEvent.key) {
// Toggle the picker on Space/Enter on Calendar button or Space on input,
// close on Escape anywhere.
@ -691,21 +695,17 @@ this.DateTimeBoxWidget = class {
aEvent.preventDefault();
break;
}
if (this.isEditable()) {
// TODO(emilio, bug 1571533): These functions should look at
// defaultPrevented.
// Ctrl+Backspace/Delete on non-macOS and
// Cmd+Backspace/Delete on macOS to clear the field
if (aEvent.getModifierState("Accel")) {
// Clear the input's value
this.clearInputFields(false);
} else {
let targetField = aEvent.originalTarget;
this.clearFieldValue(targetField);
this.setInputValueFromFields();
}
aEvent.preventDefault();
// Ctrl+Backspace/Delete on non-macOS and
// Cmd+Backspace/Delete on macOS to clear the field
if (aEvent.getModifierState("Accel")) {
// Clear the input's value
this.clearInputFields(false);
} else {
let targetField = aEvent.originalTarget;
this.clearFieldValue(targetField);
this.setInputValueFromFields();
}
aEvent.preventDefault();
break;
}
case "ArrowRight":