Last active
May 16, 2024 08:56
-
-
Save filiphlm/4ed4789c56ded46ad3df16727befa3a4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class TimeInput extends HTMLInputElement | |
{ | |
static is = 'time-input' | |
connectedCallback () | |
{ | |
this.addEventListener('input', this) | |
} | |
disconnectedCalback () | |
{ | |
this.removeEventListener('input', this) | |
} | |
/** | |
* @param {InputEvent} event | |
*/ | |
handleEvent ( event ) | |
{ | |
const value = this.value.replace(/[^0-9]/g, '') | |
if (value.length > 2) { | |
let hours = value.slice(0, 2), | |
minutes = value.slice(2) | |
parseInt(hours) > 23 && (hours = '00') | |
parseInt(minutes) > 59 && (minutes = '59') | |
this.value = hours + ':' + minutes | |
} | |
} | |
} | |
customElements.get(TimeInput.is) === void 0 && customElements.define(TimeInput.is, TimeInput, { extends: 'input' }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment