Skip to content

Commit

Permalink
[BUGFIX] Le focus de Pix Select ne fonctionne pas dans tous les cas (…
Browse files Browse the repository at this point in the history
…PIX-14876)

 #743
  • Loading branch information
pix-service-auto-merge authored Oct 16, 2024
2 parents 65e9b64 + fa78620 commit 00e796a
Show file tree
Hide file tree
Showing 2 changed files with 34 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
28 changes: 28 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,34 @@ 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 class='tab-interface'>
<div role='tablist' aria-label='Sample Tabs'>
<span role='tab' aria-selected='true' aria-controls='panel-1' id='tab-1' tabindex='0'>
Je ne dois pas être focus !
</span>
</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 00e796a

Please sign in to comment.