Skip to content

Commit

Permalink
Added the possibility to define options globally.
Browse files Browse the repository at this point in the history
  • Loading branch information
César Mendonça committed Jul 28, 2017
1 parent a96c1bf commit 85f4335
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 6 deletions.
33 changes: 32 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { CurrencyMaskModule } from "ng2-currency-mask";

@NgModule({
imports: [
//... you others modules
...
CurrencyMaskModule
],
declarations: [...],
Expand Down Expand Up @@ -57,4 +57,35 @@ Available options:
* `suffix` - Money suffix (default: `''`)
* `thousands` - Separator of thousands (default: `','`)

You can also set options globally...

```ts
import { CurrencyMaskModule } from "ng2-currency-mask";
import { CurrencyMaskConfig, CURRENCY_MASK_CONFIG } from "ng2-currency-mask/src/currency-mask.config";

export const CustomCurrencyMaskConfig: CurrencyMaskConfig = {
align: "right",
allowNegative: true,
allowZero: true,
decimal: ",",
precision: 2,
prefix: "R$ ",
suffix: "",
thousands: "."
};

@NgModule({
imports: [
...
CurrencyMaskModule
],
declarations: [...],
providers: [
{ provide: CURRENCY_MASK_CONFIG, useValue: CustomCurrencyMaskConfig }
],
bootstrap: [AppComponent]
})
export class AppModule {}
```

## Questions? Open a Issue!
4 changes: 2 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './src/currency-mask.directive';
export * from './src/currency-mask.module';
export * from "./src/currency-mask.directive";
export * from "./src/currency-mask.module";
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@
"typescript": "2.2.1",
"zone.js": "0.8.5"
}
}
}
15 changes: 15 additions & 0 deletions src/currency-mask.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { InjectionToken } from "@angular/core";

export interface CurrencyMaskConfig {

align: string;
allowNegative: boolean;
allowZero: boolean;
decimal: string;
precision: number;
prefix: string;
suffix: string;
thousands: string;
}

export let CURRENCY_MASK_CONFIG = new InjectionToken<CurrencyMaskConfig>("currency.mask.config");
9 changes: 7 additions & 2 deletions src/currency-mask.directive.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AfterViewInit, Directive, DoCheck, ElementRef, forwardRef, HostListener, KeyValueDiffer, KeyValueDiffers, Input, OnInit } from "@angular/core";
import { AfterViewInit, Directive, DoCheck, ElementRef, forwardRef, HostListener, Inject, KeyValueDiffer, KeyValueDiffers, Input, OnInit, Optional } from "@angular/core";
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from "@angular/forms";

import { CurrencyMaskConfig, CURRENCY_MASK_CONFIG } from "./currency-mask.config";
import { InputHandler } from "./input.handler";

export const CURRENCYMASKDIRECTIVE_VALUE_ACCESSOR: any = {
Expand Down Expand Up @@ -31,7 +32,11 @@ export class CurrencyMaskDirective implements AfterViewInit, ControlValueAccesso
thousands: ","
};

constructor(private elementRef: ElementRef, private keyValueDiffers: KeyValueDiffers) {
constructor( @Optional() @Inject(CURRENCY_MASK_CONFIG) private currencyMaskConfig: CurrencyMaskConfig, private elementRef: ElementRef, private keyValueDiffers: KeyValueDiffers) {
if (currencyMaskConfig) {
this.optionsTemplate = currencyMaskConfig;
}

this.keyValueDiffer = keyValueDiffers.find({}).create(null);
}

Expand Down

0 comments on commit 85f4335

Please sign in to comment.