Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions packages/main/cypress/specs/Select.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1393,4 +1393,50 @@ describe("Select general interaction", () => {

cy.get("[ui5-select]").should("have.prop", "value", "Third");
});

it("navigates with ArrowDown when initial value does not match any option", () => {
cy.mount(
<Select value="missing">
<Option value="A">A</Option>
<Option value="B">B</Option>
<Option value="C">C</Option>
</Select>
);

cy.get("[ui5-select]")
.should("have.prop", "value", "missing")
.find("[ui5-option][selected]")
.should("not.exist");

cy.get("[ui5-select]").realClick().realPress("ArrowDown");

cy.get("[ui5-select]")
.find("[ui5-option]")
.eq(0)
.should("have.attr", "selected");
cy.get("[ui5-select]").should("have.prop", "value", "A");
});

it("navigates with ArrowUp when initial value does not match any option", () => {
cy.mount(
<Select value="missing">
<Option value="A">A</Option>
<Option value="B">B</Option>
<Option value="C">C</Option>
</Select>
);

cy.get("[ui5-select]")
.should("have.prop", "value", "missing")
.find("[ui5-option][selected]")
.should("not.exist");

cy.get("[ui5-select]").realClick().realPress("ArrowUp");

cy.get("[ui5-select]")
.find("[ui5-option]")
.eq(2)
.should("have.attr", "selected");
cy.get("[ui5-select]").should("have.prop", "value", "C");
});
});
16 changes: 14 additions & 2 deletions packages/main/src/Select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -768,15 +768,27 @@ class Select extends UI5Element implements IFormInputElement {
_changeSelectedItem(oldIndex: number, newIndex: number) {
const options: Array<IOption> = this.options;

// Normalize: first navigation with Up when nothing selected -> last item
if (oldIndex === -1 && newIndex < 0 && options.length) {
newIndex = options.length - 1;
}

// Abort on invalid target
if (newIndex < 0 || newIndex >= options.length) {
return;
}

const previousOption = options[oldIndex];
const nextOption = options[newIndex];

if (previousOption === nextOption) {
return;
}

previousOption.selected = false;
previousOption.focused = false;
if (previousOption) {
previousOption.selected = false;
previousOption.focused = false;
}

nextOption.selected = true;
nextOption.focused = true;
Expand Down
Loading