Move input specific events to input component

This commit is contained in:
Josh Johnson 2017-08-23 13:23:37 +01:00
parent 852837cd37
commit fc7b53350a
2 changed files with 42 additions and 30 deletions

View file

@ -213,8 +213,6 @@ class Choices {
this._onTouchEnd = this._onTouchEnd.bind(this);
this._onMouseDown = this._onMouseDown.bind(this);
this._onMouseOver = this._onMouseOver.bind(this);
this._onPaste = this._onPaste.bind(this);
this._onInput = this._onInput.bind(this);
// Monitor touch taps/scrolls
this.wasTap = true;
@ -1494,10 +1492,10 @@ class Choices {
this.containerOuter.element.addEventListener('blur', this._onBlur);
}
this.input.element.addEventListener('input', this._onInput);
this.input.element.addEventListener('paste', this._onPaste);
this.input.element.addEventListener('focus', this._onFocus);
this.input.element.addEventListener('blur', this._onBlur);
this.input.addEventListeners();
}
/**
@ -1519,10 +1517,10 @@ class Choices {
this.containerOuter.element.removeEventListener('blur', this._onBlur);
}
this.input.element.removeEventListener('input', this._onInput);
this.input.element.removeEventListener('paste', this._onPaste);
this.input.element.removeEventListener('focus', this._onFocus);
this.input.element.removeEventListener('blur', this._onBlur);
this.input.removeEventListeners();
}
/**
@ -1751,17 +1749,6 @@ class Choices {
this.canSearch = this.config.searchEnabled;
}
/**
* Input event
* @return
* @private
*/
_onInput() {
if (!this.isSelectOneElement) {
this.input.setWidth();
}
}
/**
* Touch move event
* @return
@ -1911,19 +1898,6 @@ class Choices {
}
}
/**
* Paste event
* @param {Object} e Event
* @return
* @private
*/
_onPaste(e) {
// Disable pasting into the input if option has been set
if (e.target === this.input.element && !this.config.paste) {
e.preventDefault();
}
}
/**
* Focus event
* @param {Object} e Event

View file

@ -8,6 +8,44 @@ export default class Input {
this.instance = instance;
this.element = element;
this.classNames = classNames;
// Bind event listeners
this.onPaste = this.onPaste.bind(this);
this.onInput = this.onInput.bind(this);
}
addEventListeners() {
this.element.addEventListener('input', this.onInput);
this.element.addEventListener('paste', this.onPaste);
}
removeEventListeners() {
this.element.removeEventListener('input', this.onInput);
this.element.removeEventListener('paste', this.onPaste);
}
/**
* Input event
* @return
* @private
*/
onInput() {
if (!this.instance.isSelectOneElement) {
this.setWidth();
}
}
/**
* Paste event
* @param {Object} e Event
* @return
* @private
*/
onPaste(e) {
// Disable pasting into the input if option has been set
if (e.target === this.element && !this.instance.config.paste) {
e.preventDefault();
}
}
/**