(itemOrIndex: number | T, options: {emitChangeEvent?: boolean} = {})
| 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; |
no test coverage detected