File

src/app/components/atm-input/atm-input.component.ts

Implements

ControlValueAccessor

Metadata

Index

Properties
Methods
Accessors

Methods

atmKeyUp
atmKeyUp(event: KeyboardEvent)
Parameters :
Name Type Optional
event KeyboardEvent No
Returns : void
formatNumber
formatNumber(number)
Parameters :
Name Optional
number No
Returns : any
ignoreKeys
ignoreKeys(e)
Parameters :
Name Optional
e No
Returns : boolean
onBlur
onBlur()
Returns : void
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
Optional 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 _value
Default value : DEFAULT_VALUE
atmElement
Type : ElementRef
Decorators :
@ViewChild('atm')
disabled
Default value : false
Private onChangeCallback
Type : function
Default value : null
Private onTouchedCallback
Type : function
Default value : null

Accessors

value
getvalue()
setvalue(value: any)
Parameters :
Name Type Optional
value any No
Returns : void
import {
  Component,
  ElementRef,
  ViewChild,
  Input,
  forwardRef
} from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';

export const ATM_INPUT_VALUE_ACCESSOR: any = {
  provide: NG_VALUE_ACCESSOR,
  useExisting: forwardRef(() => AtmInputComponent),
  multi: true
};

const DEFAULT_VALUE = null;

@Component({
  selector: 'app-atm-input',
  templateUrl: './atm-input.component.html',
  styleUrls: ['./atm-input.component.css'],
  providers: [ATM_INPUT_VALUE_ACCESSOR]
})
export class AtmInputComponent implements ControlValueAccessor {
  @ViewChild('atm') atmElement: ElementRef;
  private _value = DEFAULT_VALUE;
  disabled = false;
  // placeholders for the callbacks which are later provided by the Control Value Accessor
  private onTouchedCallback: () => void = null;
  private onChangeCallback: (_: any) => void = null;

  // get accessor
  get value(): any {
    return this._value;
  }

  // set accessor including call the onchange callback
  set value(value: any) {
    this.writeValue(value);
    this.onChangeCallback(value);
  }

  // from ControlValueAccessor interface
  setDisabledState?(isDisabled: boolean): void {
    this.disabled = isDisabled;
  }

  // set touched on blur
  onBlur() {
    this.onTouchedCallback();
  }

  // from ControlValueAccessor interface
  writeValue(obj: any): void {
    if (obj !== this._value) {
      let value = parseFloat(obj);
      value = isNaN(value) ? null : obj;
    }
  }

  // from ControlValueAccessor interface
  registerOnChange(fn: any): void {
    this.onChangeCallback = fn;
  }

  // from ControlValueAccessor interface
  registerOnTouched(fn: any): void {
    this.onTouchedCallback = fn;
  }

  atmKeyUp(event: KeyboardEvent) {
    const value = this.atmElement.nativeElement.value;
    const raw = value.replace('.', '');
    let formatted = this.formatNumber(raw);
    this.atmElement.nativeElement.value = formatted;
    this.writeValue(formatted);
    this.onChangeCallback(formatted);
  }

  formatNumber(number) {
    if (!number) {
      return DEFAULT_VALUE;
    }
    const parsed = parseFloat(number);
    if (isNaN(parsed)) {
      return DEFAULT_VALUE;
    }
    const formatted = (parsed / 100).toFixed(2);
    return formatted;
  }

  ignoreKeys(e) {
    if (
      e.keyCode === 8 ||
      e.keyCode === 46 ||
      e.keyCode === 9 ||
      (e.keyCode >= 96 && e.keyCode <= 105) ||
      (e.keyCode >= 48 && e.keyCode <= 57)
    ) {
      return true;
    } else {
      return false;
    }
  }
}
<div class="parent-relative">
  <span class="input-badge">
    <i class="material-icons">attach_money</i>
  </span>

  <input [(ngModel)]="value" [disabled]="disabled" type="tel" pattern=".{1,}" maxlength="8" placeholder="0.00" class="atm-input"
    #atm (keyup)="atmKeyUp($event)" (keydown)="ignoreKeys($event)" required />

  <span class="approve">
    <i class="material-icons status-positive">check</i>
  </span>
</div>

./atm-input.component.css

.atm-input {
  padding: 0 45px 0 30px;
}
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""