Choices/src/scripts/components/input.js

140 lines
3.1 KiB
JavaScript
Raw Normal View History

2019-10-28 20:27:10 +01:00
import { sanitise } from '../lib/utils';
2017-08-21 17:59:56 +02:00
2017-08-17 14:50:14 +02:00
export default class Input {
/**
*
* @typedef {import('../../../types/index').Choices.passedElement} passedElement
* @typedef {import('../../../types/index').Choices.ClassNames} ClassNames
* @param {{element: HTMLInputElement, type: passedElement['type'], classNames: ClassNames, preventPaste: boolean }} p
*/
constructor({ element, type, classNames, preventPaste }) {
2017-08-17 14:50:14 +02:00
this.element = element;
this.type = type;
2017-10-10 13:56:36 +02:00
this.classNames = classNames;
this.preventPaste = preventPaste;
2017-08-27 14:49:35 +02:00
this.isFocussed = this.element === document.activeElement;
this.isDisabled = element.disabled;
2018-05-25 10:22:14 +02:00
this._onPaste = this._onPaste.bind(this);
this._onInput = this._onInput.bind(this);
this._onFocus = this._onFocus.bind(this);
this._onBlur = this._onBlur.bind(this);
}
2018-04-24 13:54:45 +02:00
set placeholder(placeholder) {
this.element.placeholder = placeholder;
}
get value() {
return sanitise(this.element.value);
2017-10-18 14:05:07 +02:00
}
set value(value) {
this.element.value = value;
}
addEventListeners() {
2018-05-25 10:22:14 +02:00
this.element.addEventListener('paste', this._onPaste);
2019-10-29 18:46:10 +01:00
this.element.addEventListener('input', this._onInput, {
passive: true,
});
this.element.addEventListener('focus', this._onFocus, {
passive: true,
});
this.element.addEventListener('blur', this._onBlur, {
passive: true,
});
}
removeEventListeners() {
2019-10-29 18:46:10 +01:00
this.element.removeEventListener('input', this._onInput, {
passive: true,
});
2018-05-25 10:22:14 +02:00
this.element.removeEventListener('paste', this._onPaste);
2019-10-29 18:46:10 +01:00
this.element.removeEventListener('focus', this._onFocus, {
passive: true,
});
this.element.removeEventListener('blur', this._onBlur, {
passive: true,
});
2017-08-27 14:49:35 +02:00
}
2017-08-23 14:32:07 +02:00
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();
}
}
blur() {
if (this.isFocussed) {
this.element.blur();
}
}
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
}
2018-05-21 18:01:03 +02:00
return this;
2017-08-21 09:53:19 +02:00
}
2017-08-21 17:59:56 +02:00
/**
* Set the correct input width based on placeholder
* value or input value
*/
2019-10-28 20:27:10 +01:00
setWidth() {
// Resize input to contents or placeholder
const { style, value, placeholder } = this.element;
style.minWidth = `${placeholder.length + 1}ch`;
style.width = `${value.length + 1}ch`;
2017-08-27 14:49:35 +02:00
}
2017-10-12 10:35:58 +02:00
setActiveDescendant(activeDescendantID) {
this.element.setAttribute('aria-activedescendant', activeDescendantID);
2017-08-27 14:49:35 +02:00
}
removeActiveDescendant() {
this.element.removeAttribute('aria-activedescendant');
}
2018-05-25 10:22:14 +02:00
_onInput() {
if (this.type !== 'select-one') {
this.setWidth();
}
}
2018-05-28 17:22:22 +02:00
_onPaste(event) {
if (this.preventPaste) {
2018-05-28 17:22:22 +02:00
event.preventDefault();
2018-05-25 10:22:14 +02:00
}
}
_onFocus() {
this.isFocussed = true;
}
_onBlur() {
this.isFocussed = false;
}
2017-08-17 14:50:14 +02:00
}