projects/rebirth-ng/src/lib/action-button/action-button.component.ts
exportAs | actionButton |
selector | re-action-button |
templateUrl | ./action-button.component.html |
Properties |
Methods |
|
Inputs |
Outputs |
HostListeners |
constructor(elementRef: ElementRef)
|
||||||
Parameters :
|
actions
|
Type: |
btnSize
|
Type: |
buttonTemplate
|
Type: |
buttonText
|
Type: |
cssClass
|
Type: |
direction
|
Type:
Default value: |
disabled
|
Default value: |
icon
|
Type: |
type
|
Type:
Default value: |
actionClick
|
$event type: EventEmitter
|
openStatusChange
|
$event type: EventEmitter
|
document:click |
Arguments : '$event'
|
document:click($event: )
|
afterVisibilityAnimation | ||||||
afterVisibilityAnimation(e: AnimationEvent)
|
||||||
Parameters :
Returns :
void
|
close |
close()
|
Returns :
void
|
Private hide |
hide()
|
Returns :
void
|
onActionClick | ||||||
onActionClick(item: ActionItem)
|
||||||
Parameters :
Returns :
void
|
Private show |
show()
|
Returns :
void
|
toggle |
toggle()
|
Returns :
void
|
animateState |
animateState:
|
Type : string
|
Default value : 'initial'
|
isOpen |
isOpen:
|
Default value : false
|
import { Component, Input, EventEmitter, HostListener, Output, TemplateRef, ElementRef } from '@angular/core';
import { ActionItem } from './action-item.model';
import { trigger, state, style, animate, transition, AnimationEvent } from '@angular/animations';
@Component({
selector: 're-action-button',
templateUrl: './action-button.component.html',
exportAs: 'actionButton',
animations: [
trigger('state', [
state('void', style({ transform: 'scale(0)' })),
state('initial', style({ transform: 'scale(0)' })),
state('visible', style({ transform: 'scale(1)' })),
state('hidden', style({ transform: 'scale(0)' })),
transition('* => visible', animate('150ms cubic-bezier(0.0, 0.0, 0.2, 1)')),
transition('* => hidden', animate('150ms cubic-bezier(0.4, 0.0, 1, 1)')),
])
]
})
export class ActionButtonComponent {
@Input() type: 'default' | 'primary' | 'success' | 'info' | 'warning' | 'danger' = 'default';
@Input() direction: 'down' | 'up' = 'down';
@Input() icon: string;
@Input() btnSize: 'lg' | 'sm' | 'xs';
@Input() buttonText: string;
@Input() cssClass: string;
@Input() actions: ActionItem[];
@Input() disabled = false;
@Input() buttonTemplate: TemplateRef<any>;
@Output() actionClick = new EventEmitter<ActionItem>();
@Output() openStatusChange = new EventEmitter<boolean>();
animateState = 'initial';
isOpen = false;
constructor(private elementRef: ElementRef) {
}
toggle() {
if (!this.isOpen) {
this.show();
} else {
this.hide();
}
this.openStatusChange.emit(this.isOpen);
}
afterVisibilityAnimation(e: AnimationEvent) {
if (e.toState === 'hidden' && this.isOpen) {
this.isOpen = false;
}
}
@HostListener('document:click', ['$event'])
onDocumentClick($event) {
if (!this.elementRef.nativeElement.contains($event.target)) {
this.close();
}
}
close() {
if (this.isOpen) {
this.hide();
this.openStatusChange.emit(this.isOpen);
}
}
onActionClick(item: ActionItem) {
if (!item.disabled) {
this.close();
this.actionClick.emit(item);
}
}
private show() {
this.isOpen = true;
this.animateState = 'visible';
}
private hide() {
this.animateState = 'hidden';
}
}
<ng-template #defaultButtonTemplate let-action>
<button class="btn {{action.type? 'btn-' + action.type : ''}} {{action.btnSize? 'btn-' + action.btnSize : ''}}"
(click)="action.toggle($event)"
[disabled]="action.disabled">
<i *ngIf="action.icon" class="{{action.icon}}" aria-hidden="true"></i>
{{action.buttonText}}
</button>
<button type="button"
class="btn {{action.type? 'btn-' + action.type : ''}} {{action.btnSize? 'btn-' + action.btnSize : ''}} dropdown-toggle"
aria-haspopup="true"
[attr.aria-expanded]="true"
(click)="action.toggle()" [disabled]="action.disabled">
<span class="caret"></span>
<span class="sr-only">|</span>
</button>
</ng-template>
<div class="btn-group {{direction? 'drop' + direction : '' }} {{cssClass}}" [ngClass]="{open: isOpen}">
<ng-template [ngTemplateOutlet]="buttonTemplate || defaultButtonTemplate"
[ngTemplateOutletContext]="{ $implicit: this}"></ng-template>
<ul class="dropdown-menu" role="menu" [@state]="animateState" (@state.done)="afterVisibilityAnimation($event)">
<ng-container *ngFor="let item of actions">
<li *ngIf="!item.divider && ! item.header" role="menuitem" [ngClass]="{'disabled': item.disabled}"
(click)="onActionClick(item)">
<a><i *ngIf="item.icon" class="{{item.icon}}"></i> {{item.text}}</a></li>
<li *ngIf="item.header" class="dropdown-header">{{item.text}}</li>
<li *ngIf="item.divider" class="divider" role="separator"></li>
</ng-container>
</ul>
</div>