Skip to content

Commit

Permalink
fix: Pix Select focused wrong when other elements had the aria-select…
Browse files Browse the repository at this point in the history
…ed to true.

Co-authored-by: Laura Bergoens <[email protected]>
Co-authored-by: Jérémie Jadé <[email protected]>
Co-authored-by: Fael Bassetti <[email protected]>
  • Loading branch information
4 people authored and Laura Bergoens committed Oct 16, 2024
1 parent fbab995 commit 97ea066
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
8 changes: 6 additions & 2 deletions addon/components/pix-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ export default class PixSelect extends Component {
element.style.setProperty('--pix-select-width', `${selectWidth}rem`);
});
}

this.elementHelper.waitForElement(`container-${this.selectId}`).then((element) => {
this.rootElement = element;
});
}

get displayDefaultOption() {
Expand Down Expand Up @@ -151,11 +155,11 @@ export default class PixSelect extends Component {
focus() {
if (this.isExpanded) {
if (this.args.value) {
document.querySelector("[aria-selected='true']").focus();
this.rootElement.querySelector("[aria-selected='true']").focus();
} else if (this.args.isSearchable) {
document.getElementById(this.searchId).focus();
} else if (this.displayDefaultOption) {
document.querySelector("[aria-selected='true']").focus();
this.rootElement.querySelector("[aria-selected='true']").focus();
}
}
}
Expand Down
24 changes: 24 additions & 0 deletions tests/integration/components/pix-select-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,30 @@ module('Integration | Component | PixSelect', function (hooks) {
// then
assert.dom(select).isFocused();
});

test("it should focus first element of Pix Select even when there's an aria-selected=true somewhere", async function (assert) {
// given
const screen = await render(
hbs`<div role='tabpanel'>
<div aria-label='Write' tabindex='0'>Je ne dois pas prendre le focus !</div>
</div>
<PixSelect @options={{this.options}} @placeholder={{this.placeholder}}><:label
>{{this.label}}</:label></PixSelect>`,
);

// when
await screen.getByLabelText('Mon menu déroulant').focus();

await userEvent.keyboard('[ArrowDown]');

await screen.findByRole('listbox');
fireEvent(document.querySelector('.pix-select__dropdown'), new Event('transitionend'));

const option = screen.getByRole('option', { name: 'Choisissez une option' });

// then
assert.dom(option).isFocused();
});
});
});

Expand Down

0 comments on commit 97ea066

Please sign in to comment.