* Helper for making Elements with class name and attributes * * @param {string} tagName - new Element tag name * @param {string[]|string} [classNames] - list or name of CSS class name(s) * @param {object} [attributes] - any attributes * @returns {HTMLElement}
(tagName: string, classNames: string | (string | undefined)[] | null = null, attributes: object = {})
| 55 | * @returns {HTMLElement} |
| 56 | */ |
| 57 | public static make(tagName: string, classNames: string | (string | undefined)[] | null = null, attributes: object = {}): HTMLElement { |
| 58 | const el = document.createElement(tagName); |
| 59 | |
| 60 | if (Array.isArray(classNames)) { |
| 61 | const validClassnames = classNames.filter(className => className !== undefined) as string[]; |
| 62 | |
| 63 | el.classList.add(...validClassnames); |
| 64 | } else if (classNames) { |
| 65 | el.classList.add(classNames); |
| 66 | } |
| 67 | |
| 68 | for (const attrName in attributes) { |
| 69 | if (Object.prototype.hasOwnProperty.call(attributes, attrName)) { |
| 70 | el[attrName] = attributes[attrName]; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return el; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Creates Text Node with the passed content |
no test coverage detected