Skip to content

Commit

Permalink
[FIX #14628] Don’t use preventDefault() on IE unless the user pressed…
Browse files Browse the repository at this point in the history
… enter (#14667)

* 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
  • Loading branch information
mattapperson authored and Matt Apperson committed Oct 30, 2017
1 parent e765e3b commit cd9437b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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 });
}
};
};
};
Original file line number Diff line number Diff line change
@@ -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 });
}
};
};
};
25 changes: 25 additions & 0 deletions src/core_plugins/metrics/public/components/lib/detect_ie.js
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit cd9437b

Please sign in to comment.