File

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

Implements

ControlValueAccessor

Metadata

changeDetection ChangeDetectionStrategy.OnPush
host {
}
providers { : , : (() => ), : true }
selector re-select-button
templateUrl ./select-button.component.html

Index

Properties
Methods
Inputs

Constructor

constructor(changeDetectorRef: ChangeDetectorRef, rebirthNGConfig: RebirthNGConfig)
Parameters :
Name Type Optional
changeDetectorRef ChangeDetectorRef no
rebirthNGConfig RebirthNGConfig no

Inputs

cssClass

Type: string

disabled

Default value: false

items

Type: SelectButtonItem

justified

Type: boolean

multiple

Type: boolean

size

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

type

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

Methods

Private fillMultipleValue
fillMultipleValue(itemValue: any | string)
Parameters :
Name Type Optional
itemValue any | string no
Returns : void
Private fillSingleValue
fillSingleValue(itemValue: any | string)
Parameters :
Name Type Optional
itemValue any | string no
Returns : void
isSelected
isSelected(item: SelectButtonItem)
Parameters :
Name Type Optional
item SelectButtonItem no
Returns : boolean
registerOnChange
registerOnChange(fn: any)
Parameters :
Name Type Optional
fn any no
Returns : void
registerOnTouched
registerOnTouched(fn: any)
Parameters :
Name Type Optional
fn any no
Returns : void
selectedItem
selectedItem(item: SelectButtonItem)
Parameters :
Name Type Optional
item SelectButtonItem no
Returns : void
setDisabledState
setDisabledState(isDisabled: boolean)
Parameters :
Name Type Optional
isDisabled boolean no
Returns : void
writeValue
writeValue(obj: any)
Parameters :
Name Type Optional
obj any no
Returns : void

Properties

Private onChange
onChange:
Default value : (_: any) => null
Private onTouched
onTouched:
Default value : () => null
value
value: any
Type : any
import { Component, ChangeDetectionStrategy, forwardRef, Input, ChangeDetectorRef } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { SelectButtonItem } from './select-button-item.model';
import { RebirthNGConfig } from '../rebirth-ng.config';

@Component({
  selector: 're-select-button',
  templateUrl: './select-button.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
  host: {
    '[class]': '(cssClass || "") + " btn-group " + (size ? "btn-group-" + size : "") + (justified ? " btn-group-justified" : "") ',
    'role': 'group',
    'aria-label': 'Button group'
  },
  providers: [{
    provide: NG_VALUE_ACCESSOR,
    useExisting: forwardRef(() => SelectButtonComponent),
    multi: true
  }]
})
export class SelectButtonComponent implements ControlValueAccessor {

  @Input() size: 'lg' | 'sm' | 'xs';
  @Input() type: 'default' | 'primary' | 'success' | 'info' | 'warning' | 'danger';
  @Input() justified: boolean;
  @Input() multiple: boolean;
  @Input() items: SelectButtonItem;
  @Input() cssClass: string;
  @Input() disabled = false;
  value: any;
  private onChange = (_: any) => null;
  private onTouched = () => null;

  constructor(private changeDetectorRef: ChangeDetectorRef, rebirthNGConfig: RebirthNGConfig) {
    this.type = <any>rebirthNGConfig.selectButton.type;
    this.justified = rebirthNGConfig.selectButton.justified;
    this.multiple = rebirthNGConfig.selectButton.multiple;
  }

  writeValue(obj: any): void {
    this.value = obj;
    this.changeDetectorRef.markForCheck();
  }

  registerOnChange(fn: any): void {
    this.onChange = fn;
  }

  registerOnTouched(fn: any): void {
    this.onTouched = fn;
  }

  setDisabledState(isDisabled: boolean): void {
    this.disabled = isDisabled;
  }

  isSelected(item: SelectButtonItem) {
    const itemValue = item.value || item.text;
    if (this.value) {
      return this.multiple ? this.value.indexOf(itemValue) !== -1 : this.value === itemValue;
    }
  }

  selectedItem(item: SelectButtonItem) {
    this.onTouched();
    const itemValue = item.value || item.text;
    if (this.multiple) {
      return this.fillMultipleValue(itemValue);
    }

    this.fillSingleValue(itemValue);
  }

  private fillSingleValue(itemValue: any|string) {
    if (this.value !== itemValue) {
      this.value = itemValue;
      this.onChange(this.value);
    }
  }

  private fillMultipleValue(itemValue: any|string) {
    const value = this.value || [];
    if (value.indexOf(itemValue) === -1) {
      this.value = [...value, itemValue];
    } else {
      this.value = value.filter(function (item) {
        return item !== itemValue;
      });
    }
    this.onChange(this.value);
  }
}
<div class="btn-group" role="group" *ngFor="let item of items">
  <button type="button" class="btn btn-{{type}}" [ngClass]="{active: isSelected(item)}" [disabled]="disabled"
          (click)="selectedItem(item)">{{item.text}}
  </button>
</div>
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""