File

projects/rebirth-ng/src/lib/action-button/action-button.component.ts

Metadata

exportAs actionButton
selector re-action-button
templateUrl ./action-button.component.html

Index

Properties
Methods
Inputs
Outputs
HostListeners

Constructor

constructor(elementRef: ElementRef)
Parameters :
Name Type Optional
elementRef ElementRef no

Inputs

actions

Type: ActionItem[]

btnSize

Type: "lg" | "sm" | "xs"

buttonTemplate

Type: TemplateRef<any>

buttonText

Type: string

cssClass

Type: string

direction

Type: "down" | "up"

Default value: 'down'

disabled

Default value: false

icon

Type: string

type

Type: "default" | "primary" | "success" | "info" | "warning" | "danger"

Default value: 'default'

Outputs

actionClick $event type: EventEmitter
openStatusChange $event type: EventEmitter

HostListeners

document:click
Arguments : '$event'
document:click($event: )

Methods

afterVisibilityAnimation
afterVisibilityAnimation(e: AnimationEvent)
Parameters :
Name Type Optional
e AnimationEvent no
Returns : void
close
close()
Returns : void
Private hide
hide()
Returns : void
onActionClick
onActionClick(item: ActionItem)
Parameters :
Name Type Optional
item ActionItem no
Returns : void
Private show
show()
Returns : void
toggle
toggle()
Returns : void

Properties

animateState
animateState: string
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>&nbsp;{{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>
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""