@oslokommune/punkt-elements
Version:
Komponentbiblioteket til Punkt, et designsystem laget av Oslo Origo
97 lines (91 loc) • 3.13 kB
text/typescript
import { html } from 'lit'
import { customElement, property } from 'lit/decorators.js'
import { classMap } from 'lit/directives/class-map.js'
import { ifDefined } from 'lit/directives/if-defined.js'
import { ref } from 'lit/directives/ref.js'
import { PktDatepickerBase } from './datepicker-base'
import { keyboardUtils, formUtils } from './datepicker-utils'
import { IDatepickerStrings, defaultSingleStrings } from './datepicker-types'
import { isIOS } from 'shared-utils/device-utils'
export class PktDatepickerSingle extends PktDatepickerBase {
value: string = ''
strings: IDatepickerStrings = defaultSingleStrings
private dispatchManageValidity(input: HTMLInputElement) {
this.dispatchEvent(
new CustomEvent('manage-validity', {
detail: input,
bubbles: true,
composed: true,
}),
)
}
render() {
return html`
<div class="pkt-input__container">
<input
class="${classMap(this.inputClasses)}"
.type=${this.inputType}
id="${this.id}-input"
.value=${this.value}
min=${ifDefined(this.min)}
max=${ifDefined(this.max)}
placeholder=${ifDefined(this.placeholder)}
?readonly=${this.isInputReadonly}
aria-describedby="${this.id}-helptext"
=${(e: MouseEvent) => {
e.preventDefault()
this.dispatchToggleCalendar(e)
}}
=${(e: TouchEvent) => {
e.preventDefault()
this.dispatchToggleCalendar(e)
}}
?disabled=${this.disabled}
=${(e: KeyboardEvent) =>
keyboardUtils.handleInputKeydown(
e,
(event) => this.dispatchToggleCalendar(event),
() =>
formUtils.submitFormOrFallback(this.internals, () => this.inputRef.value?.blur()),
undefined,
() => this.inputRef.value?.blur(),
)}
=${(e: Event) => {
this.dispatchInput(e)
e.stopImmediatePropagation()
}}
=${() => {
this.dispatchFocus()
if (isIOS()) {
this.dispatchToggleCalendar(new Event('focus'))
}
}}
=${(e: FocusEvent) => {
this.dispatchBlur(e)
this.dispatchManageValidity(e.target as HTMLInputElement)
this.dispatchEvent(
new CustomEvent('value-change', {
detail: (e.target as HTMLInputElement).value,
bubbles: true,
composed: true,
}),
)
}}
=${(e: Event) => {
this.dispatchChange(e)
e.stopImmediatePropagation()
}}
${ref(this.inputRef)}
/>
${this.renderCalendarButton()}
</div>
`
}
}
try {
customElement('pkt-datepicker-single')(PktDatepickerSingle)
} catch (e) {
console.warn('Forsøker å definere <pkt-datepicker-single>, men den er allerede definert')
}