Fix initial selection of multi-select fields when no value is present

pull/6263/head
Oliver Günther 7 years ago
parent 42c5d53504
commit 70c1c95726
No known key found for this signature in database
GPG Key ID: 88872239EB414F99
  1. 20
      frontend/app/components/wp-edit/field-types/wp-edit-multi-select-field.module.ts

@ -91,21 +91,19 @@ export class MultiSelectEditField extends EditField {
*/
public buildSelectedOption() {
const value:HalResource|HalResource[] = this.changeset.value(this.name);
const finder = (val:{ href:string|null }) => _.find(this.valueOptions,
(valueOption) => valueOption.href === val.href)!;
if (this.isMultiselect) {
if (!Array.isArray(value)) {
return [finder(value)];
return [this.findValueOption(value)];
}
return value.map(val => finder(val));
return value.map(val => this.findValueOption(val));
}
if (!Array.isArray(value)) {
return finder(value);
return this.findValueOption(value);
} else if (value.length > 0) {
return finder(value[0]);
return this.findValueOption(value[0]);
}
return this.nullOption;
@ -148,6 +146,16 @@ export class MultiSelectEditField extends EditField {
this._selectedOption = this.buildSelectedOption();
}
private findValueOption(option?:HalResource):ValueOption {
let result;
if (option) {
result = _.find(this.valueOptions, (valueOption) => valueOption.href === option.href)!;
}
return result || this.nullOption;
}
private setValues(availableValues:any[], sortValuesByName:boolean = false) {
if (sortValuesByName) {
availableValues.sort(function (a:any, b:any) {

Loading…
Cancel
Save