Skip to content

Instantly share code, notes, and snippets.

View thisiszoaib's full-sized avatar

Zoaib thisiszoaib

View GitHub Profile
@thisiszoaib
thisiszoaib / app.module.ts
Last active January 30, 2022 07:01
Loader Network Interceptor
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule
],
providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: NetworkInterceptor,
multi: true
<div class="content">
<app-chips-multi-select [options]="options" [formControl]="chipsControl">
</app-chips-multi-select>
<mat-divider></mat-divider>
<h3>Value: {{chipsControlValue$ | async}}</h3>
<mat-divider></mat-divider>
@Component({
selector: 'app-chips-multi-select',
templateUrl: './chips-multi-select.component.html',
styleUrls: ['./chips-multi-select.component.scss'],
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: ChipsMultiSelectComponent,
multi: true,
},
disabled = false;
setDisabledState?(isDisabled: boolean): void {
this.disabled = isDisabled;
}
toggleSelection(chip: MatChip) {
if (!this.disabled) chip.toggleSelected();
}
ngAfterViewInit() {
this.chipList.chipSelectionChanges
.pipe(
untilDestroyed(this),
map((event) => event.source))
.subscribe((chip) => {
if (chip.selected) {
this.value = [...this.value, chip.value];
@ViewChild(MatChipList) chipList!: MatChipList;
value: string[] = [];
writeValue(value: string[]): void {
// When form value set when chips list initialized
if (this.chipList && value) {
this.selectChips(value);
} else if (value) {
// When chips not initialized
onChange!: (value: string[]) => void;
registerOnChange(fn: any): void {
this.onChange = fn;
}
propagateChange(value: string[]) {
if (this.onChange) {
this.onChange(value);
}
export class ChipsMultiSelectComponent implements OnInit, ControlValueAccessor {
writeValue(value: string[]): void {
}
registerOnChange(fn: any): void {
}
registerOnTouched(fn: any): void {
}
<mat-chip #c="matChip" *ngFor="let option of options" [value]="option">
<mat-icon *ngIf="c.selected">check</mat-icon>
{{option}}
</mat-chip>
<mat-chip-list selectable multiple>
<mat-chip #c="matChip" *ngFor="let option of options" [value]="option" (click)="toggleSelection(c)">
{{option}}
</mat-chip>
</mat-chip-list>