Skip to content

Commit

Permalink
refactor: clean up class switching
Browse files Browse the repository at this point in the history
  • Loading branch information
mcataford committed Mar 1, 2020
1 parent 7b02ab0 commit 07a7035
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 25 deletions.
8 changes: 4 additions & 4 deletions src/components/CountryList.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
Expand All @@ -23,16 +24,15 @@ export default class CountryList extends Component {
const shouldUpdate = !utils.shallowEquals(this.props, nextProps);

if (shouldUpdate && nextProps.showDropdown) {
this.listElement.setAttribute('class', 'country-list v-hide');
this.listElement.classList.add('v-hide');
this.setDropdownPosition();
}

return shouldUpdate;
}

setDropdownPosition = () => {
utils.removeClass(this.listElement, 'hide');

this.listElement.classList.remove('hide')
const inputTop = this.props.inputTop;
const windowTop =
window.pageYOffset !== undefined
Expand Down Expand Up @@ -60,7 +60,7 @@ export default class CountryList extends Component {
: '';

this.listElement.style.top = cssTop;
this.listElement.setAttribute('class', 'country-list');
this.listElement.classList.remove('v-hide')
};

appendListItem = (countries, isPreferred = false) => {
Expand Down
11 changes: 0 additions & 11 deletions src/components/__tests__/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,17 +156,6 @@ describe('utils', () => {
expect(element.classList.contains('efg')).toBeTruthy();
});

it('removeClass', () => {
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.removeClass(element, 'abc');

expect(element.classList.contains('abc')).toBeFalsy();
});

it('findIndex', () => {
let array = [];
Expand Down
10 changes: 0 additions & 10 deletions src/components/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,16 +188,6 @@ export default {
}
},

removeClass(el, className) {
if (el.classList) {
el.classList.remove(className);
} else if (this.hasClass(el, className)) {
const reg = new RegExp(`(\\s|^)${className}(\\s|$)`);

el.className = el.className.replace(reg, ' ');
}
},

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

Expand Down

0 comments on commit 07a7035

Please sign in to comment.