File

projects/angular-material-extensions/select-icon/src/lib/mat-select-icon/mat-select-icon.component.ts

Implements

OnInit ControlValueAccessor

Metadata

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

Index

Properties
Methods
Inputs
Outputs
Accessors

Constructor

constructor()

Inputs

icons
Type : MatSelectIcon[]
value

Outputs

onIconSelected
Type : EventEmitter<MatSelectIcon>

Methods

ngOnInit
ngOnInit()
Returns : void
registerOnChange
registerOnChange(fn: any)
Parameters :
Name Type Optional
fn any No
Returns : void
registerOnTouched
registerOnTouched(fn: any)
Parameters :
Name Type Optional
fn any No
Returns : void
select
select(icon: MatSelectIcon)
Parameters :
Name Type Optional
icon MatSelectIcon No
Returns : void
setDisabledState
setDisabledState(isDisabled: boolean)
Parameters :
Name Type Optional
isDisabled boolean No
Returns : void
writeValue
writeValue(value: MatSelectIcon)
Parameters :
Name Type Optional
value MatSelectIcon No
Returns : void

Properties

Private _value
Type : MatSelectIcon
propagateChange
Default value : () => {...}

Accessors

value
getvalue()
setvalue(value)
Parameters :
Name Optional
value No
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;
  }
}
Legend
Html element
Component
Html element with directive

result-matching ""

    No results matching ""