import { html, nothing } 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, createRef, ref } from 'lit/directives/ref.js' import { PktDatepickerBase } from './datepicker-base' import { keyboardUtils, formUtils, cssUtils } from './datepicker-utils' import { IDatepickerStrings, defaultRangeStrings } from './datepicker-types' import { isIOS } from 'shared-utils/device-utils' import '@/components/icon' export class PktDatepickerRange extends PktDatepickerBase { @property({ type: Array }) value: string[] = [] @property({ type: String }) label: string = '' @property({ type: Boolean }) showRangeLabels: boolean = false @property({ type: Object }) strings: IDatepickerStrings = defaultRangeStrings inputRefTo: Ref = createRef() get inputElementTo(): HTMLInputElement | undefined { return this.inputRefTo.value } render() { const rangeLabelClasses = cssUtils.getRangeLabelClasses(this.showRangeLabels) return html`
${this.showRangeLabels ? html`
${this.strings.generic?.from}
` : nothing} { e.preventDefault() this.dispatchToggleCalendar(e) }} @touchend=${(e: TouchEvent) => { e.preventDefault() this.dispatchToggleCalendar(e) }} @keydown=${(e: KeyboardEvent) => keyboardUtils.handleInputKeydown( e, (event) => this.dispatchToggleCalendar(event), () => formUtils.submitFormOrFallback(this.internals, () => this.inputRefTo.value?.focus(), ), () => this.inputRefTo.value?.focus(), () => this.inputRef.value?.blur(), )} @input=${(e: Event) => { this.dispatchInput(e) e.stopImmediatePropagation() }} @focus=${() => { this.dispatchFocus() if (isIOS()) { this.dispatchToggleCalendar(new Event('focus')) } }} @blur=${(e: FocusEvent) => { this.dispatchBlur(e) this.dispatchEvent( new CustomEvent('range-blur', { detail: { event: e, values: this.value, inputType: 'from', }, bubbles: true, composed: true, }), ) }} @change=${(e: Event) => { this.dispatchChange(e) e.stopImmediatePropagation() }} ${ref(this.inputRef)} />
${this.strings.generic?.to}
${!this.showRangeLabels ? html`
` : nothing} { e.preventDefault() this.dispatchToggleCalendar(e) }} @touchend=${(e: TouchEvent) => { e.preventDefault() this.dispatchToggleCalendar(e) }} @keydown=${(e: KeyboardEvent) => keyboardUtils.handleInputKeydown( e, (event) => this.dispatchToggleCalendar(event), () => formUtils.submitFormOrFallback(this.internals, () => this.inputRefTo.value?.blur()), undefined, () => this.inputRefTo.value?.blur(), )} @input=${(e: Event) => { this.dispatchInput(e) e.stopImmediatePropagation() }} @focus=${() => { this.dispatchFocus() if (isIOS()) { this.dispatchToggleCalendar(new Event('focus')) } }} @blur=${(e: FocusEvent) => { this.dispatchBlur(e) this.dispatchEvent( new CustomEvent('range-blur', { detail: { event: e, values: this.value, inputType: 'to', }, bubbles: true, composed: true, }), ) }} @change=${(e: Event) => { this.dispatchChange(e) e.stopImmediatePropagation() }} ${ref(this.inputRefTo)} /> ${this.renderCalendarButton()}
` } } try { customElement('pkt-datepicker-range')(PktDatepickerRange) } catch (e) { console.warn('Forsøker å definere , men den er allerede definert') }