Choices/src/scripts/components/input.ts

148 lines
3.2 KiB
TypeScript
Raw Normal View History

2019-10-28 20:27:10 +01:00
import { sanitise } from '../lib/utils';
import { SELECT_ONE_TYPE } from '../constants';
import { ClassNames } from '../interfaces/class-names';
import { PassedElementType } from '../interfaces/passed-element-type';
2017-08-17 14:50:14 +02:00
export default class Input {
element: HTMLInputElement;
type: PassedElementType;
classNames: ClassNames;
preventPaste: boolean;
isFocussed: boolean;
isDisabled: boolean;
constructor({
element,
type,
classNames,
preventPaste,
}: {
element: HTMLInputElement;
type: PassedElementType;
classNames: ClassNames;
preventPaste: boolean;
}) {
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;
this.isFocussed = this.element.isEqualNode(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);
}
set placeholder(placeholder: string) {
2018-04-24 13:54:45 +02:00
this.element.placeholder = placeholder;
}
get value(): string {
return sanitise(this.element.value);
2017-10-18 14:05:07 +02:00
}
set value(value: string) {
this.element.value = value;
}
addEventListeners(): void {
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(): void {
this.element.removeEventListener('input', this._onInput);
2018-05-25 10:22:14 +02:00
this.element.removeEventListener('paste', this._onPaste);
this.element.removeEventListener('focus', this._onFocus);
this.element.removeEventListener('blur', this._onBlur);
2017-08-27 14:49:35 +02:00
}
enable(): void {
2017-08-23 14:32:07 +02:00
this.element.removeAttribute('disabled');
2017-08-29 13:56:54 +02:00
this.isDisabled = false;
2017-08-23 14:32:07 +02:00
}
disable(): void {
2017-08-23 14:32:07 +02:00
this.element.setAttribute('disabled', '');
2017-08-29 13:56:54 +02:00
this.isDisabled = true;
2017-08-23 14:32:07 +02:00
}
focus(): void {
2017-08-27 14:49:35 +02:00
if (!this.isFocussed) {
this.element.focus();
}
}
blur(): void {
if (this.isFocussed) {
this.element.blur();
}
}
clear(setWidth = true): this {
2017-08-21 09:53:19 +02:00
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
*/
setWidth(): void {
2019-10-28 20:27:10 +01:00
// 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
}
setActiveDescendant(activeDescendantID: string): void {
2017-10-12 10:35:58 +02:00
this.element.setAttribute('aria-activedescendant', activeDescendantID);
2017-08-27 14:49:35 +02:00
}
removeActiveDescendant(): void {
2017-08-27 14:49:35 +02:00
this.element.removeAttribute('aria-activedescendant');
}
2018-05-25 10:22:14 +02:00
_onInput(): void {
if (this.type !== SELECT_ONE_TYPE) {
2018-05-25 10:22:14 +02:00
this.setWidth();
}
}
2018-05-28 17:22:22 +02:00
_onPaste(event: ClipboardEvent): void {
if (this.preventPaste) {
2018-05-28 17:22:22 +02:00
event.preventDefault();
2018-05-25 10:22:14 +02:00
}
}
_onFocus(): void {
2018-05-25 10:22:14 +02:00
this.isFocussed = true;
}
_onBlur(): void {
2018-05-25 10:22:14 +02:00
this.isFocussed = false;
}
2017-08-17 14:50:14 +02:00
}