Replace ControlState with custom logic to play nicely with the wrapped input.

Basically all focus handling is based on the wrapped input. tp-input is
merely a shell.
This commit is contained in:
2024-10-21 23:27:46 +02:00
parent a0b086b54b
commit 5b804a8ec3
2 changed files with 38 additions and 6 deletions

View File

@@ -1,6 +1,6 @@
{ {
"name": "@tp/tp-input", "name": "@tp/tp-input",
"version": "1.0.6", "version": "1.1.0",
"description": "", "description": "",
"main": "tp-input.js", "main": "tp-input.js",
"scripts": { "scripts": {

View File

@@ -7,13 +7,11 @@ This program is available under Apache License Version 2.0
import { LitElement, html, css } from 'lit'; import { LitElement, html, css } from 'lit';
import { FormElement } from '@tp/helpers/form-element.js'; import { FormElement } from '@tp/helpers/form-element.js';
import { EventHelpers } from '@tp/helpers/event-helpers.js'; import { EventHelpers } from '@tp/helpers/event-helpers.js';
import { ControlState } from '@tp/helpers/control-state.js';
import { Inert } from '@tp/helpers/inert.js'; import { Inert } from '@tp/helpers/inert.js';
const mixins = [ const mixins = [
FormElement, FormElement,
EventHelpers, EventHelpers,
ControlState,
Inert Inert
]; ];
@@ -82,10 +80,12 @@ class TpInput extends BaseElement {
.prefix ::slotted([slot="prefix"]) { .prefix ::slotted([slot="prefix"]) {
padding-right: 5px; padding-right: 5px;
white-space: nowrap;
} }
.suffix ::slotted([slot="suffix"]) { .suffix ::slotted([slot="suffix"]) {
padding-left: 5px; padding-left: 5px;
white-space: nowrap;
} }
` `
]; ];
@@ -115,9 +115,6 @@ class TpInput extends BaseElement {
// The value for this element. // The value for this element.
value: { type: String }, value: { type: String },
// If true the control can't be edited.
disabled: { type: Boolean, reflect: true },
// If true, something invalid was entered. // If true, something invalid was entered.
invalid: { type: Boolean, reflect: true }, invalid: { type: Boolean, reflect: true },
@@ -163,6 +160,10 @@ class TpInput extends BaseElement {
readonly: { type: Boolean, reflect: true }, readonly: { type: Boolean, reflect: true },
focused: { type: Boolean, reflect: true },
disabled: { type: Boolean, reflect: true },
/* @private */ /* @private */
_previousValidInput: { type: String, value: '' } _previousValidInput: { type: String, value: '' }
}; };
@@ -192,6 +193,8 @@ class TpInput extends BaseElement {
return; return;
} }
this.listen(this.inputEl, 'focus', '_onFocus');
this.listen(this.inputEl, 'blur', '_onBlur');
this.listen(this.inputEl, 'input', '_onInput'); this.listen(this.inputEl, 'input', '_onInput');
this.listen(this.inputEl, 'keypress', '_onKeypress'); this.listen(this.inputEl, 'keypress', '_onKeypress');
@@ -218,6 +221,14 @@ class TpInput extends BaseElement {
} }
} }
shouldUpdate(changes) {
if (changes.has('disabled')) {
this._disabledChanged(this.disabled);
}
return super.shouldUpdate(changes);
}
updated(changes) { updated(changes) {
if (changes.has('optional')) { if (changes.has('optional')) {
this._optionalChanged(changes.get('optional')); this._optionalChanged(changes.get('optional'));
@@ -266,6 +277,14 @@ class TpInput extends BaseElement {
} }
} }
_onFocus() {
this.focused = true;
}
_onBlur() {
this.focused = false;
}
_onInput() { _onInput() {
this._inputWasChanged = true; this._inputWasChanged = true;
if (this.allowedPattern && !this._patternAlreadyChecked) { if (this.allowedPattern && !this._patternAlreadyChecked) {
@@ -425,6 +444,10 @@ class TpInput extends BaseElement {
this.inputEl.focus(); this.inputEl.focus();
} }
blur() {
this.inputEl.blur();
}
/** /**
* Reset the control if a parent era-form is reset. * Reset the control if a parent era-form is reset.
*/ */
@@ -460,6 +483,15 @@ class TpInput extends BaseElement {
this.validate(); this.validate();
} }
} }
_disabledChanged(disabled) {
this.setAttribute('aria-disabled', disabled ? 'true' : 'false');
this.style.pointerEvents = disabled ? 'none' : '';
if (disabled) {
this.focused = false;
this.blur();
}
}
} }
window.customElements.define('tp-input', TpInput); window.customElements.define('tp-input', TpInput);