Skip to content

Commit

Permalink
Datalist : only patch defaultValue if is present in values
Browse files Browse the repository at this point in the history
  • Loading branch information
TheoLechemia committed Jul 26, 2024
1 parent b58f670 commit 2b587f2
Showing 1 changed file with 26 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
DoCheck,
IterableDiffers,
IterableDiffer,
SimpleChanges,
} from '@angular/core';
import { DataFormService } from '../data-form.service';
import { GenericFormComponent } from '@geonature_common/form/genericForm.component';
Expand All @@ -16,7 +17,7 @@ import { CommonService } from '../../service/common.service';
selector: 'pnx-datalist',
templateUrl: './datalist.component.html',
})
export class DatalistComponent extends GenericFormComponent implements OnInit {
export class DatalistComponent extends GenericFormComponent implements OnInit, OnChanges {
formId: string; // Unique form id

@Input() designStyle: 'bootstrap' | 'material' = 'material';
Expand Down Expand Up @@ -58,6 +59,10 @@ export class DatalistComponent extends GenericFormComponent implements OnInit {
this.getData();
}

ngOnChanges(changes: SimpleChanges): void {
console.log("?????", changes)
}

onToppingRemoved(val) {
const value = this.parentFormControl.value;
this.parentFormControl.patchValue(value.filter((v) => v !== val));
Expand Down Expand Up @@ -144,16 +149,28 @@ export class DatalistComponent extends GenericFormComponent implements OnInit {
this.parentFormControl.value.length == 0)) &&
this.default
) {

const value = this.multiple ? this.default : [this.default];
// check if the default value is in the provided values
const valuesID = this.values.map(el => el[this.keyValue]);
const defaultValuesID = value.map(el => el[this.keyValue]);
const defaultValueIsInValues = valuesID.some(el => defaultValuesID.includes(el));

// patch value only if default value is in values
if(defaultValueIsInValues) {
const res = value.map((val) =>
typeof val === 'object'
? (this.filteredValues.find((v) =>
Object.keys(val).every((key) => v[key] === val[key])
) || {})[this.keyValue]
: val
);
this.parentFormControl.patchValue(this.multiple ? res : res[0]);

}



const res = value.map((val) =>
typeof val === 'object'
? (this.filteredValues.find((v) =>
Object.keys(val).every((key) => v[key] === val[key])
) || {})[this.keyValue]
: val
);
this.parentFormControl.patchValue(this.multiple ? res : res[0]);
}
this.parentFormControl.markAsTouched();
}
Expand Down

0 comments on commit 2b587f2

Please sign in to comment.