diff --git a/cypress/e2e/a11y-disable-while-open.cy.ts b/cypress/e2e/a11y-disable-while-open.cy.ts new file mode 100644 index 0000000..65554b1 --- /dev/null +++ b/cypress/e2e/a11y-disable-while-open.cy.ts @@ -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'); + keydown('Enter', 13); + + cy.get(`#${mountId}`).should('have.prop', 'value', 'o1'); + cy.get('@change').should('not.have.been.called'); + }); +}); diff --git a/cypress/e2e/a11y-live-region.cy.ts b/cypress/e2e/a11y-live-region.cy.ts index d77db65..94080f4 100644 --- a/cypress/e2e/a11y-live-region.cy.ts +++ b/cypress/e2e/a11y-live-region.cy.ts @@ -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'); diff --git a/docs/examples/disabled-keyboard.html b/docs/examples/disabled-keyboard.html new file mode 100644 index 0000000..17b832c --- /dev/null +++ b/docs/examples/disabled-keyboard.html @@ -0,0 +1,51 @@ + + + + + + Disable an open Virtual Select + + + +

Disable an open Virtual Select

+

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.

+ + + +
+

Enabled

+

Change count 0

+ + + + diff --git a/src/virtual-select.js b/src/virtual-select.js index 91f3e55..4069a3c 100644 --- a/src/virtual-select.js +++ b/src/virtual-select.js @@ -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(); } }