Update dropdown methods

This commit is contained in:
Josh Johnson 2017-08-16 13:01:17 +01:00
parent b86b8f0ed0
commit 67fe5d7dda
3 changed files with 20 additions and 27 deletions

View file

@ -754,8 +754,7 @@ class Choices {
this.containerOuter.classList.add(this.config.classNames.openState); this.containerOuter.classList.add(this.config.classNames.openState);
this.containerOuter.setAttribute('aria-expanded', 'true'); this.containerOuter.setAttribute('aria-expanded', 'true');
this.dropdown.element.classList.add(this.config.classNames.activeState); this.dropdown.show();
this.dropdown.element.setAttribute('aria-expanded', 'true');
const dimensions = this.dropdown.element.getBoundingClientRect(); const dimensions = this.dropdown.element.getBoundingClientRect();
const dropdownPos = Math.ceil(dimensions.top + window.scrollY + this.dropdown.offsetHeight); const dropdownPos = Math.ceil(dimensions.top + window.scrollY + this.dropdown.offsetHeight);
@ -794,8 +793,7 @@ class Choices {
this.containerOuter.classList.remove(this.config.classNames.openState); this.containerOuter.classList.remove(this.config.classNames.openState);
this.containerOuter.setAttribute('aria-expanded', 'false'); this.containerOuter.setAttribute('aria-expanded', 'false');
this.dropdown.element.classList.remove(this.config.classNames.activeState); this.dropdown.hide();
this.dropdown.element.setAttribute('aria-expanded', 'false');
if (isFlipped) { if (isFlipped) {
this.containerOuter.classList.remove(this.config.classNames.flippedState); this.containerOuter.classList.remove(this.config.classNames.flippedState);
@ -817,13 +815,7 @@ class Choices {
* @public * @public
*/ */
toggleDropdown() { toggleDropdown() {
const hasActiveDropdown = this.dropdown.element.classList.contains(this.config.classNames.activeState); this.dropdown.toggle();
if (hasActiveDropdown) {
this.hideDropdown();
} else {
this.showDropdown(true);
}
return this; return this;
} }
@ -1246,7 +1238,7 @@ class Choices {
const id = element.getAttribute('data-id'); const id = element.getAttribute('data-id');
const choice = this.store.getChoiceById(id); const choice = this.store.getChoiceById(id);
const passedKeyCode = activeItems[0] && activeItems[0].keyCode ? activeItems[0].keyCode : null; const passedKeyCode = activeItems[0] && activeItems[0].keyCode ? activeItems[0].keyCode : null;
const hasActiveDropdown = this.dropdown.element.classList.contains(this.config.classNames.activeState); const hasActiveDropdown = this.dropdown.isActive;
// Update choice keyCode // Update choice keyCode
choice.keyCode = passedKeyCode; choice.keyCode = passedKeyCode;
@ -1602,7 +1594,7 @@ class Choices {
const target = e.target; const target = e.target;
const activeItems = this.store.getItemsFilteredByActive(); const activeItems = this.store.getItemsFilteredByActive();
const hasFocusedInput = this.input === document.activeElement; const hasFocusedInput = this.input === document.activeElement;
const hasActiveDropdown = this.dropdown.element.classList.contains(this.config.classNames.activeState); const hasActiveDropdown = this.dropdown.isActive;
const hasItems = this.itemList && this.itemList.children; const hasItems = this.itemList && this.itemList.children;
const keyString = String.fromCharCode(e.keyCode); const keyString = String.fromCharCode(e.keyCode);
@ -1678,7 +1670,7 @@ class Choices {
const onEscapeKey = () => { const onEscapeKey = () => {
if (hasActiveDropdown) { if (hasActiveDropdown) {
this.toggleDropdown(); this.dropdown.hide();
this.containerOuter.focus(); this.containerOuter.focus();
} }
}; };
@ -1843,7 +1835,7 @@ class Choices {
*/ */
_onTouchEnd(e) { _onTouchEnd(e) {
const target = e.target || e.touches[0].target; const target = e.target || e.touches[0].target;
const hasActiveDropdown = this.dropdown.element.classList.contains(this.config.classNames.activeState); const hasActiveDropdown = this.dropdown.isActive;
// If a user tapped within our container... // If a user tapped within our container...
if (this.wasTap === true && this.containerOuter.contains(target)) { if (this.wasTap === true && this.containerOuter.contains(target)) {
@ -1911,7 +1903,7 @@ class Choices {
*/ */
_onClick(e) { _onClick(e) {
const target = e.target; const target = e.target;
const hasActiveDropdown = this.dropdown.element.classList.contains(this.config.classNames.activeState); const hasActiveDropdown = this.dropdown.isActive;
const activeItems = this.store.getItemsFilteredByActive(); const activeItems = this.store.getItemsFilteredByActive();
// If target is something that concerns us // If target is something that concerns us
@ -2707,7 +2699,7 @@ class Choices {
this.input = input; this.input = input;
this.choiceList = choiceList; this.choiceList = choiceList;
this.itemList = itemList; this.itemList = itemList;
this.dropdown = new Dropdown(this, dropdown); this.dropdown = new Dropdown(this, dropdown, this.config.classNames);
// Hide passed input // Hide passed input
this.passedElement.classList.add( this.passedElement.classList.add(

View file

@ -1,7 +1,8 @@
export default class Dropdown { export default class Dropdown {
constructor(instance, element) { constructor(instance, element, classNames) {
this.instance = instance; this.instance = instance;
this.element = element; this.element = element;
this.classNames = classNames;
this.dimensions = this.element.getBoundingClientRect(); this.dimensions = this.element.getBoundingClientRect();
this.position = Math.ceil(this.dimensions.top + window.scrollY + this.element.offsetHeight); this.position = Math.ceil(this.dimensions.top + window.scrollY + this.element.offsetHeight);
this.isActive = false; this.isActive = false;
@ -14,9 +15,9 @@ export default class Dropdown {
*/ */
toggle() { toggle() {
if (this.isActive) { if (this.isActive) {
this.hideDropdown(); this.hide();
} else { } else {
this.showDropdown(); this.show();
} }
return this.instance; return this.instance;
@ -28,8 +29,8 @@ export default class Dropdown {
* @public * @public
*/ */
show() { show() {
this.dropdown.classList.add(this.config.classNames.activeState); this.element.classList.add(this.classNames.activeState);
this.dropdown.setAttribute('aria-expanded', 'true'); this.element.setAttribute('aria-expanded', 'true');
this.isActive = true; this.isActive = true;
return this.instance; return this.instance;
} }
@ -40,7 +41,7 @@ export default class Dropdown {
* @public * @public
*/ */
hide() { hide() {
this.element.classList.remove(this.config.classNames.activeState); this.element.classList.remove(this.classNames.activeState);
this.element.setAttribute('aria-expanded', 'false'); this.element.setAttribute('aria-expanded', 'false');
this.isActive = false; this.isActive = false;
return this.instance; return this.instance;

View file

@ -763,10 +763,10 @@ describe('Choices', () => {
}); });
it('should handle toggleDropdown()', function() { it('should handle toggleDropdown()', function() {
spyOn(this.choices, 'hideDropdown'); spyOn(this.choices.dropdown, 'hide');
this.choices.showDropdown(); this.choices.dropdown.show();
this.choices.toggleDropdown(); this.choices.dropdown.toggle();
expect(this.choices.hideDropdown).toHaveBeenCalled(); expect(this.choices.dropdown.hide).toHaveBeenCalled();
}); });
it('should handle hideDropdown()', function() { it('should handle hideDropdown()', function() {