Skip to content

Commit

Permalink
fix(ld-input): cannot focus year on safari
Browse files Browse the repository at this point in the history
  • Loading branch information
borisdiakur committed Feb 2, 2022
1 parent 6fa267d commit 0c2137d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/liquid/components/ld-input/ld-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,9 @@ export class LdInput implements InnerFocusable, ClonesAttributes {

if (target.closest('ld-button')) return

this.input.focus()
if (this.el.shadowRoot.activeElement !== this.input) {
this.input.focus()
}

if (target === this.el) {
this.input.dispatchEvent(new MouseEvent('click', { bubbles: false }))
Expand Down
19 changes: 19 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 @@ -187,6 +187,25 @@ describe('ld-input', () => {
expect(input.focus).toHaveBeenCalledTimes(2)
})

it('does not focus the input on click of non-interactive elment inside the component if already focused', async () => {
const page = await newSpecPage({
components: [LdInput],
html: `<ld-input><span slot="end"><span id="banana">🍌</span></span></ld-input>`,
})
const ldInput = page.root
const banana = ldInput.querySelector('#banana') as HTMLElement
const input = ldInput.shadowRoot.querySelector('input')

const doc = ldInput.shadowRoot as unknown as { activeElement: Element }
doc.activeElement = input

input.focus = jest.fn()
banana.dispatchEvent(new Event('click', { bubbles: true }))
ldInput.dispatchEvent(new Event('click'))

expect(input.focus).not.toHaveBeenCalled()
})

it('forwards click to input (default)', async () => {
const { root } = await newSpecPage({
components: [LdInput],
Expand Down

0 comments on commit 0c2137d

Please sign in to comment.