From cd9437b3538dceec5d5627a0002ec3215a043030 Mon Sep 17 00:00:00 2001 From: Matt Apperson Date: Mon, 30 Oct 2017 17:24:45 -0400 Subject: [PATCH] =?UTF-8?q?[FIX=20#14628]=20Don=E2=80=99t=20use=20preventD?= =?UTF-8?q?efault()=20on=20IE=20unless=20the=20user=20pressed=20enter=20(#?= =?UTF-8?q?14667)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Don’t use prevent default on IE unless the user pressed enter * Implament ie fix on number fields too * share the ie detection lib * change file names and export types --- .../components/lib/create_number_handler.js | 6 +++-- .../components/lib/create_text_handler.js | 8 ++++-- .../public/components/lib/detect_ie.js | 25 +++++++++++++++++++ 3 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 src/core_plugins/metrics/public/components/lib/detect_ie.js diff --git a/src/core_plugins/metrics/public/components/lib/create_number_handler.js b/src/core_plugins/metrics/public/components/lib/create_number_handler.js index d5f02a550547b..fe84af91af815 100644 --- a/src/core_plugins/metrics/public/components/lib/create_number_handler.js +++ b/src/core_plugins/metrics/public/components/lib/create_number_handler.js @@ -1,10 +1,12 @@ import _ from 'lodash'; +import { detectIE } from './detect_ie'; export default (handleChange) => { return (name, defaultValue) => (e) => { - e.preventDefault(); + if (!detectIE() || e.keyCode === 13) e.preventDefault(); + const value = Number(_.get(e, 'target.value', defaultValue)); if (_.isFunction(handleChange)) { return handleChange({ [name]: value }); } }; -}; +}; \ No newline at end of file diff --git a/src/core_plugins/metrics/public/components/lib/create_text_handler.js b/src/core_plugins/metrics/public/components/lib/create_text_handler.js index eaa7202437328..8c4894f964b06 100644 --- a/src/core_plugins/metrics/public/components/lib/create_text_handler.js +++ b/src/core_plugins/metrics/public/components/lib/create_text_handler.js @@ -1,10 +1,14 @@ import _ from 'lodash'; +import { detectIE } from './detect_ie'; + export default (handleChange) => { return (name, defaultValue) => (e) => { - e.preventDefault(); + // IE preventDefault breaks input, but we still need top prevent enter from being pressed + if (!detectIE() || e.keyCode === 13) e.preventDefault(); + const value = _.get(e, 'target.value', defaultValue); if (_.isFunction(handleChange)) { return handleChange({ [name]: value }); } }; -}; +}; \ No newline at end of file diff --git a/src/core_plugins/metrics/public/components/lib/detect_ie.js b/src/core_plugins/metrics/public/components/lib/detect_ie.js new file mode 100644 index 0000000000000..70c3360bf1b8c --- /dev/null +++ b/src/core_plugins/metrics/public/components/lib/detect_ie.js @@ -0,0 +1,25 @@ +export function detectIE() { + const ua = window.navigator.userAgent; + + const msie = ua.indexOf('MSIE '); + if (msie > 0) { + // IE 10 or older => return version number + return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10); + } + + const trident = ua.indexOf('Trident/'); + if (trident > 0) { + // IE 11 => return version number + const rv = ua.indexOf('rv:'); + return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10); + } + + const edge = ua.indexOf('Edge/'); + if (edge > 0) { + // Edge (IE 12+) => return version number + return parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10); + } + + // other browser + return false; +} \ No newline at end of file