Skip to content

Commit

Permalink
fix(cdk): fix tuiFocusedIn error when focus events happened in reacti…
Browse files Browse the repository at this point in the history
…ve context (#10020)
  • Loading branch information
MillerSvt authored Dec 18, 2024
1 parent 97d24ad commit 12e9a29
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
1 change: 1 addition & 0 deletions projects/cdk/observables/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ export * from './query-list-observable';
export * from './scroll-from';
export * from './take-until-destroyed';
export * from './typed-from-event';
export * from './untracked-scheduler';
export * from './watch';
export * from './zone';
21 changes: 21 additions & 0 deletions projects/cdk/observables/untracked-scheduler.ts
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,
);
},
};
5 changes: 3 additions & 2 deletions projects/cdk/utils/focus/focused-in.ts
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},
);
}
30 changes: 30 additions & 0 deletions projects/cdk/utils/focus/tests/focused-in.spec.ts
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);
});
});

0 comments on commit 12e9a29

Please sign in to comment.