File

projects/common/lib/components/postal-code/postal-code.component.ts

Extends

Base

Implements

OnInit ControlValueAccessor

Metadata

selector common-postal-code
styleUrls ./postal-code.component.scss
templateUrl ./postal-code.component.html

Index

Properties
Methods
Inputs
Outputs
Accessors

Constructor

constructor(controlDir: NgControl)
Parameters :
Name Type Optional
controlDir NgControl No

Inputs

disabled
Type : boolean
Default value : false
displayMask
Type : boolean
Default value : true
errorMessage
Type : ErrorMessage
label
Type : string
Default value : 'Postal Code'
labelforId
Type : string
Default value : 'postalCode_' + this.objectId
maxlen
Type : string
Default value : '250'
required
Type : boolean
Default value : false
value
Type : string

Outputs

blurEvent
Type : EventEmitter<any>
valueChange
Type : EventEmitter<string>

Methods

ngOnInit
ngOnInit()
Returns : void
onBlurEvent
onBlurEvent(event: any)
Parameters :
Name Type Optional
event any No
Returns : void
onValueChange
onValueChange(value: any)
Parameters :
Name Type Optional
value any No
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
setDisabledState
setDisabledState(isDisabled: boolean)
Parameters :
Name Type Optional
isDisabled boolean No
Returns : void
Private setErrorMsg
setErrorMsg()
Returns : void
upperCasePipe
upperCasePipe(text: string)

Upper cases letters in string

Parameters :
Name Type Optional
text string No
Returns : any
writeValue
writeValue(value: any)
Parameters :
Name Type Optional
value any No
Returns : void

Properties

_onChange
Default value : () => {...}
_onTouched
Default value : () => {...}
Public controlDir
Type : NgControl
Decorators :
@Optional()
@Self()
defaultErrMsg
Type : ErrorMessage
Default value : { required: 'is required.', invalidChar: 'must contain letters and/or numbers and may include blank characters.', pattern: 'Must be in the format A1A 1A1', invalidBCPostal: 'Invalid postal code for British Columbia.' }
mask
Type : any
postalCode
Type : string
Default value : ''
Public objectId
Type : string
Default value : UUID.UUID()
Inherited from Base
Defined in Base:11

An identifier for parents to keep track of components

Accessors

value
getvalue()
setvalue(val: string)
Parameters :
Name Type Optional
val string No
Returns : void

Design Guidelines

Lorem ipsum dolor sit amet consectetur adipisicing elit. Sapiente, magnam ipsam. Sit quasi natus architecto rerum unde non provident! Quia nisi facere amet iste mollitia voluptatem non molestias esse optio?

Aperiam fugiat consectetur temporibus, iste repellat, quisquam sapiente nisi distinctio optio, autem nemo tenetur error eum voluptatibus ab accusamus quis voluptatum blanditiis. Quam et ut reprehenderit vitae nobis, at ipsum!

Exercitationem pariatur animi repudiandae corporis obcaecati ratione ducimus beatae quam, nostrum magnam unde numquam quidem cupiditate odit id. Beatae alias molestiae, optio incidunt harum quia voluptates deserunt sequi. Nesciunt, optio.

import { Component, Input, Output, EventEmitter, Optional, Self, OnInit } from '@angular/core';
import { ControlValueAccessor, NgControl } from '@angular/forms';
import { LETTER, NUMBER, SPACE } from '../../models/mask.constants';
import { Base } from '../../models/base';
import { ErrorMessage } from '../../../public_api';


@Component({
  selector: 'common-postal-code',
  templateUrl: './postal-code.component.html',
  styleUrls: ['./postal-code.component.scss']
})
export class PostalCodeComponent extends Base implements OnInit, ControlValueAccessor  {

