Skip to content

Commit

Permalink
Extract constants and purge superfluous utils (patw0929#318)
Browse files Browse the repository at this point in the history
* refactor: booleans are eq

* refactor: extract constants

* refactor: addClass can be replaced by classList.add

* refactor: unused util
  • Loading branch information
mcataford committed Mar 1, 2020
1 parent 2b66580 commit 00ae43f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 68 deletions.
39 changes: 14 additions & 25 deletions src/components/IntlTelInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import AllCountries from './AllCountries';
import FlagDropDown from './FlagDropDown';
import TelInput from './TelInput';
import utils from './utils';
import { KEYS } from './constants'
import '../styles/intlTelInput.scss';

const mobileUserAgentRegexp = /Android.+Mobile|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i;
Expand Down Expand Up @@ -56,18 +57,6 @@ class IntlTelInput extends Component {

this.windowLoaded = false;

this.keys = {
UP: 38,
DOWN: 40,
ENTER: 13,
ESC: 27,
PLUS: 43,
A: 65,
Z: 90,
SPACE: 32,
TAB: 9,
};

this.query = '';
this.selectedCountryData = {};

Expand Down Expand Up @@ -603,13 +592,13 @@ class IntlTelInput extends Component {
const current = this.flagDropDown.querySelectorAll('.highlight')[0];
const prevElement = current ? current.previousElementSibling : undefined;
const nextElement = current ? current.nextElementSibling : undefined;
let next = key === this.keys.UP ? prevElement : nextElement;
let next = key === KEYS.UP ? prevElement : nextElement;

if (next) {
// skip the divider
if (next.getAttribute('class').indexOf('divider') > -1) {
next =
key === this.keys.UP
key === KEYS.UP
? next.previousElementSibling
: next.nextElementSibling;
}
Expand Down Expand Up @@ -1028,7 +1017,7 @@ class IntlTelInput extends Component {
this.wrapperClass['separate-dial-code'] = this.props.separateDialCode;

if (this.isMobile && this.props.useMobileFullscreenDropdown) {
utils.addClass(document.querySelector('body'), 'iti-mobile');
document.querySelector('body').classList.add('iti-mobile')
// on mobile, we want a full screen dropdown, so we must append it to the body
this.dropdownContainer = 'body';
window.addEventListener('scroll', this.handleWindowScroll);
Expand All @@ -1038,10 +1027,10 @@ class IntlTelInput extends Component {
handleSelectedFlagKeydown = e => {
if (
!this.state.showDropdown &&
(e.which === this.keys.UP ||
e.which === this.keys.DOWN ||
e.which === this.keys.SPACE ||
e.which === this.keys.ENTER)
(e.which === KEYS.UP ||
e.which === KEYS.DOWN ||
e.which === KEYS.SPACE ||
e.which === KEYS.ENTER)
) {
// prevent form from being submitted if "ENTER" was pressed
e.preventDefault();
Expand All @@ -1053,7 +1042,7 @@ class IntlTelInput extends Component {
}

// allow navigation from dropdown to input on TAB
if (e.which === this.keys.TAB) {
if (e.which === KEYS.TAB) {
this.toggleDropdown(false);
}
};
Expand Down Expand Up @@ -1146,20 +1135,20 @@ class IntlTelInput extends Component {
// and enter key from submitting a form etc
e.preventDefault();

if (e.which === this.keys.UP || e.which === this.keys.DOWN) {
if (e.which === KEYS.UP || e.which === KEYS.DOWN) {
// up and down to navigate
this.handleUpDownKey(e.which);
} else if (e.which === this.keys.ENTER) {
} else if (e.which === KEYS.ENTER) {
// enter to select
this.handleEnterKey();
} else if (e.which === this.keys.ESC) {
} else if (e.which === KEYS.ESC) {
// esc to close
this.setState({
showDropdown: false,
});
} else if (
(e.which >= this.keys.A && e.which <= this.keys.Z) ||
e.which === this.keys.SPACE
(e.which >= KEYS.A && e.which <= KEYS.Z) ||
e.which === KEYS.SPACE
) {
// upper case letters (note: keyup/keydown only return upper case letters)
// jump to countries that start with the query string
Expand Down
4 changes: 2 additions & 2 deletions src/components/TelInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ export default class TelInput extends Component {
type="tel"
autoComplete={this.props.autoComplete}
className={this.props.className}
disabled={this.props.disabled ? 'disabled' : false}
readOnly={this.props.readonly ? 'readonly' : false}
disabled={this.props.disabled}
readOnly={this.props.readonly}
name={this.props.fieldName}
id={this.props.fieldId}
value={this.props.value}
Expand Down
23 changes: 0 additions & 23 deletions src/components/__tests__/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,29 +134,6 @@ describe('utils', () => {
).toEqual({});
});

it('hasClass', () => {
const DEFAULT_HTML = `<html><body>
<div class="abc cde">test</div>
</body></html>`;
const doc = jsdom.jsdom(DEFAULT_HTML);
const element = doc.querySelector('.abc');

expect(utils.hasClass(element, 'cde')).toBeTruthy();
});

it('addClass', () => {
const DEFAULT_HTML = `<html><body>
<div class="abc cde">test</div>
</body></html>`;
const doc = jsdom.jsdom(DEFAULT_HTML);
const element = doc.querySelector('.abc');

utils.addClass(element, 'efg');

expect(element.classList.contains('efg')).toBeTruthy();
});


it('findIndex', () => {
let array = [];
let predicate = () => true;
Expand Down
12 changes: 12 additions & 0 deletions src/components/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// eslint-disable-next-line import/prefer-default-export
export const KEYS = {
UP: 38,
DOWN: 40,
ENTER: 13,
ESC: 27,
PLUS: 43,
A: 65,
Z: 90,
SPACE: 32,
TAB: 9,
};
19 changes: 1 addition & 18 deletions src/components/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,24 +170,7 @@ export default {

return {};
},

// Copied from http://jaketrent.com/post/addremove-classes-raw-javascript/
hasClass(el, className) {
if (el.classList) {
return el.classList.contains(className);
}

return !!el.className.match(new RegExp(`(\\s|^)${className}(\\s|$)`));
},

addClass(el, className) {
if (el.classList) {
el.classList.add(className);
} else if (!this.hasClass(el, className)) {
el.className += ` ${className}`;
}
},


findIndex(items, predicate) {
let index = -1;

Expand Down

0 comments on commit 00ae43f

Please sign in to comment.