MCPcopy Create free account
hub / github.com/angular/components / focusItem

Method focusItem

src/cdk/a11y/key-manager/tree-key-manager.ts:253–289  ·  view source on GitHub ↗
(itemOrIndex: number | T, options: {emitChangeEvent?: boolean} = {})

Source from the content-addressed store, hash-verified

251 focusItem(item: T, options?: {emitChangeEvent?: boolean}): void;
252 focusItem(itemOrIndex: number | T, options?: {emitChangeEvent?: boolean}): void;
253 focusItem(itemOrIndex: number | T, options: {emitChangeEvent?: boolean} = {}) {
254 // Set default options
255 options.emitChangeEvent ??= true;
256
257 let index =
258 typeof itemOrIndex === 'number'
259 ? itemOrIndex
260 : this._items.findIndex(item => this._trackByFn(item) === this._trackByFn(itemOrIndex));
261 if (index < 0 || index >= this._items.length) {
262 return;
263 }
264 const activeItem = this._items[index];
265
266 // If we're just setting the same item, don't re-call activate or focus
267 if (
268 this._activeItem !== null &&
269 this._trackByFn(activeItem) === this._trackByFn(this._activeItem)
270 ) {
271 return;
272 }
273
274 const previousActiveItem = this._activeItem;
275 this._activeItem = activeItem ?? null;
276 this._activeItemIndex = index;
277 this._typeahead?.setCurrentSelectedItemIndex(index);
278
279 this._activeItem?.focus();
280 previousActiveItem?.unfocus();
281
282 if (options.emitChangeEvent) {
283 this.change.next(this._activeItem);
284 }
285
286 if (this._shouldActivationFollowFocus) {
287 this._activateCurrentItem();
288 }
289 }
290
291 private _updateActiveItemIndex(newItems: T[]) {
292 const activeItem = this._activeItem;

Callers 8

_initializeFocusMethod · 0.95
_focusFirstItemMethod · 0.95
_focusLastItemMethod · 0.95
_focusNextItemMethod · 0.95
_focusPreviousItemMethod · 0.95
_setTypeAheadMethod · 0.95
_collapseCurrentItemMethod · 0.95
_expandCurrentItemMethod · 0.95

Calls 5

_activateCurrentItemMethod · 0.95
focusMethod · 0.65
unfocusMethod · 0.65
nextMethod · 0.45

Tested by

no test coverage detected