| 22 | } |
| 23 | |
| 24 | addField(args){ |
| 25 | if(!args.options) throw new ReferenceError('No options defined for DropdownField'); |
| 26 | |
| 27 | this.classList.add(...this.classes.field); |
| 28 | const wrapper = this.querySelector(this.selectors.wrapper); |
| 29 | wrapper.classList.add(...this.classes.fieldInputWrapper); |
| 30 | |
| 31 | let input = document.createElement('select'); |
| 32 | input.classList.add(...this.classes.fieldInput); |
| 33 | input.placeholder = (args.placeholder) ? args.placeholder : ''; |
| 34 | input.name = this.fieldName; |
| 35 | input.id = this.fieldId; |
| 36 | |
| 37 | if(args.defaultValue){ |
| 38 | const defaultOption = document.createElement("option"); |
| 39 | defaultOption.disabled = true; |
| 40 | defaultOption.selected = true; |
| 41 | defaultOption.innerText = args.defaultValue; |
| 42 | |
| 43 | input.add(defaultOption); |
| 44 | input.placeholder = (args.placeholder) ? args.placeholder : args.defaultValue; |
| 45 | } |
| 46 | |
| 47 | args.options.forEach(option => { |
| 48 | let o = document.createElement("option"); |
| 49 | o.innerText = option.text; |
| 50 | o.value = option.value; |
| 51 | input.add(o); |
| 52 | }); |
| 53 | |
| 54 | let arrow = document.createElement('div'); |
| 55 | arrow.classList.add(...this.classes.arrow); |
| 56 | |
| 57 | let iconSVG = document.createElementNS('http://www.w3.org/2000/svg','svg'); |
| 58 | iconSVG.classList.add(...this.classes.svgIcon); |
| 59 | iconSVG.setAttribute('role', 'presentation'); |
| 60 | iconSVG.focusable = 'false'; |
| 61 | |
| 62 | let use = document.createElementNS('http://www.w3.org/2000/svg', 'use'); |
| 63 | use.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', '#caret-down'); |
| 64 | use.setAttributeNS('http://www.w3.org/1999/xlink', 'href', '#caret-down'); |
| 65 | |
| 66 | iconSVG.appendChild(use); |
| 67 | arrow.appendChild(iconSVG); |
| 68 | |
| 69 | wrapper.appendChild(input); |
| 70 | wrapper.appendChild(arrow); |
| 71 | } |
| 72 | } |