Created
March 28, 2018 16:56
-
-
Save MinDBreaK/caad0c18f3d65dddeabd417aca49ce39 to your computer and use it in GitHub Desktop.
Material Design Select dropdown. It will automatically target all selects available in the DOM.
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
| .select-wrapper { | |
| display: block; | |
| position: relative; | |
| box-sizing: border-box; | |
| height: 60px; | |
| } | |
| .select-wrapper ul { | |
| position: absolute; | |
| list-style: none; | |
| margin: 0; | |
| padding: 0; | |
| top: 52px; | |
| background-color: #fff; | |
| box-shadow: 0 2px 5px 0 rgba(0,0,0,0.16),0 2px 10px 0 rgba(0,0,0,0.12); | |
| max-height: 180px; | |
| overflow-y: auto; | |
| z-index: 999; | |
| opacity: 0; | |
| pointer-events: none; | |
| transition: all 0.2s ease; | |
| width: 100%; | |
| } | |
| .select-wrapper.active ul { | |
| opacity: 1; | |
| pointer-events: auto; | |
| } | |
| .select-wrapper ul li { | |
| clear: both; | |
| color: rgba(0,0,0,0.87); | |
| cursor: pointer; | |
| min-height: 50px; | |
| line-height: 1.5rem; | |
| width: 100%; | |
| text-align: left; | |
| text-transform: none; | |
| transition: all 0.2s ease; | |
| } | |
| .select-wrapper ul li:hover { | |
| background-color: #eee; | |
| } | |
| .select-wrapper ul li>span { | |
| font-size: 16px; | |
| color: #26a69a; | |
| display: block; | |
| line-height: 22px; | |
| padding: 14px 16px; | |
| } | |
| .select-wrapper input.select-dropdown { | |
| position: relative; | |
| display: block; | |
| cursor: pointer; | |
| background-color: transparent; | |
| border: none; | |
| border-bottom: 1px solid #9e9e9e; | |
| outline: none; | |
| height: 50px; | |
| line-height: 50px; | |
| width: 100%; | |
| font-size: 1rem; | |
| margin: 0 0 20px 0; | |
| padding: 0; | |
| } | |
| .select-wrapper select { | |
| display: none; | |
| } |
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
| "use strict"; | |
| function generateMaterialSelect(select_previous, options) { | |
| let container = document.createElement("div"); | |
| container.classList.add("select-wrapper"); | |
| let input = document.createElement('input'); | |
| input.classList.add('select-dropdown'); | |
| input.type = "text"; | |
| input.value = "Select:"; | |
| input.readOnly = true; | |
| let select = document.createElement("select"); | |
| select.id = select_previous.id; | |
| select.classList = select_previous.classList; | |
| select.name = select_previous.name; | |
| let ul = document.createElement("ul"); | |
| for (let i = 0; i < options.length; i++) { | |
| let value = options.item(i); | |
| let li = document.createElement("li"); | |
| let option = document.createElement("option"); | |
| option.value = options.item(i).value; | |
| option.text = options.item(i).text; | |
| option.selected = options.item(i).selected; | |
| if (option.selected) { | |
| input.value = option.text; | |
| } | |
| li.dataset.value = value.value; | |
| let span = document.createElement("span"); | |
| span.innerText = value.text; | |
| li.appendChild(span); | |
| ul.appendChild(li); | |
| select.appendChild(option); | |
| } | |
| select.value = select_previous.value; | |
| container.appendChild( input ); | |
| container.appendChild( select ); | |
| container.appendChild( ul ); | |
| return container; | |
| } | |
| let selects = document.getElementsByTagName( "select" ); | |
| for (let i=0; i < selects.length; i++) { | |
| let options = selects.item(i).options; | |
| let newSelect = generateMaterialSelect(selects.item(i), options); | |
| selects.item(i).replaceWith(newSelect); | |
| } | |
| let dropdowns = document.getElementsByClassName("select-dropdown"); | |
| for (let i = 0; i < dropdowns.length; i++) { | |
| dropdowns.item( i ).onclick = function(event) { | |
| if (event.target.parentElement.classList.contains("active")) { | |
| event.target.parentElement.classList.remove('active'); | |
| } else { | |
| event.target.parentElement.classList.add("active"); | |
| } | |
| } | |
| } | |
| let list_elements = document.querySelectorAll(".select-wrapper > ul > li"); | |
| for (let i = 0; i < list_elements.length; i++) { | |
| list_elements.item( i ).onclick = function (event) { | |
| let element = event.target.parentElement; | |
| let parent = element.parentElement.parentElement; | |
| let value = element.dataset.value; | |
| let text = event.target.innerHTML; | |
| parent.firstElementChild.value = text; | |
| parent.getElementsByTagName("select")[0].value = value; | |
| parent.classList.remove("active"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment