Last active
July 12, 2022 09:01
-
-
Save Sroose/3d8a25b469ecc4e556558d5caa986be1 to your computer and use it in GitHub Desktop.
Ionic Angular directive to remove the arrow from an ion-select (and make placeholder look same as other elements)
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
/** | |
* Author: Sam Roose | |
*/ | |
import { Directive, ElementRef } from '@angular/core'; | |
@Directive({ | |
selector: '[appNoArrow]' | |
}) | |
export class NoArrowDirective { | |
private observer: MutationObserver; | |
constructor(private el: ElementRef) { | |
const node = this.el.nativeElement; | |
this.observer = new MutationObserver((mutations) => { | |
// Mutations arrived, try to remove arrow | |
this.removeArrow(); | |
}); | |
this.observer.observe(node, { | |
childList: true, | |
}); | |
} | |
private removeArrow() { | |
// Check if the arrow element is already here | |
if (this.el.nativeElement.shadowRoot.querySelector('.select-icon') === null) { | |
// Note yet, ignore this mutation | |
return; | |
} | |
// This mutation has added the arrow. Remove it. | |
this.el.nativeElement.shadowRoot.querySelector('.select-icon').setAttribute('style', 'display: none !important'); | |
// Also set the placeholder text to the same color as other placeholders | |
// If a default value was selected, no placeholder element will be present, so check for null on this one again | |
if (this.el.nativeElement.shadowRoot.querySelector('.select-placeholder') !== null) { | |
this.el.nativeElement.shadowRoot.querySelector('.select-placeholder').setAttribute('style', 'opacity: 1; color: #999 !important'); | |
} | |
// Stop listening for mutations | |
this.observer.disconnect(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank you so much