Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow currencyMask to be used as input instead of options #194

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,7 @@ You can set options...

```html
<!-- example for pt-BR money -->
<input
currencyMask
formControlName="value"
[options]="{ prefix: 'R$ ', thousands: '.', decimal: ',' }"
/>
<input [currencyMask]="{ prefix: 'R$ ', thousands: '.', decimal: ',' }" formControlName="value" />
```

Available options:
Expand Down
32 changes: 23 additions & 9 deletions projects/ngx-currency/src/lib/ngx-currency.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {

@Directive({
standalone: true,
selector: '[currencyMask]',
selector: 'input[currencyMask]',
providers: [
{
provide: NG_VALUE_ACCESSOR,
Expand All @@ -35,22 +35,36 @@ import {
export class NgxCurrencyDirective
implements AfterViewInit, ControlValueAccessor, DoCheck, OnInit
{
@Input() options: Partial<NgxCurrencyConfig> = {};
@Input()
set currencyMask(value: Partial<NgxCurrencyConfig> | string) {
if (typeof value === 'string') return;

this._options = value;
}

/**
* @deprecated Use currencyMask input instead
*/
@Input()
set options(value: Partial<NgxCurrencyConfig>) {
this._options = value;
}

private _inputHandler!: InputHandler;
private readonly _keyValueDiffer: KeyValueDiffer<
keyof NgxCurrencyConfig,
unknown
>;

private _optionsTemplate: NgxCurrencyConfig;
private _options: Partial<NgxCurrencyConfig> = {};
private readonly _optionsTemplate: NgxCurrencyConfig;

constructor(
@Optional()
@Inject(NGX_CURRENCY_CONFIG)
globalOptions: Partial<NgxCurrencyConfig>,
keyValueDiffers: KeyValueDiffers,
private readonly _elementRef: ElementRef
private readonly _elementRef: ElementRef<HTMLInputElement>,
) {
this._optionsTemplate = {
align: 'right',
Expand All @@ -72,23 +86,23 @@ export class NgxCurrencyDirective
ngOnInit() {
this._inputHandler = new InputHandler(this._elementRef.nativeElement, {
...this._optionsTemplate,
...this.options,
...this._options,
});
}

ngAfterViewInit() {
this._elementRef.nativeElement.style.textAlign =
this.options?.align ?? this._optionsTemplate.align;
this._options?.align ?? this._optionsTemplate.align;
}

ngDoCheck() {
if (this._keyValueDiffer.diff(this.options)) {
if (this._keyValueDiffer.diff(this._options)) {
this._elementRef.nativeElement.style.textAlign =
this.options?.align ?? this._optionsTemplate.align;
this._options?.align ?? this._optionsTemplate.align;

this._inputHandler.updateOptions({
...this._optionsTemplate,
...this.options,
...this._options,
});
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@
autofocus
placeholder="R$ 0,00"
formControlName="value"
currencyMask
[options]="ngxCurrencyOptions"
[currencyMask]="ngxCurrencyOptions"
/>
</div>
</div>
Expand Down