Created
September 2, 2019 11:44
-
-
Save alonbardavid/2de1061a13778b355ff622a7c67330f4 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 = {}){ | |
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 { | |
onChange(raw); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment