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
@NgModule({ | |
declarations: [AppComponent], | |
imports: [ | |
BrowserModule | |
], | |
providers: [ | |
{ | |
provide: HTTP_INTERCEPTORS, | |
useClass: NetworkInterceptor, | |
multi: true |
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
<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> |
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
@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, | |
}, |
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
disabled = false; | |
setDisabledState?(isDisabled: boolean): void { | |
this.disabled = isDisabled; | |
} | |
toggleSelection(chip: MatChip) { | |
if (!this.disabled) chip.toggleSelected(); | |
} |
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
ngAfterViewInit() { | |
this.chipList.chipSelectionChanges | |
.pipe( | |
untilDestroyed(this), | |
map((event) => event.source)) | |
.subscribe((chip) => { | |
if (chip.selected) { | |
this.value = [...this.value, chip.value]; |
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
@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 |
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
onChange!: (value: string[]) => void; | |
registerOnChange(fn: any): void { | |
this.onChange = fn; | |
} | |
propagateChange(value: string[]) { | |
if (this.onChange) { | |
this.onChange(value); | |
} |
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
export class ChipsMultiSelectComponent implements OnInit, ControlValueAccessor { | |
writeValue(value: string[]): void { | |
} | |
registerOnChange(fn: any): void { | |
} | |
registerOnTouched(fn: any): void { | |
} |
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
<mat-chip #c="matChip" *ngFor="let option of options" [value]="option"> | |
<mat-icon *ngIf="c.selected">check</mat-icon> | |
{{option}} | |
</mat-chip> |
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
<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> |
NewerOlder