Skip to content

fix(ui5-select): prevent crash on ArrowUp/Down when value matches no option #12094

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 id="sel-down" value="missing">
<Option value="A">A</Option>
<Option value="B">B</Option>
<Option value="C">C</Option>
</Select>
);

cy.get("#sel-down")
.should("have.attr", "value", "missing")
.find("[ui5-option][selected]")
.should("not.exist");

cy.get("#sel-down").realClick().realPress("ArrowDown");

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

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

cy.get("#sel-up")
.should("have.attr", "value", "missing")
.find("[ui5-option][selected]")
.should("not.exist");

cy.get("#sel-up").realClick().realPress("ArrowUp");

cy.get("#sel-up")
.find("[ui5-option]")
.eq(2)
.should("have.attr", "selected");
cy.get("#sel-up").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