  @Input() label: string = 'Postal Code';
  @Input() displayMask: boolean = true;
  @Input() maxlen: string = '250';
  @Input() labelforId: string = 'postalCode_' + this.objectId;
  @Input() disabled: boolean = false;
  @Input() errorMessage: ErrorMessage;
  @Input() required: boolean = false;

  @Input()
  set value( val: string ) {
    if (val) {
      this.postalCode = val;
    }
  }
  get value() {
    return this.postalCode;
  }

  @Output() valueChange: EventEmitter<string> = new EventEmitter<string>();
  @Output() blurEvent: EventEmitter<any> = new EventEmitter<any>();

  postalCode: string = '';
  mask: any;

  defaultErrMsg: ErrorMessage = {
    required: 'is required.',
    invalidChar: 'must contain letters and/or numbers and may include blank characters.',
    pattern: 'Must be in the format A1A 1A1',
    invalidBCPostal: 'Invalid postal code for British Columbia.'
  };

  _onChange = (_: any) => {};
  _onTouched = (_: any) => {};

  constructor( @Optional() @Self() public controlDir: NgControl ) {
    super();
    if ( controlDir ) {
      controlDir.valueAccessor = this;
    }

    this.mask = [LETTER, NUMBER, LETTER, SPACE, NUMBER, LETTER, NUMBER];
  }

  ngOnInit() {
    this.setErrorMsg();
  }

  onValueChange( value: any ) {

    if ( value !== this.postalCode ) { // IE fix when focus does not display required error
      this._onChange( value );
      this.valueChange.emit( value );
      this.postalCode = value;
    }
  }

  onBlurEvent( event: any ) {
    this._onTouched( event );
    this.blurEvent.emit( event );
  }

  writeValue( value: any ): void {
    if ( value !== undefined ) {
      this.postalCode = value;
    }
  }

  // Register change functiong
  registerOnChange( fn: any ): void {
    this._onChange = fn;
  }

  // Register touched function
  registerOnTouched( fn: any ): void {
    this._onTouched = fn;
  }

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

  /**
   * Upper cases letters in string
   */
  upperCasePipe(text: string) {
    return text.toUpperCase();
  }

  private setErrorMsg() {
    if ( this.errorMessage ) {
      Object.keys(this.errorMessage).map( x => this.defaultErrMsg[x] = this.errorMessage[x] );
    }
  }
}
<label for="{{labelforId}}" class="control-label">{{label}}</label>
<div *ngIf="displayMask; else NoMask">
<input class="form-control"
       spellcheck="false"
       id="{{labelforId}}"
       [ngModel]="postalCode"
       (ngModelChange)="onValueChange($event)"
       (blur)="onBlurEvent($event)"
       [textMask]="{mask: mask, pipe: upperCasePipe}"
       [required]="required"
       [disabled]="disabled"
       autocomplete="off"/>
  </div>

  <!-- Error messages for input -->
  <common-error-container
    [displayError]="controlDir && !disabled && (controlDir.touched || controlDir.dirty) && controlDir.errors">
    <div *ngIf="controlDir?.errors?.required">
      {{label}} {{defaultErrMsg.required}}
    </div>
    <div *ngIf="controlDir?.errors?.pattern">
      {{defaultErrMsg.pattern}}
    </div>
    <div *ngIf="controlDir?.errors?.invalidBCPostal">
      {{defaultErrMsg.invalidBCPostal}}
    </div>
    <div *ngIf="controlDir?.errors?.invalidChar">
    {{label}} {{defaultErrMsg.invalidChar}}
  </div>
  </common-error-container>

<ng-template #NoMask>
  <input class="form-control"
         spellcheck="false"
         type="text"
         id="{{labelforId}}"
         [value]="postalCode"
         (input)="onValueChange($event.target.value)"
         (blur)="onBlurEvent($event)"
         [disabled]="disabled"
         [attr.maxlength]="maxlen"
         autocomplete="off"/>
</ng-template>
Legend
Html element
Component
Html element with directive

result-matching ""

    No results matching ""