Skip to content

Commit

Permalink
fix(select): fix disabled select can be open using keydowns
Browse files Browse the repository at this point in the history
  • Loading branch information
Erbil Nas committed Feb 8, 2024
1 parent 75104c0 commit 22f71c7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
19 changes: 19 additions & 0 deletions src/components/select/bl-select.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,25 @@ describe("bl-select", () => {
expect(document.activeElement).to.not.equal(blSelect);
});


it("should not open popover if it is disabled", async () => {
// if it is disabled, it should not open popover and return from function
blSelect.disabled = true;

// given

await sendKeys({
press: tabKey,
});

await sendKeys({
press: "Space",
});

// then
expect(blSelect.opened).to.equal(false);
});

["Space", "Enter", "ArrowDown", "ArrowUp"].forEach(keyCode => {
it(`should open popover with ${keyCode} key`, async () => {
//given
Expand Down
21 changes: 16 additions & 5 deletions src/components/select/bl-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export default class BlSelect<ValueType extends FormValue = string> extends Form
/**
* Views select all option in multiple select
*/
@property({ type: Boolean, attribute: "view-select-all" })
@property({ type: Boolean, attribute: "view-select-all", converter: stringBooleanConverter() })
viewSelectAll = false;

/**
Expand Down Expand Up @@ -434,12 +434,15 @@ export default class BlSelect<ValueType extends FormValue = string> extends Form
>`
: ""}
<div class="actions" @click=${this._togglePopover}>
<div class="actions">
${this.opened ? (this.searchBarLoadingState ? searchLoadingIcon : searchMagIcon) : ""}
${!this.opened ? removeButton : ""} ${actionDivider}
<bl-icon class="dropdown-icon open" name="arrow_up"></bl-icon>
<bl-icon class="dropdown-icon closed" name="arrow_down"></bl-icon>
<div @click=${this._togglePopover}>
<bl-icon class="dropdown-icon open" name="arrow_up"></bl-icon>
<bl-icon class="dropdown-icon closed" name="arrow_down"></bl-icon>
</div>
</div>
</fieldset>`;

Expand Down Expand Up @@ -548,7 +551,15 @@ export default class BlSelect<ValueType extends FormValue = string> extends Form
private focusedOptionIndex = -1;

private handleKeydown(event: KeyboardEvent) {
if (this.focusedOptionIndex === -1 && ["Enter", "Space"].includes(event.code)) {
if (
this.focusedOptionIndex === -1 &&
!this.searchBar &&
!this.disabled &&
["Space"].includes(event.code)
) {
this._togglePopover();
event.preventDefault();
} else if (["Enter"].includes(event.code) && !this.disabled) {
this._togglePopover();
event.preventDefault();
} else if (this._isPopoverOpen === false && ["ArrowDown", "ArrowUp"].includes(event.code)) {
Expand Down

0 comments on commit 22f71c7

Please sign in to comment.