projects/angular-material-extensions/select-icon/src/lib/mat-select-icon/mat-select-icon.component.ts
providers |
{
provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => MatSelectIconComponent), multi: true
}
|
selector | mat-select-icon |
styleUrls | ./mat-select-icon.component.scss |
templateUrl | ./mat-select-icon.component.html |
Properties |
|
Methods |
Inputs |
Outputs |
Accessors |
constructor()
|
icons | |
Type : MatSelectIcon[]
|
|
value | |
onIconSelected | |
Type : EventEmitter<MatSelectIcon>
|
|
ngOnInit |
ngOnInit()
|
Returns :
void
|
registerOnChange | ||||||
registerOnChange(fn: any)
|
||||||
Parameters :
Returns :
void
|
registerOnTouched | ||||||
registerOnTouched(fn: any)
|
||||||
Parameters :
Returns :
void
|
select | ||||||
select(icon: MatSelectIcon)
|
||||||
Parameters :
Returns :
void
|
setDisabledState | ||||||
setDisabledState(isDisabled: boolean)
|
||||||
Parameters :
Returns :
void
|
writeValue | ||||||
writeValue(value: MatSelectIcon)
|
||||||
Parameters :
Returns :
void
|
Private _value |
Type : MatSelectIcon
|
propagateChange |
Default value : () => {...}
|
value | ||||
getvalue()
|
||||
setvalue(value)
|
||||
Parameters :
Returns :
void
|
import { Component, EventEmitter, forwardRef, Input, OnInit, Output } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { ThemePalette } from '@angular/material/core';
export interface MatSelectIcon {
url: string;
value?: any;
color?: ThemePalette;
tags?: string[]; // todo: 10.2020
}
@Component({
selector: 'mat-select-icon',
templateUrl: './mat-select-icon.component.html',
styleUrls: ['./mat-select-icon.component.scss'],
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => MatSelectIconComponent),
multi: true
}
]
})
export class MatSelectIconComponent implements OnInit, ControlValueAccessor {
propagateChange = (_: any) => {
};
@Input()
icons: MatSelectIcon[];
@Output()
onIconSelected: EventEmitter<MatSelectIcon> = new EventEmitter<MatSelectIcon>();
constructor() {
}
private _value: MatSelectIcon;
get value(): MatSelectIcon {
return this._value;
}
@Input()
set value(value: MatSelectIcon) {
this._value = value;
this.propagateChange(this._value);
}
ngOnInit(): void {
}
select(icon: MatSelectIcon) {
this.value = icon;
this.onIconSelected.next(this.value);
}
writeValue(value: MatSelectIcon) {
if (value !== undefined) {
this.value = value;
}
}
registerOnChange(fn: any): void {
this.propagateChange = fn;
}
registerOnTouched(fn: any): void {
}
setDisabledState(isDisabled: boolean): void {
}
}
<div class="dep-img" fxLayout="row wrap">
<button (click)="select(icon)"
*ngFor="let icon of this.icons"
[color]="icon?.color ? icon.color : 'accent'"
class="dep-img"
mat-icon-button
type="button">
<img [ngClass]="{'gray': this.value && this.value !== icon}"
[src]="icon?.url"
class="mat-icon">
</button>
</div>
./mat-select-icon.component.scss
.dep-img {
.mat-icon-button {
width: 80px;
height: 80px;
}
img.gray {
-webkit-filter: grayscale(100%); /* Safari 6.0 - 9.0 */
filter: grayscale(100%);
}
img.mat-icon {
width: 48px;
height: 48px;
}
}