-
Notifications
You must be signed in to change notification settings - Fork 471
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(cdk): fix tuiFocusedIn error when focus events happened in reacti…
…ve context (#10020)
- Loading branch information
Showing
4 changed files
with
55 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import {untracked} from '@angular/core'; | ||
import type {SchedulerAction, SchedulerLike, Subscription} from 'rxjs'; | ||
import {queueScheduler} from 'rxjs'; | ||
|
||
export const tuiUntrackedScheduler: SchedulerLike = { | ||
now: queueScheduler.now.bind(queueScheduler), | ||
|
||
schedule<T>( | ||
work: (this: SchedulerAction<T>, state?: T) => void, | ||
delay?: number, | ||
state?: T, | ||
): Subscription { | ||
return queueScheduler.schedule( | ||
function (this: SchedulerAction<T>, s?: T) { | ||
return untracked(() => work.call(this, s)); | ||
}, | ||
delay, | ||
state, | ||
); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
import type {Signal} from '@angular/core'; | ||
import {toSignal} from '@angular/core/rxjs-interop'; | ||
import {TUI_FALSE_HANDLER, TUI_TRUE_HANDLER} from '@taiga-ui/cdk/constants'; | ||
import {fromEvent, map, merge} from 'rxjs'; | ||
import {tuiUntrackedScheduler} from '@taiga-ui/cdk/observables'; | ||
import {fromEvent, map, merge, observeOn} from 'rxjs'; | ||
|
||
export function tuiFocusedIn(node: Node): Signal<boolean> { | ||
return toSignal( | ||
merge( | ||
fromEvent(node, 'focusin').pipe(map(TUI_TRUE_HANDLER)), | ||
fromEvent(node, 'focusout').pipe(map(TUI_FALSE_HANDLER)), | ||
), | ||
).pipe(observeOn(tuiUntrackedScheduler)), | ||
{initialValue: false}, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// import {REACTIVE_NODE, setActiveConsumer} from '@angular/core/primitives/signals'; | ||
// import {TestBed} from '@angular/core/testing'; | ||
// import {tuiFocusedIn} from '@taiga-ui/cdk'; | ||
|
||
describe('tuiFocusedIn', () => { | ||
// eslint-disable-next-line jest/prefer-todo | ||
it('should change value in reactive context', () => { | ||
// TODO uncomment after update to angular@18 | ||
// const element = document.createElement('input'); | ||
// | ||
// const isFocused = TestBed.runInInjectionContext(() => tuiFocusedIn(element)); | ||
// | ||
// expect(isFocused()).toBe(false); | ||
// | ||
// setActiveConsumer({ | ||
// ...REACTIVE_NODE, | ||
// consumerAllowSignalWrites: false, | ||
// }); | ||
// | ||
// element.dispatchEvent(new Event('focusin')); | ||
// | ||
// expect(isFocused()).toBe(true); | ||
// | ||
// element.dispatchEvent(new Event('focusout')); | ||
// | ||
// expect(isFocused()).toBe(false); | ||
// | ||
// setActiveConsumer(null); | ||
}); | ||
}); |