Choices/src/scripts/src/components/input.js

160 lines
3.7 KiB
JavaScript
Raw Normal View History

2017-08-21 17:59:56 +02:00
import { getWidthOfInput } from '../lib/utils';
2017-08-17 14:50:14 +02:00
export default class Input {
2017-10-10 13:56:36 +02:00
constructor(instance, element, classNames) {
2017-08-17 14:50:14 +02:00
this.instance = instance;
this.element = element;
2017-10-10 13:56:36 +02:00
this.classNames = classNames;
2017-08-27 14:49:35 +02:00
this.isFocussed = this.element === document.activeElement;
2017-08-29 13:56:54 +02:00
this.isDisabled = false;
// Bind event listeners
this.onPaste = this.onPaste.bind(this);
this.onInput = this.onInput.bind(this);
2017-08-27 14:49:35 +02:00
this.onFocus = this.onFocus.bind(this);
this.onBlur = this.onBlur.bind(this);
}
addEventListeners() {
this.element.addEventListener('input', this.onInput);
this.element.addEventListener('paste', this.onPaste);
2017-08-27 14:49:35 +02:00
this.element.addEventListener('focus', this.onFocus);
this.element.addEventListener('blur', this.onBlur);
}
removeEventListeners() {
this.element.removeEventListener('input', this.onInput);
this.element.removeEventListener('paste', this.onPaste);
2017-08-27 14:49:35 +02:00
this.element.removeEventListener('focus', this.onFocus);
this.element.removeEventListener('blur', this.onBlur);
}
/**
* 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();
}
2017-08-17 14:50:14 +02:00
}
2017-08-21 09:53:19 +02:00
2017-08-29 13:56:54 +02:00
/**
* Set focussed state
*/
2017-08-27 14:49:35 +02:00
onFocus() {
this.isFocussed = true;
}
2017-08-29 13:56:54 +02:00
/**
* Remove focussed state
*/
2017-08-27 14:49:35 +02:00
onBlur() {
this.isFocussed = false;
}
2017-08-23 14:32:07 +02:00
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) {
2017-08-27 14:49:35 +02:00
this.removeActiveDescendant();
2017-08-23 14:32:07 +02:00
// 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');
2017-08-29 13:56:54 +02:00
this.isDisabled = false;
2017-08-23 14:32:07 +02:00
}
disable() {
this.element.setAttribute('disabled', '');
2017-08-29 13:56:54 +02:00
this.isDisabled = true;
2017-08-23 14:32:07 +02:00
}
2017-08-27 14:49:35 +02:00
focus() {
if (!this.isFocussed) {
this.element.focus();
}
}
2017-08-21 09:53:19 +02:00
/**
* Set value of input to blank
* @return {Object} Class instance
* @public
*/
clear(setWidth = true) {
if (this.element.value) {
this.element.value = '';
}
if (setWidth) {
2017-08-21 17:59:56 +02:00
this.setWidth();
2017-08-21 09:53:19 +02:00
}
return this.instance;
}
2017-08-21 17:59:56 +02:00
/**
* Set the correct input width based on placeholder
* value or input value
* @return
*/
2017-08-27 14:49:35 +02:00
setWidth(enforceWidth) {
2017-08-21 17:59:56 +02:00
if (this.instance.placeholder) {
// If there is a placeholder, we only want to set the width of the input when it is a greater
// length than 75% of the placeholder. This stops the input jumping around.
if (
2017-08-27 14:49:35 +02:00
(this.element.value &&
this.element.value.length >= (this.instance.placeholder.length / 1.25)) ||
enforceWidth
2017-08-21 17:59:56 +02:00
) {
this.element.style.width = getWidthOfInput(this.element);
}
} else {
// If there is no placeholder, resize input to contents
this.element.style.width = getWidthOfInput(this.element);
}
}
2017-08-23 14:32:07 +02:00
setPlaceholder(placeholder) {
this.element.placeholder = placeholder;
}
2017-08-27 14:49:35 +02:00
setValue(value) {
this.element.value = value;
}
getValue() {
return this.element.value;
}
setActiveDescendant(activeDescendant) {
this.element.setAttribute('aria-activedescendant', activeDescendant);
}
removeActiveDescendant() {
this.element.removeAttribute('aria-activedescendant');
}
2017-08-17 14:50:14 +02:00
}