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): handling duplicates when using value or default value … #740

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ export class OdsCombobox implements OdsFormElement {
value: value,
}];
this.value = value;
if (this.inputElement) {
this.inputElement.value = event.detail.text;
}
}
this.currentFocusedItemIndex = updateItemsFocus(this.resultElements, this.currentFocusedItemIndex, 'reset');
this.closeDropdown();
Expand Down Expand Up @@ -350,10 +353,9 @@ export class OdsCombobox implements OdsFormElement {

const itemValue = (resultElement.searchLabel || resultElement.textContent || '').trim();
const doesMatchValue = filterRegex.test(itemValue);

resultElement.isVisible = doesMatchValue;

if (doesMatchValue && noMatch) {
const isAlreadySelected = this.allowMultiple && this.currentSelections.some((selection) => selection.value === resultElement.value);
resultElement.isVisible = doesMatchValue && !isAlreadySelected;
if (doesMatchValue && !isAlreadySelected && noMatch) {
noMatch = false;
}
});
Expand All @@ -366,7 +368,8 @@ export class OdsCombobox implements OdsFormElement {
this.hasNoResults = noMatch;
} else {
this.resultElements?.forEach((resultElement) => {
resultElement.isVisible = true;
const isAlreadySelected = this.allowMultiple && this.currentSelections.some((selection) => selection.value === resultElement.value);
resultElement.isVisible = !isAlreadySelected;
});
this.hasNoResults = false;
}
Expand Down Expand Up @@ -559,6 +562,10 @@ export class OdsCombobox implements OdsFormElement {
this.shouldUpdateSelection = false;
this.value = inlineSelection(this.currentSelections);

if (this.isOpen) {
this.filterResults(this.inputElement?.value || '');
}

if (this.currentSelections.length === 0
|| this.currentTagFocusedIndex === -1
|| this.currentTagFocusedIndex >= this.currentSelections.length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -618,4 +618,38 @@ describe('ods-combobox behaviour', () => {
expect(url.searchParams.get('odsCombobox')).toBe('value');
});
});

describe('keyboard navigation', () => {
it('should allow to select a value, delete it with keyboard and reselect it in single mode', async() => {
await setup(`
<ods-combobox allow-new-element="false">
<ods-combobox-item value="test">Test value</ods-combobox-item>
</ods-combobox>
`);
const odsChangeSpy = await page.spyOnEvent('odsChange');

await openList();
await input.press('ArrowDown');
await input.press('Enter');
await page.waitForChanges();

expect(await el.getProperty('value')).toBe('test');
expect(await input.getProperty('value')).toBe('Test value');
expect(odsChangeSpy).toHaveReceivedEventTimes(1);

await input.press('Backspace');
await page.waitForChanges();

expect(await el.getProperty('value')).toBe('test');
expect(await input.getProperty('value')).toBe('Test valu');

await openList();
await input.press('ArrowDown');
await input.press('Enter');
await page.waitForChanges();

expect(await el.getProperty('value')).toBe('test');
expect(await input.getProperty('value')).toBe('Test value');
});
});
});