Skip to content

Commit

Permalink
added @input actions
Browse files Browse the repository at this point in the history
  • Loading branch information
raygig committed Oct 16, 2024
1 parent 2f34b9b commit 44621bd
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 20 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@firestitch/chip",
"title": "Chip",
"description": "@firestitch/chip",
"version": "18.0.0",
"version": "18.0.1",
"repository": {
"type": "git",
"url": "https://github.com/Firestitch/ngx-chip"
Expand Down
6 changes: 6 additions & 0 deletions playground/app/components/example/example.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
<mat-checkbox [(ngModel)]="removable" (ngModelChange)="removedChanged()"></mat-checkbox>
</fs-label-field>

<fs-label-field>
<fs-label>Show Actions</fs-label>
<mat-checkbox [(ngModel)]="actionable" (ngModelChange)="actionsChanged()"></mat-checkbox>
</fs-label-field>

<fs-label-field>
<fs-label>Show Selected</fs-label>
<mat-checkbox [(ngModel)]="selected" (ngModelChange)="selectedChanged()"></mat-checkbox>
Expand Down Expand Up @@ -62,6 +67,7 @@
[outlined]="outlined"
[icon]="showIcon ? 'settings' : null"
[size]="size"
[actions]="actions"
(removed)="chipRemoved($event)">
Tag A
</fs-chip>
Expand Down
15 changes: 15 additions & 0 deletions playground/app/components/example/example.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export class ExampleComponent {
public showImage;
public showIcon;
public removable;
public actionable = false;
public size;
public outlined = false;
public selected = false;
Expand All @@ -23,6 +24,7 @@ export class ExampleComponent {
public icon;
public config: any = {};
public mm = [1, 2, 3];
public actions = [];

constructor(
private _message: FsMessage,
Expand All @@ -37,6 +39,19 @@ export class ExampleComponent {
this._message.success(`Chip ${ e.selected ? 'Selected' : 'Unselected'}`);
}

public actionsChanged() {
this.actions = this.actionable ?
[
{
icon: 'help',
click: () => {
this._message.success('Action clicked');
},
},
]
: [];
}

public removedChanged() {
if (this.removable) {
this.selectable = false;
Expand Down
Binary file modified playground/assets/headshot2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 11 additions & 4 deletions src/app/components/chip/chip.component.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<img *ngIf="image" [src]="image" class="image" alt="">
<ng-container *ngIf="image">
<img [src]="image" class="image" alt="">
</ng-container>
<ng-container *ngIf="icon">
<mat-icon class="icon">{{icon}}</mat-icon>
</ng-container>
Expand All @@ -10,6 +12,11 @@
<mat-icon [style.color]="styleColor">check</mat-icon>
</div>
</ng-container>
<div *ngIf="removed.observers.length && removable" class="remove" (click)="remove($event)">
<mat-icon [style.color]="styleColor">remove_circle_outline</mat-icon>
</div>
@for (action of actions; track action.icon) {
<mat-icon
[style.color]="styleColor"
class="action"
(click)="action.click($event)">
{{action.icon}}
</mat-icon>
}
17 changes: 8 additions & 9 deletions src/app/components/chip/chip.component.scss
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
padding-left: 5px;
}

&.removable,
&.actionable,
&.selected {
padding-right: 3px;
}
Expand Down Expand Up @@ -68,7 +68,7 @@
display: flex;
}

.remove {
.action {
display: flex;
margin-left: 5px;
cursor: pointer;
Expand All @@ -89,7 +89,7 @@
margin-right: 2px;
}

.remove {
.action {
margin-left: 1px;
}

Expand All @@ -101,7 +101,7 @@
padding-left: 0;
}

&.removable,
&.actionable,
&.selected {
padding-right: 0px;
}
Expand Down Expand Up @@ -132,12 +132,11 @@
padding-left: 3px;
}

.remove {
.action {
margin-left: 2px;
margin-right: 2px;
}


.selected-check {
margin: 0 1px 0 0;
}
Expand All @@ -146,7 +145,7 @@
padding-left: 0;
}

&.removable,
&.actionable,
&.selected {
padding-right: 0px;
}
Expand Down Expand Up @@ -178,7 +177,7 @@
padding-left: 0;
}

.remove {
.action {
margin-right: 2px;
}

Expand All @@ -189,7 +188,7 @@
}
}

&.removable,
&.actionable,
&.selected {
padding-right: 0;
}
Expand Down
31 changes: 25 additions & 6 deletions src/app/components/chip/chip.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
HostBinding,
HostListener,
Input,
OnChanges,
OnDestroy,
OnInit,
Output,
} from '@angular/core';

Expand All @@ -19,7 +19,7 @@ import { Observable, Subject } from 'rxjs';
styleUrls: ['./chip.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class FsChipComponent implements OnInit, OnDestroy {
export class FsChipComponent implements OnDestroy, OnChanges {

@HostBinding('class.fs-chip')
public fsChip = true;
Expand All @@ -30,11 +30,15 @@ export class FsChipComponent implements OnInit, OnDestroy {
@Input()
@HostBinding('class.selectable')
public selectable = false;

@Input()
@HostBinding('class.removable')
public removable = true;

@Input()
@HostBinding('class.actionable')
public actionable = true;

@HostBinding('style.backgroundColor')
public styleBackgroundColor = '';

Expand All @@ -54,6 +58,12 @@ export class FsChipComponent implements OnInit, OnDestroy {
public classMicro = false;

@Input() public value;

@Input() public actions: {
icon: string,
click: (event: MouseEvent) => void,
type?: 'remove'
}[] = [];

@Input()
@HostBinding('class.iconed')
Expand Down Expand Up @@ -127,10 +137,19 @@ export class FsChipComponent implements OnInit, OnDestroy {
this._cdRef.markForCheck();
}

public ngOnInit() {
if(this.removed.observers.length === 0) {
this.removable = false;
public ngOnChanges() {
this.actions = this.actions
.filter((action) => action.type !== 'remove');

if(this.removed.observed && this.removable) {
this.actions.push({
icon: 'remove_circle_outline',
click: (event) => this.remove(event),
type: 'remove',
});
}

this.actionable = this.actions.length !== 0;
}

public ngOnDestroy() {
Expand Down

0 comments on commit 44621bd

Please sign in to comment.