From 864a71b4c8a3e5e88669f0c011f986d19ad6aa5c Mon Sep 17 00:00:00 2001 From: Dmytro Soldatov Date: Mon, 2 Dec 2024 15:19:40 +0200 Subject: [PATCH 1/2] fix: Text input may need to prevent default/bubble --- src/Input.ts | 55 +++++++++++++++++----------------------------------- 1 file changed, 18 insertions(+), 37 deletions(-) diff --git a/src/Input.ts b/src/Input.ts index 9f54733..7ab6f2b 100644 --- a/src/Input.ts +++ b/src/Input.ts @@ -132,18 +132,7 @@ export class Input extends Container isMobile.any && this.handleActivation(); // handleActivation always call before this function called. }); - if (isMobile.any) - { - window.addEventListener('touchstart', this.handleActivationBinding); - } - else if (!isMobile.any) - { - window.addEventListener('click', this.handleActivationBinding); - - window.addEventListener('keyup', this.onKeyUpBinding); - - window.addEventListener('input', this.onInputBinding as EventListener); - } + window.addEventListener(isMobile.any ? 'touchstart' : 'click', this.handleActivationBinding); this.onEnter = new Signal(); this.onChange = new Signal(); @@ -169,7 +158,9 @@ export class Input extends Container { const key = e.key; - if (key === 'Backspace') + const keysToSkip = ['Shift', 'Control', 'Alt', 'Meta', 'ArrowLeft', 'ArrowRight', 'ArrowUp', 'ArrowDown']; + + if (keysToSkip.includes(key)) return; { this._delete(); } @@ -179,12 +170,17 @@ export class Input extends Container } else if (key.length === 1) { + this._add(key); } else if (this.lastInputData && this.lastInputData.length === 1) { this._add(this.lastInputData); } + + if (this.input) { + this.input.value = ''; + } } protected init() @@ -331,10 +327,7 @@ export class Input extends Container this.placeholder.visible = false; this._cursor.alpha = 1; - if (isMobile.any) - { - this.createInputField(); - } + this.createInputField(); this.align(); } @@ -344,7 +337,7 @@ export class Input extends Container if (this.input) { this.input.removeEventListener('blur', this.stopEditingBinding); - this.input.removeEventListener('keyup', this.onKeyUpBinding); + this.input.removeEventListener('keydown', this.onKeyUpBinding); this.input.removeEventListener('input', this.onInputBinding as EventListener); this.input?.blur(); @@ -382,7 +375,7 @@ export class Input extends Container } input.addEventListener('blur', this.stopEditingBinding); - input.addEventListener('keyup', this.onKeyUpBinding); + input.addEventListener('keydown', this.onKeyUpBinding); input.addEventListener('input', this.onInputBinding as EventListener); this.input = input; @@ -392,6 +385,8 @@ export class Input extends Container protected handleActivation() { + if (this.editing) return; + this.stopEditing(); if (this.activation) @@ -416,12 +411,9 @@ export class Input extends Container if (this.value.length === 0) this.placeholder.visible = true; - if (isMobile.any) - { - this.input?.blur(); - this.input?.remove(); - this.input = null; - } + this.input?.blur(); + this.input?.remove(); + this.input = null; this.align(); @@ -582,18 +574,7 @@ export class Input extends Container { this.off('pointertap'); - if (isMobile.any) - { - window.removeEventListener('touchstart', this.handleActivationBinding); - } - else if (!isMobile.any) - { - window.removeEventListener('click', this.handleActivationBinding); - - window.removeEventListener('keyup', this.onKeyUpBinding); - - window.removeEventListener('input', this.onInputBinding as EventListener); - } + window.removeEventListener(isMobile.any ? 'touchstart' : 'click', this.handleActivationBinding); super.destroy(options); } From eb3e41612cd6fda87e94e2edad6e1aad328c8364 Mon Sep 17 00:00:00 2001 From: Dmytro Soldatov Date: Mon, 2 Dec 2024 15:26:25 +0200 Subject: [PATCH 2/2] fix issue --- src/Input.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Input.ts b/src/Input.ts index 7ab6f2b..2522f18 100644 --- a/src/Input.ts +++ b/src/Input.ts @@ -161,6 +161,8 @@ export class Input extends Container const keysToSkip = ['Shift', 'Control', 'Alt', 'Meta', 'ArrowLeft', 'ArrowRight', 'ArrowUp', 'ArrowDown']; if (keysToSkip.includes(key)) return; + + if (key === 'Backspace') { this._delete(); }