Created
September 2, 2019 11:45
-
-
Save alonbardavid/dbaf81258f92dd61a17504d6f3b09af1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
function myComponent(selector,label,value,onChange,validations = {},format=null){ | |
let element = document.querySelector(selector); | |
element.innerHTML=``` | |
<div class="my-element"> | |
<label>${label}</label> | |
<input></input> | |
</div> | |
``` | |
let input = element.querySelector('input'); | |
input.value = value; | |
input.addEventListener('input', e=>{ | |
let raw = e.target.value; | |
if (validations.maxLength && raw.length > validations.maxLength | |
|| validations.pattern && validations.pattern.test(raw)){ | |
// for simplicity we are going to assume input events are cancelable | |
e.preventDefault(); | |
} else { | |
if (format) { | |
raw = format(raw); | |
input.value = raw; | |
} | |
onChange(raw); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment