Skip to content

Commit

Permalink
feat(select): a11y improvement on select and option components (#588)
Browse files Browse the repository at this point in the history
Co-authored-by: mehmetcan.boz <[email protected]>
Co-authored-by: Murat Çorlu <[email protected]>
  • Loading branch information
3 people authored Jul 14, 2023
1 parent fc9af92 commit 0d99a12
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/components/select/bl-select.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ describe('bl-select', () => {
<fieldset
class="select-input"
tabindex="0"
role="button"
aria-haspopup="listbox"
aria-labelledby="label"
aria-expanded="false"
>
<legend><span></span></legend>
<span class="placeholder"></span>
Expand All @@ -42,7 +46,11 @@ describe('bl-select', () => {
<bl-icon class="dropdown-icon closed" name="arrow_down"></bl-icon>
</div>
</fieldset>
<div class="popover" tabindex="-1">
<div class="popover" tabindex="-1"
role="listbox"
aria-multiselectable="false"
aria-labelledby="label"
>
<slot></slot>
</div>
<div class="hint"></div>
Expand Down
9 changes: 8 additions & 1 deletion src/components/select/bl-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,10 @@ export default class BlSelect<ValueType extends FormValue = string> extends Form
tabindex="${this.disabled ? '-1' : 0}"
?autofocus=${this.autofocus}
@click=${this.togglePopover}
role="button"
aria-haspopup="listbox"
aria-expanded="${this.opened}"
aria-labelledby="label"
>
<legend><span>${this.label}</span></legend>
<span class="placeholder">${this.placeholder}</span>
Expand All @@ -332,7 +336,7 @@ export default class BlSelect<ValueType extends FormValue = string> extends Form

const helpMessage = this.helpText ? html`<p class="help-text">${this.helpText}</p>` : '';

const label = this.label ? html`<label>${this.label}</label>` : '';
const label = this.label ? html`<label id="label">${this.label}</label>` : '';

return html`<div
class=${classMap({
Expand All @@ -349,6 +353,9 @@ export default class BlSelect<ValueType extends FormValue = string> extends Form
class="popover"
tabindex="${ifDefined(this._isPopoverOpen ? undefined : '-1')}"
@bl-select-option=${this._handleSelectOptionEvent}
role="listbox"
aria-multiselectable="${this.multiple}"
aria-labelledby="label"
>
<slot></slot>
</div>
Expand Down
24 changes: 24 additions & 0 deletions src/components/select/option/bl-select-option.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,30 @@ describe('bl-select', () => {
assert.instanceOf(el, BlSelectOption);
});

it('renders with default values', async () => {
const el = await fixture<BlSelectOption>(html`<bl-select-option></bl-select-option>`);

expect(el).shadowDom.equal(
`<div class="option-container">
<div class="focus-target single-option" role="option" aria-selected="false">
<slot>
</slot>
</div>
</div>
`
);
});

it('should have aria-selected attribute set to true if the option is selected', async function () {
const el = await fixture<BlSelectOption>(
html`<bl-select-option value="basketball" selected>Basketball</bl-select-option>`
);

const option = el.shadowRoot?.querySelector<HTMLButtonElement>('.focus-target');

expect(option).has.attribute('aria-selected', 'true');
});

describe('keyboard navigation', () => {
it('should get focus', async () => {
//when
Expand Down
4 changes: 4 additions & 0 deletions src/components/select/option/bl-select-option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ export default class BlSelectOption<ValueType extends FormValue = string> extend
@blur=${this.blur}
@keydown=${this.handleKeydown}
@click="${this._onClickOption}"
role="option"
aria-selected="${this.selected}"
>
<slot></slot>
</div>`;
Expand All @@ -96,6 +98,8 @@ export default class BlSelectOption<ValueType extends FormValue = string> extend
.checked="${this.selected}"
.disabled="${this.disabled}"
@bl-checkbox-change="${this._onCheckboxChange}"
role="option"
aria-selected="${this.selected}"
>
<slot></slot>
</bl-checkbox>`;
Expand Down

0 comments on commit 0d99a12

Please sign in to comment.