@@ -30,6 +30,12 @@ import {
3030} from "../../common" ;
3131
3232export interface IQueryListProps < T > extends IListItemsProps < T > {
33+ /**
34+ * Initial active item, useful if the parent component is controlling its selectedItem but
35+ * not activeItem.
36+ */
37+ initialActiveItem ?: T ;
38+
3339 /**
3440 * Callback invoked when user presses a key, after processing `QueryList`'s own key events
3541 * (up/down to navigate active item). This callback is passed to `renderer` and (along with
@@ -171,7 +177,7 @@ export class QueryList<T> extends AbstractComponent2<IQueryListProps<T>, IQueryL
171177 activeItem :
172178 props . activeItem !== undefined
173179 ? props . activeItem
174- : getFirstEnabledItem ( filteredItems , props . itemDisabled ) ,
180+ : props . initialActiveItem ?? getFirstEnabledItem ( filteredItems , props . itemDisabled ) ,
175181 createNewItem,
176182 filteredItems,
177183 query,
@@ -289,6 +295,21 @@ export class QueryList<T> extends AbstractComponent2<IQueryListProps<T>, IQueryL
289295 }
290296 }
291297
298+ public setActiveItem ( activeItem : T | ICreateNewItem | null ) {
299+ this . expectedNextActiveItem = activeItem ;
300+ if ( this . props . activeItem === undefined ) {
301+ // indicate that the active item may need to be scrolled into view after update.
302+ this . shouldCheckActiveItemInViewport = true ;
303+ this . setState ( { activeItem } ) ;
304+ }
305+
306+ if ( isCreateNewItem ( activeItem ) ) {
307+ Utils . safeInvoke ( this . props . onActiveItemChange , null , true ) ;
308+ } else {
309+ Utils . safeInvoke ( this . props . onActiveItemChange , activeItem , false ) ;
310+ }
311+ }
312+
292313 /** default `itemListRenderer` implementation */
293314 private renderItemList = ( listProps : IItemListRendererProps < T > ) => {
294315 const { initialContent, noResults } = this . props ;
@@ -486,21 +507,6 @@ export class QueryList<T> extends AbstractComponent2<IQueryListProps<T>, IQueryL
486507 return getFirstEnabledItem ( this . state . filteredItems , this . props . itemDisabled , direction , startIndex ) ;
487508 }
488509
489- private setActiveItem ( activeItem : T | ICreateNewItem | null ) {
490- this . expectedNextActiveItem = activeItem ;
491- if ( this . props . activeItem === undefined ) {
492- // indicate that the active item may need to be scrolled into view after update.
493- this . shouldCheckActiveItemInViewport = true ;
494- this . setState ( { activeItem } ) ;
495- }
496-
497- if ( isCreateNewItem ( activeItem ) ) {
498- Utils . safeInvoke ( this . props . onActiveItemChange , null , true ) ;
499- } else {
500- Utils . safeInvoke ( this . props . onActiveItemChange , activeItem , false ) ;
501- }
502- }
503-
504510 private isCreateItemRendered ( ) : boolean {
505511 return (
506512 this . canCreateItems ( ) &&
0 commit comments