('.pkt-timepicker-popup__option'))
const currentIdx = options.indexOf(focused)
const type = focused.dataset.type ?? ''
switch (e.key) {
case 'ArrowDown':
e.preventDefault()
this._focusOptionAndSync(options[Math.min(currentIdx + 1, options.length - 1)], type)
break
case 'ArrowUp':
e.preventDefault()
this._focusOptionAndSync(options[Math.max(currentIdx - 1, 0)], type)
break
case 'Home':
e.preventDefault()
this._focusOptionAndSync(options[0], type)
break
case 'End':
e.preventDefault()
this._focusOptionAndSync(options[options.length - 1], type)
break
case 'ArrowRight':
e.preventDefault()
if (type === 'hour') {
this._focusOptionAndSync(focused, type)
this.updateComplete.then(() => {
this._scrollToSelected()
this._focusSelectedOrFirst('minute')
})
}
break
case 'ArrowLeft':
e.preventDefault()
if (type === 'minute') {
this._focusOptionAndSync(focused, type)
this.updateComplete.then(() => {
this._scrollToSelected()
this._focusSelectedOrFirst('hour')
})
}
break
case 'Enter':
case ' ':
e.preventDefault()
focused.click()
break
case 'Escape':
e.preventDefault()
this._closePopup()
this.buttonRef.value?.focus()
break
}
}
private _stepTime(direction: 1 | -1): void {
const result = stepTime(this._hours, this._minutes, direction, this._minuteStep)
this._hours = result.hours
this._minutes = result.minutes
this._syncValueFromDisplay()
}
private _renderOption(value: number, type: 'hour' | 'minute') {
const strVal = String(value).padStart(2, '0')
const currentVal =
type === 'hour'
? this._hours !== ''
? parseInt(this._hours, 10)
: NaN
: this._minutes !== ''
? parseInt(this._minutes, 10)
: NaN
const isSelected = value === currentVal
return html`
{
e.stopImmediatePropagation()
this._handleOptionClick(value, type)
}}
>
${strVal}
`
}
private _renderPopup() {
return html`
`
}
private _renderContainer() {
const hoursLabel = this.strings.timepicker?.hours ?? 'Timer'
const minutesLabel = this.strings.timepicker?.minutes ?? 'Minutter'
const hoursAriaLabel = this.label ? `${hoursLabel}, ${this.label}` : hoursLabel
const minutesAriaLabel = this.label ? `${minutesLabel}, ${this.label}` : minutesLabel
return html`
`
}
render() {
return html`
${slotContent(this, 'helptext')}
${!this.hidePicker && !this.stepArrows
? html`
${this._renderContainer()} ${this._renderPopup()}
`
: this._renderContainer()}
`
}
}
try {
customElement('pkt-timepicker')(PktTimepicker)
} catch (e) {
console.warn('Forsøker å definere , men den er allerede definert')
}