Skip to content

Commit

Permalink
now supporting input directive
Browse files Browse the repository at this point in the history
  • Loading branch information
ainouss committed Feb 4, 2019
1 parent 28e6265 commit f1acd70
Show file tree
Hide file tree
Showing 12 changed files with 178 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@angularfy/ng-autonumeric",
"version": "1.0.0",
"version": "1.0.1",
"scripts": {
"ng": "ng",
"start": "ng serve",
Expand Down
4 changes: 2 additions & 2 deletions src/autonumeric/autonumeric.component.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<input #input [type]="!disabled && !readonly ? 'text' :'hidden'" [disabled]="disabled" [required]="required" />
<input #input [type]="!disabled && !readonly ? 'text' :'hidden'" [placeholder]="placeholder" [title]="title" [required]="required" />
<ng-container *ngIf="disabled || readonly">
<span> {{internal}}</span>
</ng-container>
</ng-container>
134 changes: 134 additions & 0 deletions src/autonumeric/autonumeric.directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/**
* @author Abdelghani AINOUSS
* [email protected]
*/

import {
ChangeDetectorRef,
ElementRef,
EventEmitter,
forwardRef,
HostListener,
Input,
OnChanges,
OnDestroy,
OnInit,
Output,
Renderer2,
SimpleChanges,
ViewChild,
ViewEncapsulation,
Directive,
AfterViewInit,
} from '@angular/core';
import { NgAutonumericOptionsSelect } from './autonumeric-options-select';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import AutoNumeric from 'autonumeric';
import { BasicInput } from './basic-input';

@Directive({
selector: 'input[ngAutonumeric]',
exportAs: 'ngAutonumeric',
providers: [{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => NgAutonumericDirective),
multi: true
}],
})
export class NgAutonumericDirective extends BasicInput implements OnInit, OnChanges, ControlValueAccessor, AfterViewInit, OnDestroy {

@Input()
ngModel: number | string;
@Input()
ngAutonumeric: NgAutonumericOptionsSelect;
instance: any;
unlistenFormatted: () => void;
@Output()
format = new EventEmitter();
_onChange = (_) => {
};
_onTouched = () => {
};

constructor(private cd: ChangeDetectorRef, private renderer: Renderer2, private input: ElementRef) {
super();
}

ngOnInit() {
}

ngAfterViewInit(): void {
if (AutoNumeric === undefined) {
throw "AutoNumeric is a peer dependency, please make sure you install it before using this library. Hint : npm install --save autonumeric@latest";
}
this.instance = new AutoNumeric(this.input.nativeElement, this.ngAutonumeric);
setTimeout(()=>{
this.instance.set(this.ngModel);
},0);
this.unlistenFormatted = this.renderer.listen(this.input.nativeElement, 'autoNumeric:formatted', ($event) => {
this.onFormatted($event);
});
}

ngOnChanges(changes: SimpleChanges): void {
if (!this.instance) {
return;
}
if (changes.ngModel) {
this.instance.set(this.ngModel);
}
if (changes.ngAutonumeric) {
this.instance.update(this.ngAutonumeric);
}
}

set(value) {
if (this.instance) {
this.instance.set(value);
} else throw 'NgAutonumeric instance not available. try using two binding by providing [(ngModel)]';
}

@HostListener('change', ['$event.target.value'])
handleChange(value) {
this.writeValue(value);
if (this.instance) {
value = this.instance.getNumber();
}
this._onChange(value);
this._onTouched();
}

onFormatted($event) {
let value = $event;
if (this.instance) {
value = this.instance.getFormatted()
}
this.format.emit(value);
}

@HostListener('blur')
handleTouched() {
this._onTouched();
}

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

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

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

writeValue(obj: any): void {
this.ngModel = obj;
}

ngOnDestroy(): void {
if (this.unlistenFormatted)
this.unlistenFormatted();
}
}
14 changes: 8 additions & 6 deletions src/autonumeric/autonumeric.module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import {NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';
import {NgAutonumericComponent} from './autonumeric.component';
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { NgAutonumericComponent } from './autonumeric.component';
import { NgAutonumericDirective } from './autonumeric.directive';

@NgModule({
declarations: [NgAutonumericComponent],
imports: [CommonModule],
exports: [NgAutonumericComponent]
declarations: [NgAutonumericComponent, NgAutonumericDirective],
imports: [CommonModule, FormsModule],
exports: [NgAutonumericComponent, NgAutonumericDirective]
})
export class NgAutonumericModule {
}
Expand Down
15 changes: 15 additions & 0 deletions src/autonumeric/autonumeric.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @author Abdelghani AINOUSS
*
*/
import { Injectable } from '@angular/core';



@Injectable()
export class NgAutonumericService {

constructor(){

}
}
2 changes: 2 additions & 0 deletions src/autonumeric/basic-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ export class BasicInput implements Partial<HTMLInputElement> {
readonly: boolean;
@Input()
title: string;
@Input()
placeholder: string;
}
6 changes: 6 additions & 0 deletions src/autonumeric/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
export * from './autonumeric-default-settings'
export * from './autonumeric-options-select'
export * from './autonumeric.component'
export * from './autonumeric.directive'
export * from './autonumeric.module'
export * from './autonumeric.service'
export * from './basic-input'
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './autonumeric/autonumeric.module'
export * from './autonumeric/index'
4 changes: 2 additions & 2 deletions src/ng-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"deleteDestPath": false,
"lib": {
"entryFile": "./index.ts",
"umdId": "ngautonumeric",
"amdId": "ngautonumeric"
"umdId": "nga",
"amdId": "nga"
}
}
8 changes: 4 additions & 4 deletions src/ng-package.prod.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"$schema": "../node_modules/ng-packagr/ng-package.schema.json",
"dest": "../dist/ng-bootstrap",
"dest": "../dist/ng-autonumeric",
"lib": {
"flatModuleFile": "ng-bootstrap",
"flatModuleFile": "ng-autonumeric",
"entryFile": "./index.ts",
"umdId": "ngb",
"amdId": "ngb"
"umdId": "nga",
"amdId": "nga"
}
}
3 changes: 2 additions & 1 deletion src/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@angularfy/ng-autonumeric",
"version": "4.5.4",
"version": "1.0.1",
"description": "Angular implementation of autoNumeric",
"keywords": [
"angular",
Expand Down Expand Up @@ -29,6 +29,7 @@
"peerDependencies": {
"@angular/common": "^4.0.1",
"@angular/core": "^4.0.1",
"@angular/forms": "^4.0.1",
"autonumeric": "^4.0.1"
}
}
2 changes: 1 addition & 1 deletion src/public_api.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './autonumeric/autonumeric.module'
export * from './index'

0 comments on commit f1acd70

Please sign in to comment.