Skip to content
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
39 changes: 39 additions & 0 deletions cypress/e2e/a11y-disable-while-open.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/** cSpell:ignore vscomp */

import { makeOptions, mountVs, unmountVs } from '../support/mount';

describe('Disabling an open Select', () => {
const mountId = 'vs-disable-while-open';

afterEach(() => {
cy.window().then((win) => unmountVs(win, mountId));
});

it('does not commit a keyboard selection after disabling', () => {
cy.viewport(1280, 800);
cy.visit('get-started');
cy.window().then((win) => mountVs(win, mountId, { options: makeOptions(3), search: true, selectedValue: 'o1' }));
cy.get(`#${mountId} .vscomp-toggle-button`).click();
cy.get(`#${mountId} .vscomp-search-input`).should('be.focused');

cy.get(`#${mountId}`).then(($host) => {
const host = $host[0] as HTMLElement & { disable: () => void };
host.disable();
host.addEventListener('change', cy.spy().as('change'));
});

const keydown = (key: string, keyCode: number) => {
cy.get(`#${mountId} .vscomp-search-input`).should('be.focused').then(($input) => {
$input[0].dispatchEvent(new KeyboardEvent('keydown', { key, keyCode, bubbles: true, cancelable: true }));
});
};

keydown('ArrowDown', 40);
keydown('ArrowDown', 40);
cy.get(`#${mountId} .vscomp-option.focused`).should('not.have.attr', 'data-value', 'o1');
Comment on lines +31 to +33

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assertion requires ArrowDown to keep moving the highlight after disable(), which locks in behaviour we probably don't want. A more complete fix (closing the dropbox on disable, or ignoring arrow keys while disabled) would make this line fail.

Suggestion: move the ArrowDown presses and highlight check before host.disable(), then after disabling assert that Enter (and ideally ArrowDown) are ignored.

keydown('Enter', 13);

cy.get(`#${mountId}`).should('have.prop', 'value', 'o1');
cy.get('@change').should('not.have.been.called');
});
});
2 changes: 1 addition & 1 deletion cypress/e2e/a11y-live-region.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import { makeOptions, mountVs, unmountVs } from '../support/mount';

describe('A11y: status announcements via a live region', () => {
describe('A11y: status announcements via a live region', { testIsolation: true }, () => {
const mountId = 'vs-a11y-live';

const liveRegion = () => cy.get(`#${mountId}`).find('.vscomp-live-region');
Expand Down
51 changes: 51 additions & 0 deletions docs/examples/disabled-keyboard.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Disable an open Virtual Select</title>
<link rel="stylesheet" href="../assets/virtual-select.min.css">
</head>
<body>
<h1>Disable an open Virtual Select</h1>
<p>Click "Disable in 3 seconds", open Fruit, and highlight Orange with Arrow Down.
After the status says "Disabled", press Enter. Apple must stay selected and the change count must stay zero.</p>
<button id="disable" type="button">Disable in 3 seconds</button>
<button id="enable" type="button">Enable</button>
<label id="fruit-label">Fruit</label>
<div id="fruit"></div>
<p id="status" role="status">Enabled</p>
<p>Change count <output id="changes">0</output></p>
<script src="../assets/virtual-select.js"></script>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This page loads ../assets/virtual-select.js, but the committed bundle in docs/assets still has the old onEnterPress (else if (this.$ele.disabled === false)), since the PR only changes src/. Following the steps on the page as committed (open, ArrowDown to Orange, wait for "Disabled", Enter) still selects Orange and bumps the change count to 1.

Could you rebuild docs/assets in this PR, or add a note on the page that it needs a fresh build?

<script>
const fruit = document.getElementById('fruit');
const status = document.getElementById('status');
const changes = document.getElementById('changes');
let timer;
VirtualSelect.init({
ele: fruit,
ariaLabelledby: 'fruit-label',
search: true,
selectedValue: 'apple',
options: [{label: 'Apple', value: 'apple'}, {label: 'Orange', value: 'orange'}]
});
fruit.addEventListener('change', () => {
changes.value = String(Number(changes.value) + 1);
});
document.getElementById('disable').addEventListener('click', () => {
clearTimeout(timer);
changes.value = '0';
status.textContent = 'Disabling in 3 seconds';
timer = setTimeout(() => {
fruit.disable();
status.textContent = 'Disabled';
}, 3000);
});
document.getElementById('enable').addEventListener('click', () => {
clearTimeout(timer);
fruit.enable();
status.textContent = 'Enabled';
});
</script>
</body>
</html>
6 changes: 5 additions & 1 deletion src/virtual-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -753,9 +753,13 @@ export class VirtualSelect {
onEnterPress(e) {
e.preventDefault();

if (this.$ele.disabled) {
return;
}

if (this.isOpened()) {
this.selectFocusedOption();
} else if (this.$ele.disabled === false) {
} else {
this.openDropbox();
}
}
Expand Down