@oslokommune/punkt-elements
Version:
Komponentbiblioteket til Punkt, et designsystem laget av Oslo Origo
102 lines (94 loc) • 3.11 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'
import '@/components/icon'
export class PktDatepickerMultiple extends PktDatepickerBase {
value: string[] = []
maxlength?: number
strings: IDatepickerStrings = defaultSingleStrings
get isInputDisabled(): boolean {
return (
this.disabled ||
(this.maxlength !== undefined &&
this.maxlength !== null &&
this.value.length >= this.maxlength)
)
}
private dispatchAddToSelected(e: Event | KeyboardEvent) {
this.dispatchEvent(
new CustomEvent('add-to-selected', {
detail: e,
bubbles: true,
composed: true,
}),
)
}
render() {
return html`
<div class="pkt-input__container">
<input
class=${classMap(this.inputClasses)}
.type=${this.inputType}
id="${this.id}-input"
min=${ifDefined(this.min)}
max=${ifDefined(this.max)}
placeholder=${ifDefined(this.placeholder)}
?readonly=${this.isInputReadonly}
?disabled=${this.isInputDisabled}
=${(e: MouseEvent) => {
e.preventDefault()
this.dispatchToggleCalendar(e)
}}
=${(e: TouchEvent) => {
e.preventDefault()
this.dispatchToggleCalendar(e)
}}
=${(e: FocusEvent) => {
this.dispatchBlur(e)
this.dispatchAddToSelected(e)
}}
=${(e: Event) => {
this.dispatchInput(e)
e.stopImmediatePropagation()
}}
=${() => {
this.dispatchFocus()
if (isIOS()) {
this.dispatchToggleCalendar(new Event('focus'))
}
}}
=${(e: KeyboardEvent) =>
keyboardUtils.handleInputKeydown(
e,
(event) => this.dispatchToggleCalendar(event),
() =>
formUtils.submitFormOrFallback(this.internals, () => this.inputRef.value?.blur()),
undefined,
undefined,
(event) => this.dispatchAddToSelected(event),
)}
=${(e: Event) => {
this.dispatchChange(e)
e.stopImmediatePropagation()
}}
${ref(this.inputRef)}
/>
${this.renderCalendarButton()}
</div>
`
}
}
try {
customElement('pkt-datepicker-multiple')(PktDatepickerMultiple)
} catch (e) {
console.warn('Forsøker å definere <pkt-datepicker-multiple>, men den er allerede definert')
}