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 {
|
2019-10-29 18:35:20 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @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;
|
2019-10-29 18:35:20 +01:00
|
|
|
this.type = type;
|
2017-10-10 13:56:36 +02:00
|
|
|
this.classNames = classNames;
|
2019-10-29 18:35:20 +01:00
|
|
|
this.preventPaste = preventPaste;
|
|
|
|
|
2017-08-27 14:49:35 +02:00
|
|
|
this.isFocussed = this.element === document.activeElement;
|
2019-10-29 18:35:20 +01:00
|
|
|
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);
|
2017-08-23 14:23:37 +02:00
|
|
|
}
|
|
|
|
|
2018-04-24 13:54:45 +02:00
|
|
|
set placeholder(placeholder) {
|
|
|
|
this.element.placeholder = placeholder;
|
|
|
|
}
|
|
|
|
|
|
|
|
set value(value) {
|
2019-02-22 23:04:55 +01:00
|
|
|
this.element.value = value;
|
2018-04-24 13:54:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get value() {
|
2019-02-22 23:04:55 +01:00
|
|
|
return sanitise(this.element.value);
|
2017-10-18 14:05:07 +02:00
|
|
|
}
|
|
|
|
|
2017-08-23 14:23:37 +02:00
|
|
|
addEventListeners() {
|
2018-05-25 10:22:14 +02:00
|
|
|
this.element.addEventListener('input', this._onInput);
|
|
|
|
this.element.addEventListener('paste', this._onPaste);
|
|
|
|
this.element.addEventListener('focus', this._onFocus);
|
|
|
|
this.element.addEventListener('blur', this._onBlur);
|
2018-05-28 17:22:22 +02:00
|
|
|
|
|
|
|
if (this.element.form) {
|
|
|
|
this.element.form.addEventListener('reset', this._onFormReset);
|
|
|
|
}
|
2017-08-23 14:23:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
removeEventListeners() {
|
2018-05-25 10:22:14 +02:00
|
|
|
this.element.removeEventListener('input', this._onInput);
|
|
|
|
this.element.removeEventListener('paste', this._onPaste);
|
|
|
|
this.element.removeEventListener('focus', this._onFocus);
|
|
|
|
this.element.removeEventListener('blur', this._onBlur);
|
2018-05-28 17:22:22 +02:00
|
|
|
|
|
|
|
if (this.element.form) {
|
|
|
|
this.element.form.removeEventListener('reset', this._onFormReset);
|
|
|
|
}
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-10 17:41:39 +01:00
|
|
|
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) {
|
2019-10-29 18:35:20 +01:00
|
|
|
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
|
|
|
}
|