Skip to content

Commit

Permalink
fix(ld-input): submit associated form on return
Browse files Browse the repository at this point in the history
  • Loading branch information
renet authored and borisdiakur committed Dec 15, 2021
1 parent 99f6494 commit f74d88c
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/liquid/components/ld-input/ld-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,18 @@ export class LdInput implements InnerFocusable, ClonesAttributes {
}

private handleKeyDown = (ev: KeyboardEvent) => {
const outerForm = this.el.closest('form')
const formToSubmit = this.form
? document.querySelector<HTMLFormElement>(`#${this.form}`) ?? outerForm
: outerForm

if (
this.el.getAttribute('aria-disabled') === 'true' &&
!['ArrowLeft', 'ArrowRight', 'Tab'].includes(ev.code)
!['ArrowLeft', 'ArrowRight', 'Tab'].includes(ev.key)
) {
ev.preventDefault()
} else if (!this.multiline && ev.key === 'Enter' && formToSubmit) {
formToSubmit.requestSubmit()
}
}

Expand All @@ -324,7 +331,6 @@ export class LdInput implements InnerFocusable, ClonesAttributes {
])

if (this.multiline) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { type, ...clonedAttributesWithoutType } = this.clonedAttributes
return (
<Host class={cl} onClick={this.handleClick}>
Expand All @@ -336,7 +342,7 @@ export class LdInput implements InnerFocusable, ClonesAttributes {
part="input focusable"
ref={(el) => (this.input = el)}
/>
{this.type === 'file' && (
{type === 'file' && (
<span class="ld-input__placeholder" part="placeholder">
{this.input?.value || this.placeholder}
</span>
Expand Down
55 changes: 55 additions & 0 deletions src/liquid/components/ld-input/test/ld-input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,4 +423,59 @@ describe('ld-input', () => {
await waitForChanges()
expect(root).toMatchSnapshot()
})

it('requests submit on enter, if inside a form', async () => {
const { body, root } = await newSpecPage({
components: [LdInput],
html: `<form><ld-input name="example" /></form>`,
})

const form = body.querySelector('form')
form.requestSubmit = jest.fn()
const input = root.shadowRoot.querySelector('input')
input.dispatchEvent(
new KeyboardEvent('keydown', { key: 'Enter', bubbles: true })
)

expect(form.requestSubmit).toHaveBeenCalled()
})

it('requests submit on enter, if form attribute is given', async () => {
const { body, root } = await newSpecPage({
components: [LdInput],
html: `
<form id="test"></form>
<ld-input form="test" name="example" />`,
})

const form = body.querySelector('form')
form.requestSubmit = jest.fn()
const input = root.shadowRoot.querySelector('input')
input.dispatchEvent(
new KeyboardEvent('keydown', { key: 'Enter', bubbles: true })
)

expect(form.requestSubmit).toHaveBeenCalled()
})

it('prefers form attribute over surrounding form when requesting submit', async () => {
const { body, root } = await newSpecPage({
components: [LdInput],
html: `
<form id="test"></form>
<form id="surrounding"><ld-input form="test" name="example" /></form>`,
})

const referencedForm = body.querySelector<HTMLFormElement>('form#test')
const surroundingForm = body.querySelector<HTMLFormElement>('#surrounding')
referencedForm.requestSubmit = jest.fn()
surroundingForm.requestSubmit = jest.fn()
const input = root.shadowRoot.querySelector('input')
input.dispatchEvent(
new KeyboardEvent('keydown', { key: 'Enter', bubbles: true })
)

expect(referencedForm.requestSubmit).toHaveBeenCalled()
expect(surroundingForm.requestSubmit).not.toHaveBeenCalled()
})
})

0 comments on commit f74d88c

Please sign in to comment.