[IMP] web: SelectMenu: support object as value

Before this commit,  the `SelectMenu` component didn't support object
in the value props of the component.

This commit allow us to use object in the `SelectMenu` value props.

Part-of: odoo/odoo#115799
This commit is contained in:
fdardenne
2023-04-03 13:55:17 +02:00
committed by Lucas Perais
parent 98a89ed51f
commit d760b81271
2 changed files with 60 additions and 1 deletions
@@ -4,6 +4,7 @@ import { Component, useState, useRef } from "@odoo/owl";
import { Dropdown } from "@web/core/dropdown/dropdown";
import { DropdownItem } from "@web/core/dropdown/dropdown_item";
import { _lt } from "@web/core/l10n/translation";
import { shallowEqual } from "@web/core/utils/objects";
import { useDebounced } from "@web/core/utils/timing";
import { scrollTo } from "@web/core/utils/scrolling";
import { fuzzyLookup } from "@web/core/utils/search";
@@ -89,9 +90,15 @@ export class SelectMenu extends Component {
get displayValue() {
if (this.props.value !== undefined) {
const choices = [...this.props.choices, ...this.props.groups.flatMap((g) => g.choices)];
const value = choices.find((c) => c.value === this.props.value);
const value = choices.find((c) => {
if (typeof c.value === "object" && typeof this.props.value === "object") {
return shallowEqual(c.value, this.props.value);
}
return c.value === this.props.value;
});
return value ? value.label : this.props.value;
}
return "";
}
onOpened() {
@@ -178,6 +178,58 @@ QUnit.module("Web Components", (hooks) => {
assert.equal(document.activeElement, target.querySelector(".o_select_menu_input input"));
});
QUnit.test("value props accept array", async (assert) => {
class Parent extends Component {
setup() {
this.choices = [
{ label: "Z", value: [1, 2] },
{ label: "A", value: [1, 3] },
];
this.value = [1, 2];
}
}
Parent.components = { SelectMenu };
Parent.template = xml`
<SelectMenu
choices="this.choices"
value="this.value"
/>
`;
await mount(Parent, target, { env });
assert.equal(
target.querySelector(".o_select_menu_toggler_slot").innerText,
"Z",
`The select value shoud be "Z"`
);
});
QUnit.test("value props accept object", async (assert) => {
class Parent extends Component {
setup() {
this.choices = [
{ label: "Z", value: { hello: "world" } },
{ label: "A", value: { paper: "company" } },
];
this.value = { paper: "company" };
}
}
Parent.components = { SelectMenu };
Parent.template = xml`
<SelectMenu
choices="this.choices"
value="this.value"
/>
`;
await mount(Parent, target, { env });
assert.equal(
target.querySelector(".o_select_menu_toggler_slot").innerText,
"A",
`The select value shoud be "A"`
);
});
QUnit.test(
"Clear button calls 'onSelect' with null value and appears only when value is not null",
async (assert) => {