Repository navigation
Prevent Enter from selecting after Virtual Select is disabled #494
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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'); | ||
| keydown('Enter', 13); | ||
|
|
||
| cy.get(`#${mountId}`).should('have.prop', 'value', 'o1'); | ||
| cy.get('@change').should('not.have.been.called'); | ||
| }); | ||
| }); | ||
| 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> | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This page loads Could you rebuild |
||
| <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> | ||
There was a problem hiding this comment.
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.