From f1f136ab7323e49614e56befcd5b65f7a1abd6fa Mon Sep 17 00:00:00 2001 From: Maicol Battistini Date: Tue, 30 Nov 2021 10:54:36 +0100 Subject: [PATCH] fix(textfield): Gestione dell'aggiunta/rimozione di un'icona --- .idea/inspectionProfiles/Project_Default.xml | 3 +- resources/js/WebComponents/TextField.js | 60 +++++++++++++------- 2 files changed, 40 insertions(+), 23 deletions(-) diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml index 3af56facc..a3e41bbd7 100644 --- a/.idea/inspectionProfiles/Project_Default.xml +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -99,7 +99,7 @@ diff --git a/resources/js/WebComponents/TextField.js b/resources/js/WebComponents/TextField.js index 887fa5f0e..08a20086b 100644 --- a/resources/js/WebComponents/TextField.js +++ b/resources/js/WebComponents/TextField.js @@ -1,6 +1,5 @@ import {TextField as MWCTextField} from '@material/mwc-textfield'; import {waitUntil} from 'async-wait-until'; -import classnames from 'classnames'; import { html, type TemplateResult @@ -10,15 +9,52 @@ import { export default class TextField extends MWCTextField { async connectedCallback() { super.connectedCallback(); + + // Wait until slots are added to DOM await waitUntil(() => this.shadowRoot.querySelectorAll('slot[name^=icon]').length > 0); + const slots = this.shadowRoot.querySelectorAll('slot[name^=icon]'); for (const slot: HTMLSlotElement of slots) { + const slotClass = `mdc-text-field__icon--${slot.name === 'icon' ? 'leading' : 'trailing'}`; + const rootClass = `mdc-text-field--with-${slot.name === 'icon' ? 'leading' : 'trailing'}-icon`; + const slotParent = slot.parentElement; + const rootElement = this.shadowRoot.firstElementChild; + + // Check if slot has content if (slot.assignedNodes().length > 0) { - this[slot.name] = ' '; + slotParent.classList.add(slotClass); + rootElement.classList.add(rootClass); } + + // Listen for changes in slot (added/removed) + slot.addEventListener('slotchange', () => { + if (slot.assignedNodes().length > 0) { + slotParent.classList.add(slotClass); + rootElement.classList.add(rootClass); + } else { + slotParent.classList.remove(slotClass); + rootElement.classList.remove(rootClass); + } + }); } } + renderLeadingIcon() { + return this.renderIcon(); + } + + renderTrailingIcon() { + return this.renderIcon(true); + } + + renderIcon(isTrailingIcon: boolean = false): TemplateResult { + return html` + + + + `; + } + get nativeValidationMessage() { return this.formElement.validationMessage; } @@ -46,26 +82,6 @@ export default class TextField extends MWCTextField { } return isValid; } - - renderLeadingIcon() { - return this.renderIcon(); - } - - renderTrailingIcon() { - return this.renderIcon(true); - } - - renderIcon(isTrailingIcon: boolean = false): TemplateResult { - const classes = { - 'mdc-text-field__icon--leading': !isTrailingIcon, - 'mdc-text-field__icon--trailing': isTrailingIcon - }; - - return html` - - - `; - } } window.customElements.define('text-field', TextField);