diff --git a/cypress/e2e/a11y-live-region.cy.ts b/cypress/e2e/a11y-live-region.cy.ts index d66240c..d77db65 100644 --- a/cypress/e2e/a11y-live-region.cy.ts +++ b/cypress/e2e/a11y-live-region.cy.ts @@ -68,9 +68,21 @@ describe('A11y: status announcements via a live region', () => { liveRegion().should('have.text', '5 results available'); }); + /** + * The term is entered in one input event rather than typed key by key. Only the full term + * "Option 3" narrows the list to one match - "Opt", "Option" and "Option " all match five - + * so this is the one search case where a lost keystroke changes the outcome. In CI the run + * ended with "Opt" in the box and "5 results available" in the region: the component + * announced correctly for what it received, the rest of the keys went elsewhere. Waiting for + * the component to move focus into the input itself (the last step of opening) and then + * setting the value in one step leaves nothing to lose, and still goes through the real + * input handler and its focus check. + */ it('uses the singular form for a single match', () => { cy.get(`#${mountId}`).find('.vscomp-toggle-button').click(); - cy.get(`#${mountId}`).find('.vscomp-search-input').focus().type('Option 3'); + cy.get(`#${mountId}`).find('.vscomp-search-input').should('have.focus'); + cy.get(`#${mountId}`).find('.vscomp-search-input').invoke('val', 'Option 3').trigger('input'); + cy.get(`#${mountId}`).find('.vscomp-search-input').should('have.value', 'Option 3').and('have.focus'); liveRegion().should('have.text', '1 result available'); }); diff --git a/cypress/e2e/single-select-new-option-height.cy.ts b/cypress/e2e/single-select-new-option-height.cy.ts new file mode 100644 index 0000000..05bb6a5 --- /dev/null +++ b/cypress/e2e/single-select-new-option-height.cy.ts @@ -0,0 +1,73 @@ +/** + * Single select + allowNewOption: the options list must be sized for the option the user just + * added. selectOption() closes the dropbox before it persists the new option, and the close + * recomputes the list height without it - so the list came back one row short (0px on an + * empty list) and `has-no-options` stayed on until something else happened to recompute. + */ +describe('Single select: adding a new option keeps the options list sized', () => { + const id = 'vs-single-new-option-height'; + const optionHeight = 40; + + const addNewOption = (value: string) => { + cy.getVs(id).click(); + cy.getVs(id).find('.vscomp-wrapper').should('not.have.class', 'closed'); + cy.getVs(id).typeValue(value); + cy.getDropbox(null, id).find('.vscomp-option.current-new.focused').should('contain.text', value); + /** + * Enter rather than click: each keystroke re-renders the option rows, so a click can lose + * its target between query and action. Enter selects the focused row through the same + * selectOption() path a click takes. + */ + cy.getVs(id).pressKeys('Enter'); + /** single select closes on select; wait for the close to land before reopening */ + cy.getVs(id).find('.vscomp-wrapper').should('have.class', 'closed'); + }; + + const reopen = () => { + cy.getVs(id).click(); + cy.getVs(id).find('.vscomp-wrapper').should('not.have.class', 'closed'); + }; + + before(() => { + cy.visit('get-started'); + + cy.window().then((win) => { + const $ele = win.document.createElement('div'); + $ele.id = id; + win.document.body.appendChild($ele); + + // @ts-expect-error - VirtualSelect is attached to window by the bundle + win.VirtualSelect.init({ + ele: $ele, + allowNewOption: true, + multiple: false, + options: [], + }); + }); + }); + + it('sizes the list for the option added to an empty list', () => { + addNewOption('Alpha'); + cy.getVs(id).hasValueText('Alpha'); + + reopen(); + + cy.getVs(id).find('.vscomp-wrapper').should('not.have.class', 'has-no-options'); + cy.getDropbox(null, id).find('.vscomp-options-list').should('have.css', 'height', `${optionHeight}px`); + cy.getDropbox(null, id).find('.vscomp-option[data-value="Alpha"]').should('be.visible'); + }); + + it('grows the list by one row for the next option added', () => { + cy.getVs(id).click(); + cy.getVs(id).find('.vscomp-wrapper').should('have.class', 'closed'); + + addNewOption('Beta'); + cy.getVs(id).hasValueText('Beta'); + + reopen(); + + cy.getDropbox(null, id).find('.vscomp-options-list').should('have.css', 'height', `${optionHeight * 2}px`); + cy.getDropbox(null, id).find('.vscomp-option[data-value="Alpha"]').should('be.visible'); + cy.getDropbox(null, id).find('.vscomp-option[data-value="Beta"]').should('be.visible'); + }); +}); diff --git a/eslint.config.mjs b/eslint.config.mjs index dbe546b..99b000f 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -74,7 +74,22 @@ export default [ '@stylistic/linebreak-style': ['error', 'unix'], '@stylistic/max-len': ['error', { code: 120 }], '@stylistic/no-trailing-spaces': 'error', - '@stylistic/object-curly-newline': ['error', { ObjectPattern: { multiline: true } }], + // Successor of the deprecated object-curly-newline. list-style covers every bracketed list by default, + // which would fight Prettier (e.g. argument hugging), so it is scoped to destructuring patterns only: + // a pattern is either fully on one line or has every property on its own line. + '@stylistic/list-style': [ + 'error', + { + overrides: { + '()': 'off', + '[]': 'off', + '{}': 'off', + '<>': 'off', + // Must restate the spacing: the plugin's '{}' default (spacing: always) is dropped once '{}' is off. + ObjectPattern: { singleLine: { spacing: 'always' } }, + }, + }, + ], '@stylistic/operator-linebreak': ['error', 'after', { overrides: { '?': 'before', ':': 'before' } }], '@stylistic/quote-props': ['error', 'consistent'], }, diff --git a/src/virtual-select.js b/src/virtual-select.js index 2b6a02c..91f3e55 100644 --- a/src/virtual-select.js +++ b/src/virtual-select.js @@ -1058,6 +1058,17 @@ export class VirtualSelect { // when dropdown close resets search, so re-add as a persisted new option. this.setNewOption(selectedValue); this.toggleSelectedProp(this.lastOptionIndex, true); + + /** + * The close that removed the temporary option also recomputed the visible count, the + * list height and `has-no-options` without it, and nothing after this point recomputes + * them: setNewOption() re-adds through addOption() with no rerender, and the deferred + * setSearchValue('') below is a no-op because the close already emptied the search. + * So the list came back one row short - 0px on an empty list, with the "no options" + * message still showing - until some later filter change happened to recount. + * Recount now that the option is a regular one; the count includes it from here on. + */ + this.setVisibleOptionsCount(); } /** using setTimeout to fix the issue of dropbox getting closed on select */