tp-textarea/tp-textarea.js

215 lines
4.9 KiB
JavaScript
Raw Permalink Normal View History

2024-06-10 23:11:36 +02:00
/**
@license
Copyright (c) 2024 trading_peter
This program is available under Apache License Version 2.0
*/
import { FormElement } from '@tp/helpers/form-element.js';
import { DomQuery } from '@tp/helpers/dom-query.js';
2024-06-10 23:46:21 +02:00
import { ControlState } from '@tp/helpers/control-state.js';
2024-06-10 23:11:36 +02:00
import { LitElement, html, css } from 'lit';
2024-06-10 23:46:21 +02:00
class TpTextarea extends FormElement(DomQuery(ControlState(LitElement))) {
2024-06-10 23:11:36 +02:00
static get styles() {
return [
css`
:host {
2024-06-10 23:46:21 +02:00
display: block;
2024-06-10 23:11:36 +02:00
position: relative;
border: solid 1px #9E9E9E;
overflow: hidden;
}
:host([invalid]) {
border: solid 1px #B71C1C;
}
.error-message {
position: absolute;
bottom: 0;
left: 0;
right: 0;
font-size: 10px;
color: var(--tp-textarea-text-color-invalid, #B71C1C);
transition: opacity 0.3s;
opacity: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
pointer-events: none;
}
:host([invalid]) .error-message {
opacity: 1;
}
.wrap {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
2024-06-10 23:46:21 +02:00
padding: var(--tp-textarea-padding, 0);
2024-06-10 23:11:36 +02:00
}
.mirror-text {
visibility: hidden;
word-wrap: break-word;
2024-06-10 23:46:21 +02:00
padding: var(--tp-textarea-padding, 0);
2024-06-10 23:11:36 +02:00
}
.wrap ::slotted(textarea) {
position: relative;
outline: none;
border: none;
resize: none;
background: inherit;
color: inherit;
width: 100%;
height: 100%;
font-size: inherit;
font-family: inherit;
line-height: inherit;
text-align: inherit;
box-sizing: border-box;
overflow: hidden;
padding: 0;
}
`
];
}
render() {
const { errorMessage } = this;
return html`
2024-06-10 23:46:21 +02:00
<div id="mirror" class="mirror-text" part="mirror" aria-hidden="true">&nbsp;</div>
2024-06-10 23:11:36 +02:00
2024-06-10 23:46:21 +02:00
<div class="wrap" part="wrap">
2024-06-10 23:11:36 +02:00
<slot></slot>
</div>
2024-06-10 23:46:21 +02:00
<div class="error-message" part="error-message">${errorMessage}</div>
2024-06-10 23:11:36 +02:00
`;
}
static get properties() {
return {
readonly: { type: Boolean },
2024-06-10 23:46:21 +02:00
required: { type: Boolean },
2024-06-10 23:11:36 +02:00
errorMessage: { type: String },
invalid: { type: Boolean, reflect: true },
};
}
2024-06-10 23:46:21 +02:00
constructor() {
super();
this.value = '';
}
2024-06-10 23:11:36 +02:00
connectedCallback() {
super.connectedCallback();
2024-06-10 23:46:21 +02:00
this.addEventListener('input', this._onInput);
2024-06-10 23:11:36 +02:00
}
disconnectedCallback() {
super.disconnectedCallback();
2024-06-10 23:46:21 +02:00
this.removeEventListener('input', this._onInput);
2024-06-10 23:11:36 +02:00
}
2024-06-10 23:46:21 +02:00
updated(changes) {
2024-06-10 23:11:36 +02:00
if (changes.has('value')) {
this._syncToTextarea();
}
if (changes.has('readonly')) {
this._readOnlyChanged();
}
}
get textarea() {
return this.querySelector('textarea');
}
get selectionStart() {
return this.textarea.selectionStart;
}
get selectionEnd() {
return this.textarea.selectionEnd;
}
validate() {
// Use the nested input's native validity.
let valid = this.textarea.validity.valid;
// Only do extra checking if the browser thought this was valid.
if (valid) {
// Empty, required input is invalid
if (this.required && this.value === '') {
valid = false;
}
}
this.invalid = !valid;
return valid;
}
reset() {
this.value = '';
this.textarea.value = '';
this.invalid = false;
}
_syncToTextarea() {
if (typeof this.value !== 'string') {
return this.value = '';
}
if (this.textarea) {
this.textarea.value = this.value || '';
this.$.mirror.innerHTML = this._valueForMirror();
}
}
_readOnlyChanged(state) {
this.textarea.readOnly = state;
}
_onInput() {
this.$.mirror.innerHTML = this._valueForMirror();
this.value = this.textarea.value;
}
_constrain(tokens) {
let _tokens;
tokens = tokens || [''];
// Enforce the min and max heights for a multiline input to avoid measurement
if (this.maxRows > 0 && tokens.length > this.maxRows) {
_tokens = tokens.slice(0, this.maxRows);
} else {
_tokens = tokens.slice(0);
}
while (this.rows > 0 && _tokens.length < this.rows) {
_tokens.push('');
}
// Use &#160; instead &nbsp; of to allow this element to be used in XHTML.
return _tokens.join('<br/>') + '&#160;';
}
_valueForMirror() {
const input = this.textarea;
if (!input) {
return;
}
this.tokens = (input && input.value) ? input.value.replace(/&/gm, '&amp;').replace(/"/gm, '&quot;').replace(/'/gm, '&#39;').replace(/</gm, '&lt;').replace(/>/gm, '&gt;').split('\n') : [''];
return this._constrain(this.tokens);
}
_updateCached() {
this.$.mirror.innerHTML = this._constrain(this.tokens);
}
}
2024-06-10 23:46:21 +02:00
window.customElements.define('tp-textarea', TpTextarea);