Skip to content

Commit 51d587b

Browse files
Show the delete button in a form array even if it has a selected value
1 parent 3e93746 commit 51d587b

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

src/app/shared/form/form.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
(ngbEvent)="onCustomEvent($event)">
1414
<ng-template modelType="ARRAY" let-group let-index="index" let-context="context">
1515
<!--Array with repeatable items-->
16-
<div *ngIf="(!context.notRepeatable) && !isVirtual(context, index) && group.context.groups.length !== 1 && !isItemReadOnly(context, index)"
16+
<div *ngIf="(!context.notRepeatable) && !isVirtual(context, index) && isValidForShowDeleteButton(context, index) && !isItemReadOnly(context, index)"
1717
class="col-xs-2 d-flex flex-column justify-content-sm-start align-items-end">
1818
<button type="button" class="btn btn-secondary" role="button"
1919
title="{{'form.remove' | translate}}"

src/app/shared/form/form.component.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ function init() {
117117
id: 'bootstrapArrayGroupInput',
118118
placeholder: 'example array group input',
119119
readOnly: false,
120+
value: 'test',
120121
}),
121122
];
122123
},
@@ -434,6 +435,16 @@ describe('FormComponent test suite', () => {
434435
expect(readOnly).toBe(false);
435436
}));
436437

438+
it('should return valid property from array item for show delete button', inject([FormBuilderService], (service: FormBuilderService) => {
439+
spyOn(formComp.addArrayItem, 'emit');
440+
441+
formComp.insertItem(new Event('click'), formComp.formModel[0] as DynamicFormArrayModel, 0);
442+
443+
const isValid = formComp.isValidForShowDeleteButton(formComp.formModel[0] as DynamicFormArrayModel, 0);
444+
445+
expect(isValid).toBe(true);
446+
}));
447+
437448
it('should dispatch FormChangeAction when an item has been added to an array', inject([FormBuilderService], (service: FormBuilderService) => {
438449
formComp.insertItem(new Event('click'), formComp.formModel[0] as DynamicFormArrayModel, 0);
439450

src/app/shared/form/form.component.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,9 @@ export class FormComponent implements OnDestroy, OnInit {
372372
// dispatch remove event for any field type except for qualdrop value
373373
this.removeArrayItem.emit(event);
374374
}
375+
if (index === 0) {
376+
this.insertItem($event,arrayContext,0);
377+
}
375378
}
376379

377380
insertItem($event, arrayContext: DynamicFormArrayModel, index: number): void {
@@ -387,6 +390,19 @@ export class FormComponent implements OnDestroy, OnInit {
387390
return isNotEmpty(value) && value.isVirtual;
388391
}
389392

393+
isValidForShowDeleteButton(context, index): boolean {
394+
if(index > 0) return true;
395+
return this.hasValue(context, index);
396+
}
397+
398+
/**
399+
* @Description check if the model has a value
400+
* **/
401+
hasValue(arrayContext, index: number) {
402+
const context = arrayContext.groups[index];
403+
return isNotEmpty((context.group[0] as any).value);
404+
}
405+
390406
protected getEvent($event: any, arrayContext: DynamicFormArrayModel, index: number, type: string): DynamicFormControlEvent {
391407
const context = arrayContext.groups[index];
392408
const itemGroupModel = context.context;

0 commit comments

Comments
 (0)