Skip to content

Commit

Permalink
🐛 fix(): Replace timeout with await waitAfterIdleCallback in inputSet…
Browse files Browse the repository at this point in the history
…Focus (#1177)

* fix(): Replace timeout with await waitAfterIdleCallback in inputSetFocus

* chore: add missing select

---------

Co-authored-by: Mladen Planinicic <[email protected]>
Co-authored-by: Gery Hirschfeld <[email protected]>
  • Loading branch information
3 people authored Dec 5, 2023
1 parent 160f163 commit 8a851fa
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/kind-parrots-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@baloise/design-system-components': patch
---

Replace timeout with 'await waitAfterIdleCallback()' in inputSetFocus.
14 changes: 5 additions & 9 deletions packages/components/src/components/bal-select/bal-select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
ComponentInterface,
} from '@stencil/core'
import isNil from 'lodash.isnil'
import { debounce, deepReady, isDescendant, rIC } from '../../utils/helpers'
import { debounce, deepReady, isDescendant, rIC, waitAfterIdleCallback } from '../../utils/helpers'
import {
areArraysEqual,
isArrowDownKey,
Expand Down Expand Up @@ -392,12 +392,10 @@ export class Select implements ComponentInterface, Loggable, BalAriaFormLinking
*/
@Method()
async setFocus() {
clearTimeout(this.setFocusTimer)
this.setFocusTimer = setTimeout(() => {
if (this.inputElement && !this.disabled) {
this.inputElement.focus()
}
})
if (this.inputElement && !this.disabled) {
await waitAfterIdleCallback()
this.inputElement.focus()
}
}

/**
Expand Down Expand Up @@ -523,8 +521,6 @@ export class Select implements ComponentInterface, Loggable, BalAriaFormLinking
}
}

private setFocusTimer?: NodeJS.Timer

/**
* GETTERS
* ------------------------------------------------------
Expand Down
12 changes: 4 additions & 8 deletions packages/components/src/utils/form-input.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { EventEmitter } from '@stencil/core'
import { waitAfterIdleCallback } from './helpers'

export interface FormInput<Value> {
el: HTMLElement
Expand Down Expand Up @@ -47,14 +48,9 @@ export const inputListenOnClick = <Value>(component: FormInput<Value>, ev: UIEve
}
}

let inputSetFocusTimer: NodeJS.Timer | undefined
export const inputSetFocus = <Value>(component: FormInput<Value>): void => {
clearTimeout(inputSetFocusTimer)
inputSetFocusTimer = setTimeout(() => {
if (component.nativeInput) {
component.nativeInput.focus()
}
}, 0)
export const inputSetFocus = async <Value>(component: FormInput<Value>): Promise<void> => {
await waitAfterIdleCallback()
component?.nativeInput?.focus()
}

export const inputHandleHostClick = <Value>(component: FormInput<Value>, ev: MouseEvent) => {
Expand Down

0 comments on commit 8a851fa

Please sign in to comment.