Files
dees-catalog/ts_web/elements/dees-input-text.ts

278 lines
7.9 KiB
TypeScript
Raw Normal View History

2024-01-21 01:12:57 +01:00
import * as colors from './00colors.js';
import { DeesInputBase } from './dees-input-base.js';
import { demoFunc } from './dees-input-text.demo.js';
2024-01-21 01:12:57 +01:00
2023-10-23 16:13:02 +02:00
import {
customElement,
type TemplateResult,
property,
html,
cssManager,
css,
} from '@design.estate/dees-element';
2020-09-13 16:24:48 +00:00
2021-03-06 15:48:02 +00:00
declare global {
interface HTMLElementTagNameMap {
'dees-input-text': DeesInputText;
}
}
2020-09-13 16:24:48 +00:00
@customElement('dees-input-text')
export class DeesInputText extends DeesInputBase {
public static demo = demoFunc;
2020-09-13 16:24:48 +00:00
2021-08-20 00:25:14 +02:00
// INSTANCE
2021-08-26 21:30:35 +02:00
@property({
2023-04-06 17:32:49 +02:00
type: String,
reflect: true,
2021-08-26 21:30:35 +02:00
})
2021-09-01 21:48:22 +02:00
public value: string = '';
2020-09-13 16:24:48 +00:00
2023-01-16 11:51:21 +01:00
@property({
type: Boolean,
reflect: true,
})
public isPasswordBool = false;
@property({
type: Boolean,
reflect: true,
})
public showPasswordBool = false;
2023-10-23 16:13:02 +02:00
@property({
type: Boolean,
reflect: true,
})
public validationState: 'valid' | 'warn' | 'invalid';
2020-09-13 16:24:48 +00:00
2023-10-23 16:13:02 +02:00
@property({
reflect: true,
})
public validationText: string = '';
@property({})
validationFunction: (value: string) => boolean;
public static styles = [
...DeesInputBase.baseStyles,
2023-10-23 16:13:02 +02:00
cssManager.defaultStyles,
css`
* {
box-sizing: border-box;
}
:host {
position: relative;
z-index: auto;
2025-06-27 16:20:06 +00:00
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', sans-serif;
2023-10-23 16:13:02 +02:00
}
.maincontainer {
2025-06-27 16:20:06 +00:00
position: relative;
color: ${cssManager.bdTheme('hsl(0 0% 15%)', 'hsl(0 0% 90%)')};
2023-10-23 16:13:02 +02:00
}
input {
2025-06-27 16:20:06 +00:00
display: flex;
height: 40px;
2023-10-23 16:13:02 +02:00
width: 100%;
2025-06-27 16:20:06 +00:00
padding: 0 12px;
font-size: 14px;
line-height: 40px;
background: transparent;
border: 1px solid ${cssManager.bdTheme('hsl(0 0% 89.8%)', 'hsl(0 0% 14.9%)')};
border-radius: 6px;
transition: all 0.15s ease;
2023-10-23 16:13:02 +02:00
outline: none;
2025-06-27 16:20:06 +00:00
cursor: text;
font-family: inherit;
color: ${cssManager.bdTheme('hsl(0 0% 9%)', 'hsl(0 0% 95%)')};
2023-10-23 16:13:02 +02:00
}
2025-06-27 16:20:06 +00:00
input::placeholder {
color: ${cssManager.bdTheme('hsl(0 0% 63.9%)', 'hsl(0 0% 45.1%)')};
}
input:hover:not(:disabled):not(:focus) {
border-color: ${cssManager.bdTheme('hsl(0 0% 79.8%)', 'hsl(0 0% 20.9%)')};
2023-10-23 16:13:02 +02:00
}
input:focus {
outline: none;
2025-06-27 16:20:06 +00:00
border-color: ${cssManager.bdTheme('hsl(222.2 47.4% 51.2%)', 'hsl(217.2 91.2% 59.8%)')};
box-shadow: 0 0 0 3px ${cssManager.bdTheme('hsl(222.2 47.4% 51.2% / 0.1)', 'hsl(217.2 91.2% 59.8% / 0.1)')};
2023-12-26 21:21:18 +01:00
}
2025-06-27 16:20:06 +00:00
input:disabled {
background: ${cssManager.bdTheme('hsl(0 0% 95.1%)', 'hsl(0 0% 14.9%)')};
border-color: ${cssManager.bdTheme('hsl(0 0% 89.8%)', 'hsl(0 0% 14.9%)')};
color: ${cssManager.bdTheme('hsl(0 0% 63.9%)', 'hsl(0 0% 45.1%)')};
cursor: not-allowed;
opacity: 0.5;
2023-10-23 16:13:02 +02:00
}
2025-06-27 16:20:06 +00:00
/* Password toggle button */
2023-10-23 16:13:02 +02:00
.showPassword {
position: absolute;
2025-06-27 16:20:06 +00:00
right: 1px;
top: 50%;
transform: translateY(-50%);
display: flex;
align-items: center;
justify-content: center;
width: 38px;
height: 38px;
cursor: pointer;
color: ${cssManager.bdTheme('hsl(0 0% 45.1%)', 'hsl(0 0% 63.9%)')};
transition: all 0.15s ease;
border-radius: 0 5px 5px 0;
2023-10-23 16:13:02 +02:00
}
.showPassword:hover {
2025-06-27 16:20:06 +00:00
background: ${cssManager.bdTheme('hsl(0 0% 95.1%)', 'hsl(0 0% 14.9%)')};
color: ${cssManager.bdTheme('hsl(0 0% 15%)', 'hsl(0 0% 93.9%)')};
2023-10-23 16:13:02 +02:00
}
2025-06-27 16:20:06 +00:00
/* Validation styles */
2023-10-23 16:13:02 +02:00
.validationContainer {
2025-06-27 16:20:06 +00:00
margin-top: 4px;
padding: 4px 8px;
2023-10-23 16:13:02 +02:00
font-size: 12px;
2025-06-27 16:20:06 +00:00
font-weight: 500;
border-radius: 4px;
transition: all 0.2s ease;
overflow: hidden;
}
.validationContainer.error {
background: ${cssManager.bdTheme('hsl(0 84.2% 60.2% / 0.1)', 'hsl(0 72.2% 50.6% / 0.1)')};
color: ${cssManager.bdTheme('hsl(0 84.2% 60.2%)', 'hsl(0 72.2% 50.6%)')};
}
.validationContainer.warn {
background: ${cssManager.bdTheme('hsl(25 95% 53% / 0.1)', 'hsl(25 95% 63% / 0.1)')};
color: ${cssManager.bdTheme('hsl(25 95% 53%)', 'hsl(25 95% 63%)')};
}
.validationContainer.valid {
background: ${cssManager.bdTheme('hsl(142.1 76.2% 36.3% / 0.1)', 'hsl(142.1 70.6% 45.3% / 0.1)')};
color: ${cssManager.bdTheme('hsl(142.1 76.2% 36.3%)', 'hsl(142.1 70.6% 45.3%)')};
}
/* Error state for input */
:host([validation-state="invalid"]) input {
border-color: ${cssManager.bdTheme('hsl(0 84.2% 60.2%)', 'hsl(0 72.2% 50.6%)')};
}
:host([validation-state="invalid"]) input:focus {
box-shadow: 0 0 0 3px ${cssManager.bdTheme('hsl(0 84.2% 60.2% / 0.1)', 'hsl(0 72.2% 50.6% / 0.1)')};
}
/* Warning state for input */
:host([validation-state="warn"]) input {
border-color: ${cssManager.bdTheme('hsl(25 95% 53%)', 'hsl(25 95% 63%)')};
}
:host([validation-state="warn"]) input:focus {
box-shadow: 0 0 0 3px ${cssManager.bdTheme('hsl(25 95% 53% / 0.1)', 'hsl(25 95% 63% / 0.1)')};
}
/* Valid state for input */
:host([validation-state="valid"]) input {
border-color: ${cssManager.bdTheme('hsl(142.1 76.2% 36.3%)', 'hsl(142.1 70.6% 45.3%)')};
}
:host([validation-state="valid"]) input:focus {
box-shadow: 0 0 0 3px ${cssManager.bdTheme('hsl(142.1 76.2% 36.3% / 0.1)', 'hsl(142.1 70.6% 45.3% / 0.1)')};
2023-10-23 16:13:02 +02:00
}
`,
];
2020-09-13 16:24:48 +00:00
2023-10-23 16:13:02 +02:00
public render(): TemplateResult {
return html`
<style>
2020-09-13 16:24:48 +00:00
input {
2025-06-27 16:20:06 +00:00
font-family: ${this.isPasswordBool ? 'SF Mono, Monaco, Consolas, Liberation Mono, Courier New, monospace' : 'inherit'};
letter-spacing: ${this.isPasswordBool ? '0.5px' : 'normal'};
padding-right: ${this.isPasswordBool ? '48px' : '12px'};
2021-08-26 21:30:35 +02:00
}
${this.validationText
? css`
.validationContainer {
2025-06-27 16:20:06 +00:00
height: auto;
opacity: 1;
2025-06-27 16:20:06 +00:00
transform: translateY(0);
}
`
: css`
.validationContainer {
2025-06-27 16:20:06 +00:00
height: 0;
padding: 0 !important;
opacity: 0;
2025-06-27 16:20:06 +00:00
transform: translateY(-4px);
}
`}
2020-09-13 16:24:48 +00:00
</style>
<div class="input-wrapper">
2025-06-27 16:20:06 +00:00
<dees-label .label=${this.label} .description=${this.description} .required=${this.required}></dees-label>
<div class="maincontainer">
<input
type="${this.isPasswordBool && !this.showPasswordBool ? 'password' : 'text'}"
.value=${this.value}
@input="${this.updateValue}"
.disabled=${this.disabled}
2025-06-27 16:20:06 +00:00
placeholder="${this.label ? '' : 'Enter text...'}"
/>
${this.isPasswordBool
? html`
<div class="showPassword" @click=${this.togglePasswordView}>
2025-06-27 16:20:06 +00:00
<dees-icon .iconName=${this.showPasswordBool ? 'lucideEye' : 'lucideEyeOff'}></dees-icon>
</div>
`
: html``}
2025-06-27 16:20:06 +00:00
${this.validationText
? html`
<div class="validationContainer ${this.validationState || 'error'}">
${this.validationText}
</div>
`
: html`<div class="validationContainer"></div>`}
2023-10-23 16:13:02 +02:00
</div>
2020-09-13 16:24:48 +00:00
</div>
`;
}
2023-10-23 16:13:02 +02:00
firstUpdated() {
// Input event handling is already done in updateValue method
2023-10-23 16:13:02 +02:00
}
2020-09-13 16:24:48 +00:00
public async updateValue(eventArg: Event) {
const target: any = eventArg.target;
this.value = target.value;
2021-08-20 00:25:14 +02:00
this.changeSubject.next(this);
2020-09-13 16:24:48 +00:00
}
2021-08-26 21:30:35 +02:00
public getValue(): string {
return this.value;
2021-08-26 21:30:35 +02:00
}
public setValue(value: string): void {
this.value = value;
2021-08-26 21:30:35 +02:00
}
2023-01-16 11:51:21 +01:00
2023-10-23 16:13:02 +02:00
public async togglePasswordView() {
2023-01-16 11:51:21 +01:00
this.showPasswordBool = !this.showPasswordBool;
}
2023-03-27 01:22:15 +02:00
public async focus() {
const textInput = this.shadowRoot.querySelector('input');
textInput.focus();
}
2023-04-12 02:47:45 +02:00
public async blur() {
const textInput = this.shadowRoot.querySelector('input');
textInput.blur();
}
2020-09-13 16:24:48 +00:00
}