Start fix for default option on dropdown

This commit is contained in:
Vito Albano 2016-11-10 14:15:28 +00:00 committed by Mario Romano
parent 4970920643
commit 55d73b8006
3 changed files with 61 additions and 2 deletions

View File

@ -1,11 +1,11 @@
<div class="dropdown-widget"
[class.dropdown-widget__invalid]="!field.isValid">
[class.dropdown-widget__invalid]="!field.isValid" *ngIf="field?.isVisible">
<label class="dropdown-widget__label" [attr.for]="field.id">{{field.name}}</label>
<select class="dropdown-widget__select"
[attr.id]="field.id"
[(ngModel)]="field.value"
(ngModelChange)="checkVisibility(field)">
<option *ngFor="let opt of field.options" [value]="opt.id">{{opt.name}}</option>
<option *ngFor="let opt of field.options" [value]="getOptionValue(opt)" [id]="opt.id">{{opt.name}}</option>
</select>
<span *ngIf="field.validationSummary" class="mdl-textfield__error">{{field.validationSummary}}</span>
</div>

View File

@ -98,4 +98,53 @@ describe('DropdownWidget', () => {
expect(widget.field.options[0]).toBe(emptyOption);
expect(widget.field.options[1]).toBe(restFieldValue);
});
describe('when template is ready', () => {
let dropDownWidget: DropdownWidget;
let fixture: ComponentFixture<DropdownWidget>;
let element: HTMLElement;
let componentHandler;
beforeEach(async(() => {
componentHandler = jasmine.createSpyObj('componentHandler', ['upgradeAllRegistered', 'upgradeElement']);
window['componentHandler'] = componentHandler;
TestBed.configureTestingModule({
imports: [CoreModule],
declarations: [DropdownWidget]
}).compileComponents().then(() => {
fixture = TestBed.createComponent(DropdownWidget);
dateWidget = fixture.componentInstance;
element = fixture.nativeElement;
});
}));
beforeEach(() => {
spyOn(dateWidget, 'setupMaterialTextField').and.stub();
dateWidget.field = new FormFieldModel(new FormModel(), {
id: 'date-field-id',
name: 'date-name',
value: '9-9-9999',
type: 'date',
readOnly: 'false'
});
dateWidget.field.isVisible = true;
fixture.detectChanges();
});
afterEach(() => {
fixture.destroy();
TestBed.resetTestingModule();
});
it('should show visible date widget', async(() => {
fixture.whenStable()
.then(() => {
expect(element.querySelector('#date-field-id')).toBeDefined();
expect(element.querySelector('#date-field-id')).not.toBeNull();
let dateElement: any = element.querySelector('#date-field-id');
expect(dateElement.value).toEqual('9-9-9999');
});
}));
});
});

View File

@ -80,6 +80,16 @@ export class DropdownWidget extends WidgetComponent implements OnInit {
);
}
getOptionValue(option: FormFieldOption): string {
let optionValue: string = '';
if (option.id === 'empty') {
optionValue = option.id;
} else {
optionValue = option.name;
}
return optionValue;
}
handleError(error: any) {
console.error(error);
}