Skip to content

Commit

Permalink
feat: add support for nested values in mgt-picker key-name (#2917)
Browse files Browse the repository at this point in the history
* add support for nested values in mgt-picker key-name

* add console warning to getNestedPropertyValue
  • Loading branch information
andreasomayrat committed Jan 23, 2024
1 parent 14a96ff commit e79fbdc
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions packages/mgt-components/src/components/mgt-picker/mgt-picker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,29 @@ export class MgtPicker extends MgtTemplatedTaskComponent {
placeholder=${this.placeholder}>
${this.response.map(
item => html`
<fluent-option value=${item.id} @click=${(e: MouseEvent) => this.handleClick(e, item)}> ${
item[this.keyName]
} </fluent-option>`
<fluent-option value=${item.id} @click=${(e: MouseEvent) =>
this.handleClick(e, item)}> ${this.getNestedPropertyValue(item, this.keyName)} </fluent-option>`
)}
</fluent-combobox>
`;
}

private getNestedPropertyValue(item: Entity, keyName: string) {
const keys = keyName.split('.');
let value: Entity | object | string = item;

for (const key of keys) {
value = value[key] as object | string;

if (value === undefined) {
console.warn(`mgt-picker: Key '${key}' is undefined.`);
return '';
}
}

return value;
}

/**
* Renders mgt-get which does a GET request to the resource.
*
Expand Down

0 comments on commit e79fbdc

Please sign in to comment.