From c9c1eab01409d31c2f55b373dc9a50f97187d4eb Mon Sep 17 00:00:00 2001 From: Josh Johnson Date: Wed, 23 Aug 2017 13:32:07 +0100 Subject: [PATCH] Abstract more input methods: --- assets/scripts/src/choices.js | 24 ++++++--------------- assets/scripts/src/components/input.js | 30 ++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/assets/scripts/src/choices.js b/assets/scripts/src/choices.js index e1bfebf..ade88fa 100644 --- a/assets/scripts/src/choices.js +++ b/assets/scripts/src/choices.js @@ -748,11 +748,7 @@ class Choices { showDropdown(focusInput = false) { this.containerOuter.open(this.dropdown.getPosition()); this.dropdown.show(); - - // Optionally focus the input if we have a search input - if (focusInput && this.canSearch && document.activeElement !== this.input.element) { - this.input.element.focus(); - } + this.input.activate(focusInput); triggerEvent(this.passedElement, 'showDropdown', {}); return this; @@ -766,15 +762,7 @@ class Choices { hideDropdown(blurInput = false) { this.containerOuter.close(); this.dropdown.hide(); - - // IE11 ignores aria-label and blocks virtual keyboard - // if aria-activedescendant is set without a dropdown - this.input.element.removeAttribute('aria-activedescendant'); - - // Optionally blur the input if we have a search input - if (blurInput && this.canSearch && document.activeElement === this.input.element) { - this.input.element.blur(); - } + this.input.deactivate(blurInput); triggerEvent(this.passedElement, 'hideDropdown', {}); return this; @@ -1028,7 +1016,7 @@ class Choices { if (isDisabled) { this._addEventListeners(); this.passedElement.removeAttribute('disabled'); - this.input.element.removeAttribute('disabled'); + this.input.enable(); this.containerOuter.enable(); } @@ -1051,7 +1039,7 @@ class Choices { if (isEnabled) { this._removeEventListeners(); this.passedElement.setAttribute('disabled', ''); - this.input.element.setAttribute('disabled', ''); + this.input.disable(); this.containerOuter.disable(); } @@ -1337,7 +1325,7 @@ class Choices { placeholderItem.innerHTML = this.config.loadingText; } } else { - this.input.element.placeholder = this.config.loadingText; + this.input.setPlaceholder(this.config.loadingText); } } else { // Remove loading states/text @@ -1346,7 +1334,7 @@ class Choices { if (this.isSelectOneElement) { placeholderItem.innerHTML = (this.placeholder || ''); } else { - this.input.element.placeholder = (this.placeholder || ''); + this.input.setPlaceholder(this.placeholder || ''); } } } diff --git a/assets/scripts/src/components/input.js b/assets/scripts/src/components/input.js index eed988b..02eab1d 100644 --- a/assets/scripts/src/components/input.js +++ b/assets/scripts/src/components/input.js @@ -48,6 +48,32 @@ export default class Input { } } + activate(focusInput) { + // Optionally focus the input if we have a search input + if (focusInput && this.instance.canSearch && document.activeElement !== this.element) { + this.element.focus(); + } + } + + deactivate(blurInput) { + // IE11 ignores aria-label and blocks virtual keyboard + // if aria-activedescendant is set without a dropdown + this.element.removeAttribute('aria-activedescendant'); + + // Optionally blur the input if we have a search input + if (blurInput && this.instance.canSearch && document.activeElement === this.element) { + this.element.blur(); + } + } + + enable() { + this.element.removeAttribute('disabled'); + } + + disable() { + this.element.setAttribute('disabled', ''); + } + /** * Set value of input to blank * @return {Object} Class instance @@ -85,4 +111,8 @@ export default class Input { this.element.style.width = getWidthOfInput(this.element); } } + + setPlaceholder(placeholder) { + this.element.placeholder = placeholder; + } }