I'm trying to create a dumb UI component in Angular that's completely free from logic. Lets say I have a List component that gets an array of data as input. This is rendered as a plain list where each item can be clicked, causing an emit of a delete event (since no logic should be put inside the dumb component)
Now, the smart component that listens to this output triggers a confirm dialog. On YES, I want the dumb component to animate this removal from the list. On NO, do nothing. By updating the array for the input the data is taken care of, but what way could I inform the list to animate based of the condition from the smart component?
A simple solution would of course be to support a confirm dialog inside the dumb component but that would make it less dumb. Perhaps I don't want this confirm dialog in all scenarios, perhaps another dialog, or perhaps a db call to check if the user is allowed to etc...
@Component({
selector: 'app-list',
templateUrl: `
<ol>
<li *ngFor="let item of items; let i = index" (click)="delete(i)">
{{ item }}
</li>
</ol>`,
styleUrls: ['./list.component.css']
})
export class ListComponent<T> {
@Input()
public list: T[];
@Output()
public readonly deleteItem: EventEmitter<number> = new EventEmitter();
public delete(index: number): void {
this.deleteItem.emit(index);
}
}