Visitar URL original
fix: prevent console error when using datalist with text inputs · uswds/uswds@432b2aa · GitHub
Skip to content

Commit 432b2aa

Browse files
fix: prevent console error when using datalist with text inputs
Fix two issues that cause errors when native HTML datalist elements are used with USWDS text inputs: 1. keymap.js: Guard against non-KeyboardEvent events. When a user selects a datalist option, the browser fires an InputEvent (not a KeyboardEvent). The keymap handler called getModifierState() and accessed event.key on the InputEvent, causing a TypeError. Add an early return when the event lacks these KeyboardEvent properties. 2. validate-input.js: Move the null check for validatorCheckbox before its first use. Previously, classList.toggle() was called on line 30 before the existence check on line 32, causing a TypeError when the validator checkbox element was not found in the DOM. Fixes #6567 Signed-off-by: Vijay Govindarajan <vijay.govindarajan91@gmail.com> Signed-off-by: V Govindarajan <vijay.govindarajan91@gmail.com>
1 parent 0a801d7 commit 432b2aa

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

‎packages/uswds-core/src/js/utils/keymap.js‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,16 @@
1717
* @param {KeymapConfig} map
1818
* @return {KeyboardEventHandler}
1919
*/
20-
module.exports = (map) => (event) =>
20+
module.exports = (map) => (event) => {
21+
// Bail out if this is not a KeyboardEvent (e.g. InputEvent from datalist
22+
// selection). Only KeyboardEvents have `key` and `getModifierState`.
23+
if (
24+
typeof event.key === "undefined" ||
25+
typeof event.getModifierState !== "function"
26+
) {
27+
return;
28+
}
29+
2130
Object.keys(map).forEach((combo) => {
2231
// Each key combination can have one or more modifier, where each modifier is prefixed with the
2332
// modifier name and "+".
@@ -39,3 +48,4 @@ module.exports = (map) => (event) =>
3948
map[combo](event);
4049
}
4150
});
51+
};

‎packages/uswds-core/src/js/utils/validate-input.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ module.exports = function validate(el) {
2626
`[data-validation-status]`,
2727
);
2828

29-
const checked = validatorPattern.test(el.value);
30-
validatorCheckbox.classList.toggle(CHECKED_CLASS, checked);
31-
3229
if (!validatorCheckbox) {
3330
throw new Error(`No validator checkbox found for: "${validatorName}"`);
3431
}
3532

33+
const checked = validatorPattern.test(el.value);
34+
validatorCheckbox.classList.toggle(CHECKED_CLASS, checked);
35+
3636
// Create status reports for checklist items
3737
const statusComplete = el.dataset.validationComplete || "status complete";
3838
const statusIncomplete =

0 commit comments

Comments
 (0)