import { html, nothing, PropertyValues } from 'lit' import { ifDefined } from 'lit/directives/if-defined.js' import { customElement, property, state } from 'lit/decorators.js' import { Ref, createRef, ref } from 'lit/directives/ref.js' import { ElementProps } from '@/types/typeUtils' import { classMap } from 'lit/directives/class-map.js' import { PktInputElement } from '@/base-elements/input-element' import { slotContent } from '@/directives/slot-content' import '@/components/input-wrapper' import '@/components/icon' type Props = ElementProps< PktTextinput, | 'value' | 'type' | 'size' | 'autocomplete' | 'iconNameRight' | 'prefix' | 'suffix' | 'omitSearchIcon' > export class PktTextinput extends PktInputElement { inputRef: Ref = createRef() @property({ type: String, reflect: true }) value: string = '' @property({ type: String }) type: string = 'text' @property({ type: Number }) size: number | null = null @property({ type: String }) autocomplete: string | null = null @property({ type: String }) iconNameRight: string | null = null @property({ type: String }) prefix: string | null = null @property({ type: String }) suffix: string | null = null @property({ type: Boolean }) omitSearchIcon: boolean = false @state() counterCurrent = 0 attributeChangedCallback(name: string, _old: string | null, value: string | null): void { // Only sync the counter here. if (name === 'value' && this.value !== _old) { this.counterCurrent = value ? value.length : 0 } super.attributeChangedCallback(name, _old, value) } updated(changedProperties: PropertyValues) { super.updated(changedProperties) if (changedProperties.has('value')) { this.counterCurrent = this.value?.length || 0 this.valueChanged(this.value, changedProperties.get('value')) } if (changedProperties.has('id')) { !this.name && this.id && (this.name = this.id) } } render() { const shouldShowSearchIcon = this.type === 'search' && !this.iconNameRight && !this.omitSearchIcon const inputClasses = classMap({ 'pkt-input': true, [`pkt-input--${this.inputSize}`]: true, 'pkt-input--fullwidth': this.fullwidth, 'pkt-input--counter-error': this.counter && this.counterMaxLength && this.value.length && this.value.length > this.counterMaxLength, }) const labelledBy = this.ariaLabelledby || `${this.id}-input-label` return html`
${slotContent(this, 'helptext')}
${this.prefix ? html`
${this.prefix}
` : nothing} { this.value = (e.target as HTMLInputElement).value this.onInput() e.stopImmediatePropagation() }} @change=${(e: Event) => { e.stopImmediatePropagation() }} @focus=${(e: FocusEvent) => { this.onFocus() e.stopImmediatePropagation() }} @blur=${(e: FocusEvent) => { this.value = (e.target as HTMLInputElement).value this.onBlur() e.stopImmediatePropagation() }} @keydown=${(e: KeyboardEvent) => { if (e.key === 'Enter') { const form = this.internals.form as HTMLFormElement if (form) { form.requestSubmit() } else { this.inputRef.value?.blur() } } }} /> ${this.suffix ? html`
${this.suffix} ${this.iconNameRight ? html`` : nothing} ${shouldShowSearchIcon ? html`` : nothing}
` : nothing} ${!this.suffix && this.iconNameRight ? html`` : nothing} ${!this.suffix && shouldShowSearchIcon ? html`` : nothing}
` } } try { customElement('pkt-textinput')(PktTextinput) } catch (e) { console.warn('Forsøker å definere , men den er allerede definert') }