fix(combobox): do not submit form if overlay of combobox is opened and ENTER is hit (#2481)
This commit is contained in:
parent
95b70d32c8
commit
23cd9841ed
3 changed files with 57 additions and 3 deletions
5
.changeset/witty-oranges-lick.md
Normal file
5
.changeset/witty-oranges-lick.md
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'@lion/ui': patch
|
||||
---
|
||||
|
||||
[combobox] do not submit form if overlay of combobox is opened and ENTER is hit
|
||||
|
|
@ -1134,7 +1134,7 @@ export class LionCombobox extends LocalizeMixin(OverlayMixin(CustomChoiceGroupMi
|
|||
}
|
||||
break;
|
||||
case 'Enter':
|
||||
if (this.multipleChoice && this.opened) {
|
||||
if (this.opened) {
|
||||
ev.preventDefault();
|
||||
}
|
||||
|
||||
|
|
@ -1145,8 +1145,6 @@ export class LionCombobox extends LocalizeMixin(OverlayMixin(CustomChoiceGroupMi
|
|||
this.formElements[this.activeIndex].hasAttribute('aria-hidden') ||
|
||||
!this.opened)
|
||||
) {
|
||||
ev.preventDefault();
|
||||
|
||||
this.modelValue = this.parser([...this.modelValue, this._inputNode.value]);
|
||||
this._inputNode.value = '';
|
||||
this.opened = false;
|
||||
|
|
|
|||
|
|
@ -985,6 +985,57 @@ describe('lion-combobox', () => {
|
|||
await el.updateComplete;
|
||||
expect(el.modelValue).to.eql(['Art']);
|
||||
});
|
||||
|
||||
it('submits form on [Enter] when listbox is closed', async () => {
|
||||
const submitSpy = sinon.spy(e => e.preventDefault());
|
||||
const el = /** @type {HTMLFormElement} */ (
|
||||
await fixture(html`
|
||||
<form @submit=${submitSpy}>
|
||||
<lion-combobox name="foo">
|
||||
<lion-option .choiceValue="${'Artichoke'}">Artichoke</lion-option>
|
||||
<lion-option .choiceValue="${'Chard'}">Chard</lion-option>
|
||||
<lion-option .choiceValue="${'Chicory'}">Chicory</lion-option>
|
||||
<lion-option .choiceValue="${'Victoria Plum'}">Victoria Plum</lion-option>
|
||||
</lion-combobox>
|
||||
<button type="submit">submit</button>
|
||||
</form>
|
||||
`)
|
||||
);
|
||||
const combobox = /** @type {LionCombobox} */ (el.querySelector('[name="foo"]'));
|
||||
const { _inputNode } = getComboboxMembers(combobox);
|
||||
await combobox.updateComplete;
|
||||
_inputNode.focus();
|
||||
await sendKeys({
|
||||
press: 'Enter',
|
||||
});
|
||||
expect(submitSpy.callCount).to.equal(1);
|
||||
});
|
||||
|
||||
it('does not submit form on [Enter] when listbox is opened', async () => {
|
||||
const submitSpy = sinon.spy(e => e.preventDefault());
|
||||
const el = /** @type {HTMLFormElement} */ (
|
||||
await fixture(html`
|
||||
<form @submit=${submitSpy}>
|
||||
<lion-combobox name="foo">
|
||||
<lion-option .choiceValue="${'Artichoke'}">Artichoke</lion-option>
|
||||
<lion-option .choiceValue="${'Chard'}">Chard</lion-option>
|
||||
<lion-option .choiceValue="${'Chicory'}">Chicory</lion-option>
|
||||
<lion-option .choiceValue="${'Victoria Plum'}">Victoria Plum</lion-option>
|
||||
</lion-combobox>
|
||||
<button type="submit">submit</button>
|
||||
</form>
|
||||
`)
|
||||
);
|
||||
const combobox = /** @type {LionCombobox} */ (el.querySelector('[name="foo"]'));
|
||||
const { _inputNode } = getComboboxMembers(combobox);
|
||||
combobox.opened = true;
|
||||
await combobox.updateComplete;
|
||||
_inputNode.focus();
|
||||
await sendKeys({
|
||||
press: 'Enter',
|
||||
});
|
||||
expect(submitSpy.callCount).to.equal(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Overlay visibility', () => {
|
||||
|
|
|
|||
Loading…
Reference in a new issue