[AAE-936] injecting external settings for amount widget (#5389)

* injecting external settings for amount widget

* undo form changes
This commit is contained in:
Denys Vuika 2020-01-23 11:59:40 +00:00 committed by GitHub
parent deb2a0082f
commit c7c1d76510
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 6 deletions

View File

@ -17,7 +17,7 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { FormFieldModel } from './../core/form-field.model';
import { AmountWidgetComponent } from './amount.widget';
import { AmountWidgetComponent, ADF_AMOUNT_SETTINGS } from './amount.widget';
import { setupTestBed } from '../../../../testing/setupTestBed';
import { CoreModule } from '../../../../core.module';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
@ -77,3 +77,38 @@ describe('AmountWidgetComponent', () => {
});
});
describe('AmountWidgetComponent settings', () => {
let widget: AmountWidgetComponent;
let fixture: ComponentFixture<AmountWidgetComponent>;
setupTestBed({
imports: [
NoopAnimationsModule,
CoreModule.forRoot()
],
providers: [
{
provide: ADF_AMOUNT_SETTINGS,
useValue: {
showReadonlyPlaceholder: true
}
}
]
});
beforeEach(() => {
fixture = TestBed.createComponent(AmountWidgetComponent);
widget = fixture.componentInstance;
});
it('should display placeholder via injected settings', () => {
const field: any = {
readOnly: true,
placeholder: 'some placeholder'
};
widget.field = field;
expect(widget.placeholder).toBe('some placeholder');
});
});

View File

@ -17,10 +17,16 @@
/* tslint:disable:component-selector */
import { Component, OnInit, ViewEncapsulation } from '@angular/core';
import { Component, OnInit, ViewEncapsulation, InjectionToken, Inject, Optional } from '@angular/core';
import { FormService } from './../../../services/form.service';
import { baseHost , WidgetComponent } from './../widget.component';
export interface AmountWidgetSettings {
showReadonlyPlaceholder: boolean;
}
export const ADF_AMOUNT_SETTINGS = new InjectionToken<AmountWidgetSettings>('adf-amount-settings');
@Component({
selector: 'amount-widget',
templateUrl: './amount.widget.html',
@ -31,20 +37,32 @@ import { baseHost , WidgetComponent } from './../widget.component';
export class AmountWidgetComponent extends WidgetComponent implements OnInit {
static DEFAULT_CURRENCY: string = '$';
private showPlaceholder = true;
currency: string = AmountWidgetComponent.DEFAULT_CURRENCY;
get placeholder(): string {
return !this.field.readOnly ? this.field.placeholder : '';
return this.showPlaceholder ? this.field.placeholder : '';
}
constructor(public formService: FormService) {
constructor(
public formService: FormService,
@Inject(ADF_AMOUNT_SETTINGS)
@Optional()
private settings: AmountWidgetSettings
) {
super(formService);
}
ngOnInit() {
if (this.field && this.field.currency) {
this.currency = this.field.currency;
if (this.field) {
if (this.field.currency) {
this.currency = this.field.currency;
}
if (this.field.readOnly) {
this.showPlaceholder = this.settings && this.settings.showReadonlyPlaceholder;
}
}
}