Skip to content
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

fix(combobox): Do not clear the input when no item is selected #2289

Merged
merged 2 commits into from
Jul 17, 2023
Merged
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
16 changes: 16 additions & 0 deletions cypress/integration/Autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,22 @@ describe('Autocomplete', () => {
});
});
});

context('when the user types in a value not found', () => {
beforeEach(() => {
cy.findByRole('combobox').type('Peach');
});

context('when the user hits the enter key', () => {
beforeEach(() => {
cy.findByRole('combobox').type('{enter}');
});
});

it('should not clear the input', () => {
cy.findByRole('combobox').should('have.value', 'Peach');
});
});
});
});
});
7 changes: 2 additions & 5 deletions modules/react/combobox/lib/ComboboxInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,8 @@ export const useComboboxInput = composeHooks(
dispatchInputEvent(event.currentTarget as HTMLElement, '');
}
if (event.key === 'Enter' && !event.metaKey && model.state.visibility === 'visible') {
if (
document
.querySelector(`[data-id="${model.state.cursorId}"]`)
?.getAttribute('aria-disabled') !== 'true'
Copy link
Contributor

Choose a reason for hiding this comment

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

was this chaining?

Copy link
Member Author

Choose a reason for hiding this comment

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

Originally the conditional was:

document.querySelector(`[data-id="${model.state.cursorId}"]`)

"If the element exists, continue". Later we added the requirement that the element should not be disabled via aria-disabled. The updated logic turned out to be "If the element exists AND is not disabled OR the element does not exist" when it should be "If the element exists AND is not diabled"

) {
const element = document.querySelector(`[data-id="${model.state.cursorId}"]`);
if (element && element?.getAttribute('aria-disabled') !== 'true') {
model.events.select({id: model.state.cursorId});
if (model.state.mode === 'single') {
model.events.hide(event);
Expand Down
Loading