From 782dcfd8a649a78af625562f603d270e7025f245 Mon Sep 17 00:00:00 2001 From: Katie Date: Mon, 17 Oct 2022 12:07:44 -0400 Subject: [PATCH] CSR-762 Fix merge conflict --- src/helpers/button-question-focus-helper.js | 42 +++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/helpers/button-question-focus-helper.js diff --git a/src/helpers/button-question-focus-helper.js b/src/helpers/button-question-focus-helper.js new file mode 100644 index 000000000..9884fa041 --- /dev/null +++ b/src/helpers/button-question-focus-helper.js @@ -0,0 +1,42 @@ +/** + * Helper for GA click event. When the user mouse clicks on a `base-input-button`, we + * push the click event. When the user tabs through a list of radio buttons via a + * keyboard, we only want to push the GA click event if the selection was deliberate + * (space/enter key) or if there is a selection and the user tabs off of the radio group. + */ + +let lastFocusedInputGroupName = ""; +let onButtonQuestionLostFocusCallback = null; + +// TODO KO add tests +const handleAnyComponentFocus = (e) => { + const targetType = e.target.type; + if (targetType !== "radio" && targetType !== "checkbox") { + invokeButtonQuestionLostFocusCallback(); + } +}; + +const handleButtonComponentFocus = (e) => { + if (e && lastFocusedInputGroupName !== e.groupName) { + invokeButtonQuestionLostFocusCallback(); + } +}; + +const handleInputComponentBlur = (e) => { + if (e) { + lastFocusedInputGroupName = e.groupName; + onButtonQuestionLostFocusCallback = e.onButtonQuestionLostFocusCallback; + } +}; + +const invokeButtonQuestionLostFocusCallback = () => { + if (onButtonQuestionLostFocusCallback) { + onButtonQuestionLostFocusCallback(); + } +}; + +export { + handleAnyComponentFocus, + handleButtonComponentFocus, + handleInputComponentBlur, +};